Handle calls with 'std::convert::identity'

This commit is contained in:
Jeremy Stucki 2019-08-11 19:51:43 +02:00
parent 05d9f884e1
commit f0ce04f814
No known key found for this signature in database
GPG Key ID: EEFCA93148042655
5 changed files with 33 additions and 3 deletions

View File

@ -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()`

View File

@ -95,6 +95,7 @@
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"];

View File

@ -6,7 +6,7 @@
pub use lint::LINT_LEVELS;
// begin lint list, do not remove this comment, its used in `update_lints`
pub const ALL_LINTS: [Lint; 309] = [
pub const ALL_LINTS: [Lint; 310] = [
Lint {
name: "absurd_extreme_comparisons",
group: "correctness",

View File

@ -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);
}

View File

@ -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