Skip to content

Commit b0ad06d

Browse files
authored
add MSRV check for lines_filter_map_ok (#14130)
fixes #14127 changelog: [`lines_filter_map_ok`]: respect MSRV
2 parents c529b70 + f9669e4 commit b0ad06d

File tree

7 files changed

+39
-4
lines changed

7 files changed

+39
-4
lines changed

book/src/lint_configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ The minimum rust version that the project supports. Defaults to the `rust-versio
740740
* [`index_refutable_slice`](https://rust-lang.github.io/rust-clippy/master/index.html#index_refutable_slice)
741741
* [`iter_kv_map`](https://rust-lang.github.io/rust-clippy/master/index.html#iter_kv_map)
742742
* [`legacy_numeric_constants`](https://rust-lang.github.io/rust-clippy/master/index.html#legacy_numeric_constants)
743+
* [`lines_filter_map_ok`](https://rust-lang.github.io/rust-clippy/master/index.html#lines_filter_map_ok)
743744
* [`manual_bits`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_bits)
744745
* [`manual_c_str_literals`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_c_str_literals)
745746
* [`manual_clamp`](https://rust-lang.github.io/rust-clippy/master/index.html#manual_clamp)

clippy_config/src/conf.rs

+1
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ define_Conf! {
609609
index_refutable_slice,
610610
iter_kv_map,
611611
legacy_numeric_constants,
612+
lines_filter_map_ok,
612613
manual_bits,
613614
manual_c_str_literals,
614615
manual_clamp,

clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
901901
store.register_late_pass(move |_| Box::new(manual_main_separator_str::ManualMainSeparatorStr::new(conf)));
902902
store.register_late_pass(|_| Box::new(unnecessary_struct_initialization::UnnecessaryStruct));
903903
store.register_late_pass(move |_| Box::new(unnecessary_box_returns::UnnecessaryBoxReturns::new(conf)));
904-
store.register_late_pass(|_| Box::new(lines_filter_map_ok::LinesFilterMapOk));
904+
store.register_late_pass(move |_| Box::new(lines_filter_map_ok::LinesFilterMapOk::new(conf)));
905905
store.register_late_pass(|_| Box::new(tests_outside_test_module::TestsOutsideTestModule));
906906
store.register_late_pass(|_| Box::new(manual_slice_size_calculation::ManualSliceSizeCalculation));
907907
store.register_early_pass(move || Box::new(excessive_nesting::ExcessiveNesting::new(conf)));

clippy_lints/src/lines_filter_map_ok.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1+
use clippy_config::Conf;
12
use clippy_utils::diagnostics::span_lint_and_then;
3+
use clippy_utils::msrvs::{self, Msrv};
24
use clippy_utils::ty::is_type_diagnostic_item;
35
use clippy_utils::{is_diag_item_method, is_trait_method, path_to_local_id};
46
use rustc_errors::Applicability;
57
use rustc_hir::{Body, Closure, Expr, ExprKind};
68
use rustc_lint::{LateContext, LateLintPass};
7-
use rustc_session::declare_lint_pass;
9+
use rustc_session::impl_lint_pass;
810
use 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+
1024
declare_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

6075
impl 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

90108
fn should_lint(cx: &LateContext<'_>, args: &[Expr<'_>], method_str: &str) -> bool {

clippy_utils/src/msrvs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ msrv_aliases! {
3737
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE, CONST_EXTERN_C_FN }
3838
1,59,0 { THREAD_LOCAL_CONST_INIT }
3939
1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY, CONST_RAW_PTR_DEREF }
40+
1,57,0 { MAP_WHILE }
4041
1,56,0 { CONST_FN_UNION }
4142
1,55,0 { SEEK_REWIND }
4243
1,54,0 { INTO_KEYS }

tests/ui/lines_filter_map_ok.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ fn main() -> io::Result<()> {
3131
io::stdin().lines().filter_map(|x| x.err()).for_each(|_| ());
3232
Ok(())
3333
}
34+
35+
#[clippy::msrv = "1.56"]
36+
fn msrv_check() {
37+
let _lines = BufReader::new(std::fs::File::open("some-path").unwrap())
38+
.lines()
39+
.filter_map(Result::ok);
40+
}

tests/ui/lines_filter_map_ok.rs

+7
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ fn main() -> io::Result<()> {
3131
io::stdin().lines().filter_map(|x| x.err()).for_each(|_| ());
3232
Ok(())
3333
}
34+
35+
#[clippy::msrv = "1.56"]
36+
fn msrv_check() {
37+
let _lines = BufReader::new(std::fs::File::open("some-path").unwrap())
38+
.lines()
39+
.filter_map(Result::ok);
40+
}

0 commit comments

Comments
 (0)