1
+ use clippy_config:: Conf ;
1
2
use clippy_utils:: diagnostics:: span_lint_and_then;
3
+ use clippy_utils:: msrvs:: { self , Msrv } ;
2
4
use clippy_utils:: ty:: is_type_diagnostic_item;
3
5
use clippy_utils:: { is_diag_item_method, is_trait_method, path_to_local_id} ;
4
6
use rustc_errors:: Applicability ;
5
7
use rustc_hir:: { Body , Closure , Expr , ExprKind } ;
6
8
use rustc_lint:: { LateContext , LateLintPass } ;
7
- use rustc_session:: declare_lint_pass ;
9
+ use rustc_session:: impl_lint_pass ;
8
10
use rustc_span:: sym;
9
11
12
+ pub struct LinesFilterMapOk {
13
+ msrv : Msrv ,
14
+ }
15
+
16
+ impl LinesFilterMapOk {
17
+ pub fn new ( conf : & Conf ) -> Self {
18
+ Self {
19
+ msrv : conf. msrv . clone ( ) ,
20
+ }
21
+ }
22
+ }
23
+
10
24
declare_clippy_lint ! {
11
25
/// ### What it does
12
26
/// Checks for usage of `lines.filter_map(Result::ok)` or `lines.flat_map(Result::ok)`
@@ -55,11 +69,13 @@ declare_clippy_lint! {
55
69
suspicious,
56
70
"filtering `std::io::Lines` with `filter_map()`, `flat_map()`, or `flatten()` might cause an infinite loop"
57
71
}
58
- declare_lint_pass ! ( LinesFilterMapOk => [ LINES_FILTER_MAP_OK ] ) ;
72
+
73
+ impl_lint_pass ! ( LinesFilterMapOk => [ LINES_FILTER_MAP_OK ] ) ;
59
74
60
75
impl LateLintPass < ' _ > for LinesFilterMapOk {
61
76
fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
62
- if let ExprKind :: MethodCall ( fm_method, fm_receiver, fm_args, fm_span) = expr. kind
77
+ if self . msrv . meets ( msrvs:: MAP_WHILE )
78
+ && let ExprKind :: MethodCall ( fm_method, fm_receiver, fm_args, fm_span) = expr. kind
63
79
&& is_trait_method ( cx, expr, sym:: Iterator )
64
80
&& let fm_method_str = fm_method. ident . as_str ( )
65
81
&& matches ! ( fm_method_str, "filter_map" | "flat_map" | "flatten" )
@@ -85,6 +101,8 @@ impl LateLintPass<'_> for LinesFilterMapOk {
85
101
) ;
86
102
}
87
103
}
104
+
105
+ extract_msrv_attr ! ( LateContext ) ;
88
106
}
89
107
90
108
fn should_lint ( cx : & LateContext < ' _ > , args : & [ Expr < ' _ > ] , method_str : & str ) -> bool {
0 commit comments