From 9170ca3349fca483c2789dc26e0d7dabbbe822f5 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 16 May 2019 09:47:21 -0700 Subject: [PATCH 1/3] Backport #4101 https://github.com/rust-lang/rust-clippy/pull/4101 Splits up redundant_closure's method checking into a pedantic lint --- .travis.yml | 5 +++-- CHANGELOG.md | 1 + README.md | 2 +- appveyor.yml | 9 ++------- ci/base-tests.sh | 2 +- clippy_lints/src/eta_reduction.rs | 26 ++++++++++++++++++++++++-- clippy_lints/src/lib.rs | 1 + rust-toolchain | 2 +- tests/ui/eta.rs | 6 +++++- tests/ui/eta.stderr | 24 +++++++++++++----------- tests/ui/map_clone.fixed | 2 +- tests/ui/map_clone.rs | 2 +- 12 files changed, 54 insertions(+), 28 deletions(-) diff --git a/.travis.yml b/.travis.yml index acb5b9ae0d24..7a7315663851 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: rust -rust: nightly +rust: beta os: - linux @@ -17,6 +17,7 @@ branches: env: global: - RUST_BACKTRACE=1 + - RUSTC_BOOTSTRAP=1 install: - | @@ -90,7 +91,7 @@ matrix: script: - | rm rust-toolchain - ./setup-toolchain.sh + rustup override set beta export LD_LIBRARY_PATH=$(rustc --print sysroot)/lib - | if [ -z ${INTEGRATION} ]; then diff --git a/CHANGELOG.md b/CHANGELOG.md index 7837b899b533..aca02b53d017 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -978,6 +978,7 @@ All notable changes to this project will be documented in this file. [`redundant_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone [`redundant_closure`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure [`redundant_closure_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_call +[`redundant_closure_for_method_calls`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls [`redundant_field_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names [`redundant_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern [`redundant_pattern_matching`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching diff --git a/README.md b/README.md index 43b477d2cef3..523704a86f33 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code. -[There are 298 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) +[There are 299 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you: diff --git a/appveyor.yml b/appveyor.yml index c852cd8232e1..0bf74c6fe707 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -16,14 +16,8 @@ branches: install: - curl -sSf -o rustup-init.exe https://win.rustup.rs/ - - rustup-init.exe -y --default-host %TARGET% --default-toolchain nightly + - rustup-init.exe -y --default-host %TARGET% --default-toolchain beta - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin - - git ls-remote https://github.com/rust-lang/rust.git master | awk '{print $1}' >rustc-hash.txt - - set /p RUSTC_HASH= LintArray { - lint_array!(REDUNDANT_CLOSURE) + lint_array!(REDUNDANT_CLOSURE, REDUNDANT_CLOSURE_FOR_METHOD_CALLS) } fn name(&self) -> &'static str { @@ -110,7 +132,7 @@ fn check_closure(cx: &LateContext<'_, '_>, expr: &Expr) { if let Some(name) = get_ufcs_type_name(cx, method_def_id, &args[0]); then { - span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure found", |db| { + span_lint_and_then(cx, REDUNDANT_CLOSURE_FOR_METHOD_CALLS, expr.span, "redundant closure found", |db| { db.span_suggestion( expr.span, "remove closure as shown", diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index d547e0a99b79..dca0badc29f2 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -611,6 +611,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { enum_glob_use::ENUM_GLOB_USE, enum_variants::MODULE_NAME_REPETITIONS, enum_variants::PUB_ENUM_VARIANT_NAMES, + eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS, functions::TOO_MANY_LINES, if_not_else::IF_NOT_ELSE, infinite_iter::MAYBE_INFINITE_ITER, diff --git a/rust-toolchain b/rust-toolchain index bf867e0ae5b6..65b2df87f7df 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly +beta diff --git a/tests/ui/eta.rs b/tests/ui/eta.rs index f777939c67d2..d65f8eaab1cb 100644 --- a/tests/ui/eta.rs +++ b/tests/ui/eta.rs @@ -7,7 +7,11 @@ clippy::option_map_unit_fn, clippy::trivially_copy_pass_by_ref )] -#![warn(clippy::redundant_closure, clippy::needless_borrow)] +#![warn( + clippy::redundant_closure, + clippy::redundant_closure_for_method_calls, + clippy::needless_borrow +)] use std::path::PathBuf; diff --git a/tests/ui/eta.stderr b/tests/ui/eta.stderr index 5f56cd7912a7..0d9f48d6f4a2 100644 --- a/tests/ui/eta.stderr +++ b/tests/ui/eta.stderr @@ -1,5 +1,5 @@ error: redundant closure found - --> $DIR/eta.rs:15:27 + --> $DIR/eta.rs:19:27 | LL | let a = Some(1u8).map(|a| foo(a)); | ^^^^^^^^^^ help: remove closure as shown: `foo` @@ -7,19 +7,19 @@ LL | let a = Some(1u8).map(|a| foo(a)); = note: `-D clippy::redundant-closure` implied by `-D warnings` error: redundant closure found - --> $DIR/eta.rs:16:10 + --> $DIR/eta.rs:20:10 | LL | meta(|a| foo(a)); | ^^^^^^^^^^ help: remove closure as shown: `foo` error: redundant closure found - --> $DIR/eta.rs:17:27 + --> $DIR/eta.rs:21:27 | LL | let c = Some(1u8).map(|a| {1+2; foo}(a)); | ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `{1+2; foo}` error: this expression borrows a reference that is immediately dereferenced by the compiler - --> $DIR/eta.rs:19:21 + --> $DIR/eta.rs:23:21 | LL | all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted | ^^^ help: change this to: `&2` @@ -27,43 +27,45 @@ LL | all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted = note: `-D clippy::needless-borrow` implied by `-D warnings` error: redundant closure found - --> $DIR/eta.rs:26:27 + --> $DIR/eta.rs:30:27 | LL | let e = Some(1u8).map(|a| generic(a)); | ^^^^^^^^^^^^^^ help: remove closure as shown: `generic` error: redundant closure found - --> $DIR/eta.rs:69:51 + --> $DIR/eta.rs:73:51 | LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.foo()); | ^^^^^^^^^^^ help: remove closure as shown: `TestStruct::foo` + | + = note: `-D clippy::redundant-closure-for-method-calls` implied by `-D warnings` error: redundant closure found - --> $DIR/eta.rs:71:51 + --> $DIR/eta.rs:75:51 | LL | let e = Some(TestStruct { some_ref: &i }).map(|a| a.trait_foo()); | ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `TestTrait::trait_foo` error: redundant closure found - --> $DIR/eta.rs:74:42 + --> $DIR/eta.rs:78:42 | LL | let e = Some(&mut vec![1, 2, 3]).map(|v| v.clear()); | ^^^^^^^^^^^^^ help: remove closure as shown: `std::vec::Vec::clear` error: redundant closure found - --> $DIR/eta.rs:79:29 + --> $DIR/eta.rs:83:29 | LL | let e = Some("str").map(|s| s.to_string()); | ^^^^^^^^^^^^^^^^^ help: remove closure as shown: `std::string::ToString::to_string` error: redundant closure found - --> $DIR/eta.rs:81:27 + --> $DIR/eta.rs:85:27 | LL | let e = Some('a').map(|s| s.to_uppercase()); | ^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `char::to_uppercase` error: redundant closure found - --> $DIR/eta.rs:84:65 + --> $DIR/eta.rs:88:65 | LL | let e: std::vec::Vec = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `char::to_ascii_uppercase` diff --git a/tests/ui/map_clone.fixed b/tests/ui/map_clone.fixed index d804e838d5a6..afd1568a3e11 100644 --- a/tests/ui/map_clone.fixed +++ b/tests/ui/map_clone.fixed @@ -3,7 +3,7 @@ #![allow(clippy::iter_cloned_collect)] #![allow(clippy::clone_on_copy)] #![allow(clippy::missing_docs_in_private_items)] -#![allow(clippy::redundant_closure)] +#![allow(clippy::redundant_closure_for_method_calls)] fn main() { let _: Vec = vec![5_i8; 6].iter().cloned().collect(); diff --git a/tests/ui/map_clone.rs b/tests/ui/map_clone.rs index d98cd939d8cc..deacaf4abe8b 100644 --- a/tests/ui/map_clone.rs +++ b/tests/ui/map_clone.rs @@ -3,7 +3,7 @@ #![allow(clippy::iter_cloned_collect)] #![allow(clippy::clone_on_copy)] #![allow(clippy::missing_docs_in_private_items)] -#![allow(clippy::redundant_closure)] +#![allow(clippy::redundant_closure_for_method_calls)] fn main() { let _: Vec = vec![5_i8; 6].iter().map(|x| *x).collect(); From 64a9f568e9b96c1843218200140c7029e6e27494 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Fri, 17 May 2019 13:19:28 +0200 Subject: [PATCH 2/3] Don't run dogfood on windows --- src/main.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index e0b2bcc72664..05e0c58f88d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,10 +86,9 @@ where }) .map(|p| ("CARGO_TARGET_DIR", p)); - // Run the dogfood tests directly on nightly cargo. This is required due - // to a bug in rustup.rs when running cargo on custom toolchains. See issue #3118. + // Don't run the dogfood tests on beta if std::env::var_os("CLIPPY_DOGFOOD").is_some() && cfg!(windows) { - args.insert(0, "+nightly".to_string()); + return Ok(()) } let exit_status = std::process::Command::new("cargo") From 28bde0638e2b28512d77cea2416cb44c1bbfdc05 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Fri, 17 May 2019 15:47:11 +0200 Subject: [PATCH 3/3] Don't require rustfmt on beta --- ci/base-tests.sh | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/ci/base-tests.sh b/ci/base-tests.sh index 187fc062fbf6..14b573454bbe 100755 --- a/ci/base-tests.sh +++ b/ci/base-tests.sh @@ -23,7 +23,6 @@ export CARGO_TARGET_DIR=`pwd`/target/ # Perform various checks for lint registration ./util/dev update_lints --check -cargo +nightly fmt --all -- --check # Check running clippy-driver without cargo ( @@ -50,29 +49,5 @@ cargo +nightly fmt --all -- --check # TODO: CLIPPY_CONF_DIR / CARGO_MANIFEST_DIR ) -# make sure tests are formatted - -# some lints are sensitive to formatting, exclude some files -tests_need_reformatting="false" -# switch to nightly -rustup override set nightly -# avoid loop spam and allow cmds with exit status != 0 -set +ex - -for file in `find tests | grep "\.rs$"` ; do - rustfmt ${file} --check - if [ $? -ne 0 ]; then - echo "${file} needs reformatting!" - tests_need_reformatting="true" - fi -done - -set -ex # reset - -if [ "${tests_need_reformatting}" == "true" ] ; then - echo "Tests need reformatting!" - exit 2 -fi - # switch back to master # rustup override set master