Auto merge of #12562 - m-rph:12501, r=y21
Allow `filter_map_identity` when the closure is typed This extends the `filter_map_identity` lint to support typed closures. For untyped closures, we know that the program compiles, and therefore we can safely suggest using flatten. For typed closures, they may participate in type resolution. In this case we use `Applicability::MaybeIncorrect`. Details: https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Should.20.60filter_map_identity.60.20lint.20when.20closures.20are.20typed.3F changelog: `filter_map_identity` will now suggest using flatten for typed closures. r? `@y21` && `@Centri3`
This commit is contained in:
commit
797d50dfe6
4 changed files with 288 additions and 36 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::{is_expr_untyped_identity_function, is_trait_method};
|
||||
use clippy_utils::{is_expr_identity_function, is_expr_untyped_identity_function, is_trait_method};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir as hir;
|
||||
use rustc_lint::LateContext;
|
||||
|
|
@ -7,8 +7,20 @@ use rustc_span::{sym, Span};
|
|||
|
||||
use super::FILTER_MAP_IDENTITY;
|
||||
|
||||
fn is_identity(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<Applicability> {
|
||||
if is_expr_untyped_identity_function(cx, expr) {
|
||||
return Some(Applicability::MachineApplicable);
|
||||
}
|
||||
if is_expr_identity_function(cx, expr) {
|
||||
return Some(Applicability::Unspecified);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
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) && is_expr_untyped_identity_function(cx, filter_map_arg) {
|
||||
if is_trait_method(cx, expr, sym::Iterator)
|
||||
&& let Some(applicability) = is_identity(cx, filter_map_arg)
|
||||
{
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
FILTER_MAP_IDENTITY,
|
||||
|
|
@ -16,7 +28,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, filter_map_arg:
|
|||
"use of `filter_map` with an identity function",
|
||||
"try",
|
||||
"flatten()".to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
applicability,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue