|
1 | | -use rustc_lint::LateContext; |
| 1 | +use rustc_lint::{LateContext, LintContext}; |
2 | 2 |
|
3 | 3 | use super::{ITER_FILTER_IS_OK, ITER_FILTER_IS_SOME}; |
4 | 4 |
|
5 | 5 | use clippy_utils::diagnostics::span_lint_and_sugg; |
6 | 6 | use clippy_utils::source::{indent_of, reindent_multiline}; |
7 | | -use clippy_utils::{is_trait_method, peel_blocks}; |
| 7 | +use clippy_utils::{is_trait_method, peel_blocks, span_contains_comment}; |
8 | 8 | use rustc_errors::Applicability; |
9 | 9 | use rustc_hir as hir; |
10 | 10 | use rustc_hir::def::Res; |
@@ -54,26 +54,40 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, filter_arg: &hir |
54 | 54 | let is_iterator = is_trait_method(cx, expr, sym::Iterator); |
55 | 55 | let parent_is_not_map = !parent_is_map(cx, expr); |
56 | 56 |
|
57 | | - if is_iterator && parent_is_not_map && is_method(cx, filter_arg, sym!(is_some)) { |
| 57 | + if is_iterator |
| 58 | + && parent_is_not_map |
| 59 | + && is_method(cx, filter_arg, sym!(is_some)) |
| 60 | + && !span_contains_comment( |
| 61 | + cx.sess().source_map(), |
| 62 | + filter_span.with_hi(expr.span.hi()) |
| 63 | + ) |
| 64 | + { |
58 | 65 | span_lint_and_sugg( |
59 | 66 | cx, |
60 | 67 | ITER_FILTER_IS_SOME, |
61 | 68 | filter_span.with_hi(expr.span.hi()), |
62 | 69 | "`filter` for `is_some` on iterator over `Option`", |
63 | 70 | "consider using `flatten` instead", |
64 | 71 | reindent_multiline(Cow::Borrowed("flatten()"), true, indent_of(cx, filter_span)).into_owned(), |
65 | | - Applicability::MaybeIncorrect, |
| 72 | + Applicability::HasPlaceholders, |
66 | 73 | ); |
67 | 74 | } |
68 | | - if is_iterator && parent_is_not_map && is_method(cx, filter_arg, sym!(is_ok)) { |
| 75 | + if is_iterator |
| 76 | + && parent_is_not_map |
| 77 | + && is_method(cx, filter_arg, sym!(is_ok)) |
| 78 | + && !span_contains_comment( |
| 79 | + cx.sess().source_map(), |
| 80 | + filter_span.with_hi(expr.span.hi()) |
| 81 | + ) |
| 82 | + { |
69 | 83 | span_lint_and_sugg( |
70 | 84 | cx, |
71 | 85 | ITER_FILTER_IS_OK, |
72 | 86 | filter_span.with_hi(expr.span.hi()), |
73 | 87 | "`filter` for `is_ok` on iterator over `Result`s", |
74 | 88 | "consider using `flatten` instead", |
75 | 89 | reindent_multiline(Cow::Borrowed("flatten()"), true, indent_of(cx, filter_span)).into_owned(), |
76 | | - Applicability::MaybeIncorrect, |
| 90 | + Applicability::HasPlaceholders, |
77 | 91 | ); |
78 | 92 | } |
79 | 93 | } |
0 commit comments