Handle calls with 'std::convert::identity'
This commit is contained in:
parent
05d9f884e1
commit
f0ce04f814
@ -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()`
|
||||
|
@ -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"];
|
||||
|
@ -6,7 +6,7 @@
|
||||
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",
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user