Skip to content

Commit f0ce04f

Browse files
committed
Handle calls with 'std::convert::identity'
1 parent 05d9f88 commit f0ce04f

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,6 +2186,24 @@ fn lint_flat_map<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr, fl
21862186
span_lint(cx, FLAT_MAP, expr.span, msg);
21872187
}
21882188
}
2189+
2190+
if_chain! {
2191+
if match_trait_method(cx, expr, &paths::ITERATOR);
2192+
2193+
if flat_map_args.len() == 2;
2194+
2195+
let expr = &flat_map_args[1];
2196+
2197+
if let hir::ExprKind::Path(ref qpath) = expr.node;
2198+
2199+
if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);
2200+
2201+
then {
2202+
let msg = "called `flat_map(std::convert::identity)` on an `Iterator`. \
2203+
This can be simplified by calling `flatten().`";
2204+
span_lint(cx, FLAT_MAP, expr.span, msg);
2205+
}
2206+
}
21892207
}
21902208

21912209
/// lint searching an Iterator followed by `is_some()`

clippy_lints/src/utils/paths.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "<impl [T]>", "into_vec
9595
pub const SLICE_ITER: [&str; 3] = ["core", "slice", "Iter"];
9696
pub const STDERR: [&str; 4] = ["std", "io", "stdio", "stderr"];
9797
pub const STDOUT: [&str; 4] = ["std", "io", "stdio", "stdout"];
98+
pub const STD_CONVERT_IDENTITY: [&str; 3] = ["std", "convert", "identity"];
9899
pub const STD_MEM_TRANSMUTE: [&str; 3] = ["std", "mem", "transmute"];
99100
pub const STD_PTR_NULL: [&str; 3] = ["std", "ptr", "null"];
100101
pub const STRING: [&str; 3] = ["alloc", "string", "String"];

src/lintlist/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 309] = [
9+
pub const ALL_LINTS: [Lint; 310] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",

tests/ui/unnecessary_flat_map.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#![warn(clippy::flat_map)]
22

3+
use std::convert;
4+
35
fn main() {
46
let iterator = [[0, 1], [2, 3], [4, 5]].iter();
57
iterator.flat_map(|x| x);
8+
9+
let iterator = [[0, 1], [2, 3], [4, 5]].iter();
10+
iterator.flat_map(convert::identity);
611
}

tests/ui/unnecessary_flat_map.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
error: called `flat_map(|x| x)` on an `Iterator`. This can be simplified by calling `flatten().`
2-
--> $DIR/unnecessary_flat_map.rs:5:5
2+
--> $DIR/unnecessary_flat_map.rs:7:5
33
|
44
LL | iterator.flat_map(|x| x);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::flat-map` implied by `-D warnings`
88

9-
error: aborting due to previous error
9+
error: called `flat_map(std::convert::identity)` on an `Iterator`. This can be simplified by calling `flatten().`
10+
--> $DIR/unnecessary_flat_map.rs:10:23
11+
|
12+
LL | iterator.flat_map(convert::identity);
13+
| ^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to 2 previous errors
1016

0 commit comments

Comments
 (0)