|
1 | | -use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg}; |
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
2 | 2 | use clippy_utils::msrvs::{self, Msrv}; |
3 | 3 | use clippy_utils::res::{MaybeDef, MaybeTypeckRes}; |
4 | | -use clippy_utils::source::snippet; |
| 4 | +use clippy_utils::source::snippet_with_applicability; |
5 | 5 | use rustc_errors::Applicability; |
6 | | -use rustc_hir as hir; |
| 6 | +use rustc_hir::Expr; |
7 | 7 | use rustc_lint::LateContext; |
8 | 8 | use rustc_span::sym; |
9 | 9 |
|
10 | 10 | use super::FILTER_MAP_NEXT; |
11 | 11 |
|
12 | | -pub(super) fn check<'tcx>( |
13 | | - cx: &LateContext<'tcx>, |
14 | | - expr: &'tcx hir::Expr<'_>, |
15 | | - recv: &'tcx hir::Expr<'_>, |
16 | | - arg: &'tcx hir::Expr<'_>, |
17 | | - msrv: Msrv, |
18 | | -) { |
19 | | - if cx.ty_based_def(expr).opt_parent(cx).is_diag_item(cx, sym::Iterator) { |
20 | | - if !msrv.meets(cx, msrvs::ITERATOR_FIND_MAP) { |
21 | | - return; |
22 | | - } |
23 | | - |
24 | | - let msg = "called `filter_map(..).next()` on an `Iterator`. This is more succinctly expressed by calling \ |
25 | | - `.find_map(..)` instead"; |
26 | | - let filter_snippet = snippet(cx, arg.span, ".."); |
27 | | - if filter_snippet.lines().count() <= 1 { |
28 | | - let iter_snippet = snippet(cx, recv.span, ".."); |
29 | | - span_lint_and_sugg( |
30 | | - cx, |
31 | | - FILTER_MAP_NEXT, |
32 | | - expr.span, |
33 | | - msg, |
34 | | - "try", |
35 | | - format!("{iter_snippet}.find_map({filter_snippet})"), |
36 | | - Applicability::MachineApplicable, |
37 | | - ); |
38 | | - } else { |
39 | | - span_lint(cx, FILTER_MAP_NEXT, expr.span, msg); |
40 | | - } |
| 12 | +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, arg: &Expr<'_>, msrv: Msrv) { |
| 13 | + if cx.ty_based_def(expr).opt_parent(cx).is_diag_item(cx, sym::Iterator) && msrv.meets(cx, msrvs::ITERATOR_FIND_MAP) |
| 14 | + { |
| 15 | + span_lint_and_then( |
| 16 | + cx, |
| 17 | + FILTER_MAP_NEXT, |
| 18 | + expr.span, |
| 19 | + "called `filter_map(..).next()` on an `Iterator`", |
| 20 | + |diag| { |
| 21 | + let mut app = Applicability::MachineApplicable; |
| 22 | + let iter_snippet = snippet_with_applicability(cx, recv.span, "_", &mut app); |
| 23 | + let filter_snippet = snippet_with_applicability(cx, arg.span, "..", &mut app); |
| 24 | + diag.span_suggestion_verbose( |
| 25 | + expr.span, |
| 26 | + "use `.find_map(..)` instead", |
| 27 | + format!("{iter_snippet}.find_map({filter_snippet})"), |
| 28 | + app, |
| 29 | + ); |
| 30 | + }, |
| 31 | + ); |
41 | 32 | } |
42 | 33 | } |
0 commit comments