From bd74fdce2288746b2a4db678830447385e3319d2 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sun, 9 Dec 2018 16:26:03 +0100 Subject: [PATCH 1/6] Use WIP branch for compiletest_rs --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 359a6e43bdbb..13d081e92cf9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,7 @@ rustc_tools_util = { version = "0.1.0", path = "rustc_tools_util"} [dev-dependencies] clippy_dev = { version = "0.0.1", path = "clippy_dev" } cargo_metadata = "0.6.2" -compiletest_rs = "0.3.16" +compiletest_rs = { git = "https://github.com/phansch/compiletest-rs.git", branch = "add_rustfix_support" } lazy_static = "1.0" serde_derive = "1.0" clippy-mini-macro-test = { version = "0.2", path = "mini-macro" } From 2ccfd52f5dd0774cf17d6c5459460cc9fcea6907 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sun, 9 Dec 2018 16:18:16 +0100 Subject: [PATCH 2/6] Run rustfix on first UI test --- tests/ui/unnecessary_ref.fixed | 23 +++++++++++++++++++++++ tests/ui/unnecessary_ref.rs | 3 ++- tests/ui/unnecessary_ref.stderr | 4 ++-- 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 tests/ui/unnecessary_ref.fixed diff --git a/tests/ui/unnecessary_ref.fixed b/tests/ui/unnecessary_ref.fixed new file mode 100644 index 000000000000..32ad6d720541 --- /dev/null +++ b/tests/ui/unnecessary_ref.fixed @@ -0,0 +1,23 @@ +// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// run-rustfix + + +#![feature(stmt_expr_attributes)] + +struct Outer { + inner: u32, +} + +#[deny(clippy::ref_in_deref)] +fn main() { + let outer = Outer { inner: 0 }; + let inner = outer.inner.inner; +} diff --git a/tests/ui/unnecessary_ref.rs b/tests/ui/unnecessary_ref.rs index 31aa367e5064..4ed47cf8b5dc 100644 --- a/tests/ui/unnecessary_ref.rs +++ b/tests/ui/unnecessary_ref.rs @@ -7,7 +7,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(tool_attributes)] +// run-rustfix + #![feature(stmt_expr_attributes)] struct Outer { diff --git a/tests/ui/unnecessary_ref.stderr b/tests/ui/unnecessary_ref.stderr index 77503026bde6..dc221f9921ef 100644 --- a/tests/ui/unnecessary_ref.stderr +++ b/tests/ui/unnecessary_ref.stderr @@ -1,11 +1,11 @@ error: Creating a reference that is immediately dereferenced. - --> $DIR/unnecessary_ref.rs:20:17 + --> $DIR/unnecessary_ref.rs:21:17 | LL | let inner = (&outer).inner; | ^^^^^^^^ help: try this: `outer.inner` | note: lint level defined here - --> $DIR/unnecessary_ref.rs:17:8 + --> $DIR/unnecessary_ref.rs:18:8 | LL | #[deny(clippy::ref_in_deref)] | ^^^^^^^^^^^^^^^^^^^^ From 7d9bf99df14e04ab2d1158e3f06ecc3291145bf0 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sun, 9 Dec 2018 16:57:06 +0100 Subject: [PATCH 3/6] Update .fixed files via update-references.sh --- tests/ui/update-references.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/ui/update-references.sh b/tests/ui/update-references.sh index aa99d35f7aa7..d6995985a3b1 100755 --- a/tests/ui/update-references.sh +++ b/tests/ui/update-references.sh @@ -34,6 +34,7 @@ shift while [[ "$1" != "" ]]; do STDERR_NAME="${1/%.rs/.stderr}" STDOUT_NAME="${1/%.rs/.stdout}" + FIXED_NAME="${1/%.rs/.fixed}" shift if [ -f $BUILD_DIR/$STDOUT_NAME ] && \ ! (diff $BUILD_DIR/$STDOUT_NAME $MYDIR/$STDOUT_NAME >& /dev/null); then @@ -45,6 +46,11 @@ while [[ "$1" != "" ]]; do echo updating $MYDIR/$STDERR_NAME cp $BUILD_DIR/$STDERR_NAME $MYDIR/$STDERR_NAME fi + if [ -f $BUILD_DIR/$FIXED_NAME ] && \ + ! (diff $BUILD_DIR/$FIXED_NAME $MYDIR/$FIXED_NAME >& /dev/null); then + echo updating $MYDIR/$FIXED_NAME + cp $BUILD_DIR/$FIXED_NAME $MYDIR/$FIXED_NAME + fi done From 3f978afe8d2260e29dbc593fa294ccaa39b0df32 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sun, 9 Dec 2018 17:01:51 +0100 Subject: [PATCH 4/6] Update CONTRIBUTING.md for rustfix tests --- CONTRIBUTING.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 94120a3771a2..df216a8fbc95 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -156,6 +156,15 @@ Therefore you should use `tests/ui/update-all-references.sh` (after running `cargo test`) and check whether the output looks as you expect with `git diff`. Commit all `*.stderr` files, too. +If the lint you are working on is making use of structured suggestions, the +test file should include a `// run-rustfix` comment at the top. This will +additionally run [rustfix](https://github.com/rust-lang-nursery/rustfix) for +that test. Rustfix will apply the suggestions from the lint to the code of the +test file and compare that to the contents of a `.fixed` file. + +Use `tests/ui/update-all-references.sh` to automatically generate the +`.fixed` file after running `cargo test`. + ### Running rustfmt [Rustfmt](https://github.com/rust-lang/rustfmt) is a tool for formatting Rust code according From 298aedf2f874ed86a68966c7b722918b1e2bde0a Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sun, 9 Dec 2018 17:25:45 +0100 Subject: [PATCH 5/6] Fix suggestion for unnecessary_ref lint --- clippy_lints/src/reference.rs | 8 ++------ tests/ui/unnecessary_ref.fixed | 4 ++-- tests/ui/unnecessary_ref.rs | 1 + tests/ui/unnecessary_ref.stderr | 6 +++--- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/reference.rs b/clippy_lints/src/reference.rs index 2e35719d4660..d76fa0c38b99 100644 --- a/clippy_lints/src/reference.rs +++ b/clippy_lints/src/reference.rs @@ -98,7 +98,7 @@ impl LintPass for DerefPass { impl EarlyLintPass for DerefPass { fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) { if_chain! { - if let ExprKind::Field(ref object, ref field_name) = e.node; + if let ExprKind::Field(ref object, _) = e.node; if let ExprKind::Paren(ref parened) = object.node; if let ExprKind::AddrOf(_, ref inner) = parened.node; then { @@ -109,11 +109,7 @@ impl EarlyLintPass for DerefPass { object.span, "Creating a reference that is immediately dereferenced.", "try this", - format!( - "{}.{}", - snippet_with_applicability(cx, inner.span, "_", &mut applicability), - snippet_with_applicability(cx, field_name.span, "_", &mut applicability) - ), + snippet_with_applicability(cx, inner.span, "_", &mut applicability).to_string(), applicability, ); } diff --git a/tests/ui/unnecessary_ref.fixed b/tests/ui/unnecessary_ref.fixed index 32ad6d720541..3617641a116c 100644 --- a/tests/ui/unnecessary_ref.fixed +++ b/tests/ui/unnecessary_ref.fixed @@ -9,8 +9,8 @@ // run-rustfix - #![feature(stmt_expr_attributes)] +#![allow(unused_variables)] struct Outer { inner: u32, @@ -19,5 +19,5 @@ struct Outer { #[deny(clippy::ref_in_deref)] fn main() { let outer = Outer { inner: 0 }; - let inner = outer.inner.inner; + let inner = outer.inner; } diff --git a/tests/ui/unnecessary_ref.rs b/tests/ui/unnecessary_ref.rs index 4ed47cf8b5dc..48101c87a54e 100644 --- a/tests/ui/unnecessary_ref.rs +++ b/tests/ui/unnecessary_ref.rs @@ -10,6 +10,7 @@ // run-rustfix #![feature(stmt_expr_attributes)] +#![allow(unused_variables)] struct Outer { inner: u32, diff --git a/tests/ui/unnecessary_ref.stderr b/tests/ui/unnecessary_ref.stderr index dc221f9921ef..863a6389e7fc 100644 --- a/tests/ui/unnecessary_ref.stderr +++ b/tests/ui/unnecessary_ref.stderr @@ -1,11 +1,11 @@ error: Creating a reference that is immediately dereferenced. - --> $DIR/unnecessary_ref.rs:21:17 + --> $DIR/unnecessary_ref.rs:22:17 | LL | let inner = (&outer).inner; - | ^^^^^^^^ help: try this: `outer.inner` + | ^^^^^^^^ help: try this: `outer` | note: lint level defined here - --> $DIR/unnecessary_ref.rs:18:8 + --> $DIR/unnecessary_ref.rs:19:8 | LL | #[deny(clippy::ref_in_deref)] | ^^^^^^^^^^^^^^^^^^^^ From ec1395aafbf4c95ad9043cc256eb66bd0d622bb5 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Thu, 3 Jan 2019 15:42:59 +0100 Subject: [PATCH 6/6] Update to latest compiletest-rs release --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 13d081e92cf9..17ddda4e8dd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,7 @@ rustc_tools_util = { version = "0.1.0", path = "rustc_tools_util"} [dev-dependencies] clippy_dev = { version = "0.0.1", path = "clippy_dev" } cargo_metadata = "0.6.2" -compiletest_rs = { git = "https://github.com/phansch/compiletest-rs.git", branch = "add_rustfix_support" } +compiletest_rs = "0.3.18" lazy_static = "1.0" serde_derive = "1.0" clippy-mini-macro-test = { version = "0.2", path = "mini-macro" }