mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-14 08:57:30 +00:00
Reuse is_expr_identity_function
for filter_map_identity
This commit is contained in:
parent
bb3b58cfcc
commit
9e54ce865c
4 changed files with 29 additions and 35 deletions
|
@ -1,6 +1,5 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::{is_expr_path_def_path, is_trait_method, path_to_local_id, paths};
|
||||
use if_chain::if_chain;
|
||||
use clippy_utils::{is_expr_identity_function, is_trait_method};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir as hir;
|
||||
use rustc_lint::LateContext;
|
||||
|
@ -9,32 +8,15 @@ use rustc_span::{source_map::Span, sym};
|
|||
use super::FILTER_MAP_IDENTITY;
|
||||
|
||||
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, filter_map_arg: &hir::Expr<'_>, filter_map_span: Span) {
|
||||
if is_trait_method(cx, expr, sym::Iterator) {
|
||||
let apply_lint = |message: &str| {
|
||||
if is_trait_method(cx, expr, sym::Iterator) && is_expr_identity_function(cx, filter_map_arg) {
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
FILTER_MAP_IDENTITY,
|
||||
filter_map_span.with_hi(expr.span.hi()),
|
||||
message,
|
||||
"use of `filter_map` with an identity function",
|
||||
"try",
|
||||
"flatten()".to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
};
|
||||
|
||||
if_chain! {
|
||||
if let hir::ExprKind::Closure(_, _, body_id, _, _) = filter_map_arg.kind;
|
||||
let body = cx.tcx.hir().body(body_id);
|
||||
|
||||
if let hir::PatKind::Binding(_, binding_id, ..) = body.params[0].pat.kind;
|
||||
if path_to_local_id(&body.value, binding_id);
|
||||
then {
|
||||
apply_lint("called `filter_map(|x| x)` on an `Iterator`");
|
||||
}
|
||||
}
|
||||
|
||||
if is_expr_path_def_path(cx, filter_map_arg, &paths::CONVERT_IDENTITY) {
|
||||
apply_lint("called `filter_map(std::convert::identity)` on an `Iterator`");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// run-rustfix
|
||||
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_imports, clippy::needless_return)]
|
||||
#![warn(clippy::filter_map_identity)]
|
||||
|
||||
fn main() {
|
||||
|
@ -13,4 +13,7 @@ fn main() {
|
|||
use std::convert::identity;
|
||||
let iterator = vec![Some(1), None, Some(2)].into_iter();
|
||||
let _ = iterator.flatten();
|
||||
|
||||
let iterator = vec![Some(1), None, Some(2)].into_iter();
|
||||
let _ = iterator.flatten();
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// run-rustfix
|
||||
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_imports, clippy::needless_return)]
|
||||
#![warn(clippy::filter_map_identity)]
|
||||
|
||||
fn main() {
|
||||
|
@ -13,4 +13,7 @@ fn main() {
|
|||
use std::convert::identity;
|
||||
let iterator = vec![Some(1), None, Some(2)].into_iter();
|
||||
let _ = iterator.filter_map(identity);
|
||||
|
||||
let iterator = vec![Some(1), None, Some(2)].into_iter();
|
||||
let _ = iterator.filter_map(|x| return x);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
error: called `filter_map(|x| x)` on an `Iterator`
|
||||
error: use of `filter_map` with an identity function
|
||||
--> $DIR/filter_map_identity.rs:8:22
|
||||
|
|
||||
LL | let _ = iterator.filter_map(|x| x);
|
||||
|
@ -6,17 +6,23 @@ LL | let _ = iterator.filter_map(|x| x);
|
|||
|
|
||||
= note: `-D clippy::filter-map-identity` implied by `-D warnings`
|
||||
|
||||
error: called `filter_map(std::convert::identity)` on an `Iterator`
|
||||
error: use of `filter_map` with an identity function
|
||||
--> $DIR/filter_map_identity.rs:11:22
|
||||
|
|
||||
LL | let _ = iterator.filter_map(std::convert::identity);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()`
|
||||
|
||||
error: called `filter_map(std::convert::identity)` on an `Iterator`
|
||||
error: use of `filter_map` with an identity function
|
||||
--> $DIR/filter_map_identity.rs:15:22
|
||||
|
|
||||
LL | let _ = iterator.filter_map(identity);
|
||||
| ^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()`
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
error: use of `filter_map` with an identity function
|
||||
--> $DIR/filter_map_identity.rs:18:22
|
||||
|
|
||||
LL | let _ = iterator.filter_map(|x| return x);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()`
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue