1+ use clippy_config:: Conf ;
12use clippy_utils:: diagnostics:: span_lint_and_then;
3+ use clippy_utils:: msrvs:: { self , Msrv } ;
24use clippy_utils:: ty:: is_type_diagnostic_item;
35use clippy_utils:: { is_diag_item_method, is_trait_method, path_to_local_id} ;
46use rustc_errors:: Applicability ;
57use rustc_hir:: { Body , Closure , Expr , ExprKind } ;
68use rustc_lint:: { LateContext , LateLintPass } ;
7- use rustc_session:: declare_lint_pass ;
9+ use rustc_session:: impl_lint_pass ;
810use rustc_span:: sym;
911
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+
1024declare_clippy_lint ! {
1125 /// ### What it does
1226 /// Checks for usage of `lines.filter_map(Result::ok)` or `lines.flat_map(Result::ok)`
@@ -55,11 +69,13 @@ declare_clippy_lint! {
5569 suspicious,
5670 "filtering `std::io::Lines` with `filter_map()`, `flat_map()`, or `flatten()` might cause an infinite loop"
5771}
58- declare_lint_pass ! ( LinesFilterMapOk => [ LINES_FILTER_MAP_OK ] ) ;
72+
73+ impl_lint_pass ! ( LinesFilterMapOk => [ LINES_FILTER_MAP_OK ] ) ;
5974
6075impl LateLintPass < ' _ > for LinesFilterMapOk {
6176 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
6379 && is_trait_method ( cx, expr, sym:: Iterator )
6480 && let fm_method_str = fm_method. ident . as_str ( )
6581 && matches ! ( fm_method_str, "filter_map" | "flat_map" | "flatten" )
@@ -85,6 +101,8 @@ impl LateLintPass<'_> for LinesFilterMapOk {
85101 ) ;
86102 }
87103 }
104+
105+ extract_msrv_attr ! ( LateContext ) ;
88106}
89107
90108fn should_lint ( cx : & LateContext < ' _ > , args : & [ Expr < ' _ > ] , method_str : & str ) -> bool {
0 commit comments