diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2025-01-04 12:54:19 +0100 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2025-01-06 17:35:55 +0100 |
| commit | 891e38788a36c3d217e4e06d52e4ee67797b8ccb (patch) | |
| tree | 819b8a032964df232d15ece2a9e09f0ef9ee38ef | |
| parent | b57d98b00ec53db774cf18e3d9d6fb28e8f60d0f (diff) | |
| download | rust-891e38788a36c3d217e4e06d52e4ee67797b8ccb.tar.gz rust-891e38788a36c3d217e4e06d52e4ee67797b8ccb.zip | |
Don't emit machine applicable `map_flatten` lint if there are code comments
| -rw-r--r-- | clippy_lints/src/methods/map_flatten.rs | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/clippy_lints/src/methods/map_flatten.rs b/clippy_lints/src/methods/map_flatten.rs index 07a7a12b162..f7bb8c1d696 100644 --- a/clippy_lints/src/methods/map_flatten.rs +++ b/clippy_lints/src/methods/map_flatten.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::is_trait_method; use clippy_utils::source::snippet_with_applicability; use clippy_utils::ty::is_type_diagnostic_item; +use clippy_utils::{is_trait_method, span_contains_comment}; use rustc_errors::Applicability; use rustc_hir::Expr; use rustc_lint::LateContext; @@ -17,10 +17,15 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, map_ let mut applicability = Applicability::MachineApplicable; let closure_snippet = snippet_with_applicability(cx, map_arg.span, "..", &mut applicability); + let span = expr.span.with_lo(map_span.lo()); + // If the methods are separated with comments, we don't apply suggestion automatically. + if span_contains_comment(cx.tcx.sess.source_map(), span) { + applicability = Applicability::Unspecified; + } span_lint_and_sugg( cx, MAP_FLATTEN, - expr.span.with_lo(map_span.lo()), + span, format!("called `map(..).flatten()` on `{caller_ty_name}`"), format!("try replacing `map` with `{method_to_use}` and remove the `.flatten()`"), format!("{method_to_use}({closure_snippet})"), |
