diff options
| author | Jeremy Stucki <jeremy@myelin.ch> | 2019-08-11 19:51:43 +0200 |
|---|---|---|
| committer | Jeremy Stucki <jeremy@myelin.ch> | 2019-08-11 19:51:43 +0200 |
| commit | f0ce04f81432047bdd9b3a96a57ba06264f18471 (patch) | |
| tree | 707ed0a0b210f7bea6cd3d4f0d97aed954a32f66 | |
| parent | 05d9f884e17599a72f6725018747f1732a2859f8 (diff) | |
| download | rust-f0ce04f81432047bdd9b3a96a57ba06264f18471.tar.gz rust-f0ce04f81432047bdd9b3a96a57ba06264f18471.zip | |
Handle calls with 'std::convert::identity'
| -rw-r--r-- | clippy_lints/src/methods/mod.rs | 18 | ||||
| -rw-r--r-- | clippy_lints/src/utils/paths.rs | 1 | ||||
| -rw-r--r-- | src/lintlist/mod.rs | 2 | ||||
| -rw-r--r-- | tests/ui/unnecessary_flat_map.rs | 5 | ||||
| -rw-r--r-- | tests/ui/unnecessary_flat_map.stderr | 10 |
5 files changed, 33 insertions, 3 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index e51e432749c..58b33524589 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -2186,6 +2186,24 @@ fn lint_flat_map<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr, fl span_lint(cx, FLAT_MAP, expr.span, msg); } } + + if_chain! { + if match_trait_method(cx, expr, &paths::ITERATOR); + + if flat_map_args.len() == 2; + + let expr = &flat_map_args[1]; + + if let hir::ExprKind::Path(ref qpath) = expr.node; + + if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY); + + then { + let msg = "called `flat_map(std::convert::identity)` on an `Iterator`. \ + This can be simplified by calling `flatten().`"; + span_lint(cx, FLAT_MAP, expr.span, msg); + } + } } /// lint searching an Iterator followed by `is_some()` diff --git a/clippy_lints/src/utils/paths.rs b/clippy_lints/src/utils/paths.rs index 62b22afff95..be811da0217 100644 --- a/clippy_lints/src/utils/paths.rs +++ b/clippy_lints/src/utils/paths.rs @@ -95,6 +95,7 @@ pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "<impl [T]>", "into_vec pub const SLICE_ITER: [&str; 3] = ["core", "slice", "Iter"]; pub const STDERR: [&str; 4] = ["std", "io", "stdio", "stderr"]; pub const STDOUT: [&str; 4] = ["std", "io", "stdio", "stdout"]; +pub const STD_CONVERT_IDENTITY: [&str; 3] = ["std", "convert", "identity"]; pub const STD_MEM_TRANSMUTE: [&str; 3] = ["std", "mem", "transmute"]; pub const STD_PTR_NULL: [&str; 3] = ["std", "ptr", "null"]; pub const STRING: [&str; 3] = ["alloc", "string", "String"]; diff --git a/src/lintlist/mod.rs b/src/lintlist/mod.rs index 23cad950a95..08179ec34e8 100644 --- a/src/lintlist/mod.rs +++ b/src/lintlist/mod.rs @@ -6,7 +6,7 @@ pub use lint::Lint; pub use lint::LINT_LEVELS; // begin lint list, do not remove this comment, it’s used in `update_lints` -pub const ALL_LINTS: [Lint; 309] = [ +pub const ALL_LINTS: [Lint; 310] = [ Lint { name: "absurd_extreme_comparisons", group: "correctness", diff --git a/tests/ui/unnecessary_flat_map.rs b/tests/ui/unnecessary_flat_map.rs index d0072eca9d2..b61569d1b93 100644 --- a/tests/ui/unnecessary_flat_map.rs +++ b/tests/ui/unnecessary_flat_map.rs @@ -1,6 +1,11 @@ #![warn(clippy::flat_map)] +use std::convert; + fn main() { let iterator = [[0, 1], [2, 3], [4, 5]].iter(); iterator.flat_map(|x| x); + + let iterator = [[0, 1], [2, 3], [4, 5]].iter(); + iterator.flat_map(convert::identity); } diff --git a/tests/ui/unnecessary_flat_map.stderr b/tests/ui/unnecessary_flat_map.stderr index 9ebef07f1b7..c98b403d29c 100644 --- a/tests/ui/unnecessary_flat_map.stderr +++ b/tests/ui/unnecessary_flat_map.stderr @@ -1,10 +1,16 @@ error: called `flat_map(|x| x)` on an `Iterator`. This can be simplified by calling `flatten().` - --> $DIR/unnecessary_flat_map.rs:5:5 + --> $DIR/unnecessary_flat_map.rs:7:5 | LL | iterator.flat_map(|x| x); | ^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `-D clippy::flat-map` implied by `-D warnings` -error: aborting due to previous error +error: called `flat_map(std::convert::identity)` on an `Iterator`. This can be simplified by calling `flatten().` + --> $DIR/unnecessary_flat_map.rs:10:23 + | +LL | iterator.flat_map(convert::identity); + | ^^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors |
