Skip to content

Commit 5b8b01e

Browse files
committed
Auto merge of #3519 - phansch:brave_newer_ui_tests, r=flip1995
Integrate rustfix into Clippy test suite Once the [PR to compiletest-rs](Manishearth/compiletest-rs#151) is reviewed and merged this fixes #2376. I will create a separate tracking issue for adding `run-rustfix` to all tests.
2 parents baec524 + ec1395a commit 5b8b01e

7 files changed

+47
-11
lines changed

CONTRIBUTING.md

+9
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ Therefore you should use `tests/ui/update-all-references.sh` (after running
156156
`cargo test`) and check whether the output looks as you expect with `git diff`. Commit all
157157
`*.stderr` files, too.
158158

159+
If the lint you are working on is making use of structured suggestions, the
160+
test file should include a `// run-rustfix` comment at the top. This will
161+
additionally run [rustfix](https://github.com/rust-lang-nursery/rustfix) for
162+
that test. Rustfix will apply the suggestions from the lint to the code of the
163+
test file and compare that to the contents of a `.fixed` file.
164+
165+
Use `tests/ui/update-all-references.sh` to automatically generate the
166+
`.fixed` file after running `cargo test`.
167+
159168
### Running rustfmt
160169

161170
[Rustfmt](https://github.com/rust-lang/rustfmt) is a tool for formatting Rust code according

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ rustc_tools_util = { version = "0.1.1", path = "rustc_tools_util"}
4848
[dev-dependencies]
4949
clippy_dev = { version = "0.0.1", path = "clippy_dev" }
5050
cargo_metadata = "0.6.2"
51-
compiletest_rs = "0.3.16"
51+
compiletest_rs = "0.3.18"
5252
lazy_static = "1.0"
5353
serde_derive = "1.0"
5454
clippy-mini-macro-test = { version = "0.2", path = "mini-macro" }

clippy_lints/src/reference.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl LintPass for DerefPass {
9898
impl EarlyLintPass for DerefPass {
9999
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
100100
if_chain! {
101-
if let ExprKind::Field(ref object, ref field_name) = e.node;
101+
if let ExprKind::Field(ref object, _) = e.node;
102102
if let ExprKind::Paren(ref parened) = object.node;
103103
if let ExprKind::AddrOf(_, ref inner) = parened.node;
104104
then {
@@ -109,11 +109,7 @@ impl EarlyLintPass for DerefPass {
109109
object.span,
110110
"Creating a reference that is immediately dereferenced.",
111111
"try this",
112-
format!(
113-
"{}.{}",
114-
snippet_with_applicability(cx, inner.span, "_", &mut applicability),
115-
snippet_with_applicability(cx, field_name.span, "_", &mut applicability)
116-
),
112+
snippet_with_applicability(cx, inner.span, "_", &mut applicability).to_string(),
117113
applicability,
118114
);
119115
}

tests/ui/unnecessary_ref.fixed

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
// run-rustfix
11+
12+
#![feature(stmt_expr_attributes)]
13+
#![allow(unused_variables)]
14+
15+
struct Outer {
16+
inner: u32,
17+
}
18+
19+
#[deny(clippy::ref_in_deref)]
20+
fn main() {
21+
let outer = Outer { inner: 0 };
22+
let inner = outer.inner;
23+
}

tests/ui/unnecessary_ref.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
#![feature(tool_attributes)]
10+
// run-rustfix
11+
1112
#![feature(stmt_expr_attributes)]
13+
#![allow(unused_variables)]
1214

1315
struct Outer {
1416
inner: u32,

tests/ui/unnecessary_ref.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: Creating a reference that is immediately dereferenced.
2-
--> $DIR/unnecessary_ref.rs:20:17
2+
--> $DIR/unnecessary_ref.rs:22:17
33
|
44
LL | let inner = (&outer).inner;
5-
| ^^^^^^^^ help: try this: `outer.inner`
5+
| ^^^^^^^^ help: try this: `outer`
66
|
77
note: lint level defined here
8-
--> $DIR/unnecessary_ref.rs:17:8
8+
--> $DIR/unnecessary_ref.rs:19:8
99
|
1010
LL | #[deny(clippy::ref_in_deref)]
1111
| ^^^^^^^^^^^^^^^^^^^^

tests/ui/update-references.sh

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ shift
3434
while [[ "$1" != "" ]]; do
3535
STDERR_NAME="${1/%.rs/.stderr}"
3636
STDOUT_NAME="${1/%.rs/.stdout}"
37+
FIXED_NAME="${1/%.rs/.fixed}"
3738
shift
3839
if [ -f $BUILD_DIR/$STDOUT_NAME ] && \
3940
! (diff $BUILD_DIR/$STDOUT_NAME $MYDIR/$STDOUT_NAME >& /dev/null); then
@@ -45,6 +46,11 @@ while [[ "$1" != "" ]]; do
4546
echo updating $MYDIR/$STDERR_NAME
4647
cp $BUILD_DIR/$STDERR_NAME $MYDIR/$STDERR_NAME
4748
fi
49+
if [ -f $BUILD_DIR/$FIXED_NAME ] && \
50+
! (diff $BUILD_DIR/$FIXED_NAME $MYDIR/$FIXED_NAME >& /dev/null); then
51+
echo updating $MYDIR/$FIXED_NAME
52+
cp $BUILD_DIR/$FIXED_NAME $MYDIR/$FIXED_NAME
53+
fi
4854
done
4955

5056

0 commit comments

Comments
 (0)