Skip to content

Commit eaf640d

Browse files
committed
Auto merge of #11506 - mojave2:issue-11503, r=giraffate
fix filter_map_bool_then with a bool reference changelog: [`filter_map_bool_then`]: Fix the incorrect autofix when the `bool` in question is a reference. fix #11503
2 parents e609279 + c9b212d commit eaf640d

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

clippy_lints/src/methods/filter_map_bool_then.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_errors::Applicability;
88
use rustc_hir::{Expr, ExprKind};
99
use rustc_lint::{LateContext, LintContext};
1010
use rustc_middle::lint::in_external_macro;
11-
use rustc_middle::ty::Binder;
11+
use rustc_middle::ty::{self, Binder};
1212
use rustc_span::{sym, Span};
1313

1414
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &Expr<'_>, call_span: Span) {
@@ -36,6 +36,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &
3636
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(value.hir_id)
3737
&& match_def_path(cx, def_id, &BOOL_THEN)
3838
&& !is_from_proc_macro(cx, expr)
39+
&& let ref_bool = matches!(cx.typeck_results().expr_ty(recv).kind(), ty::Ref(..))
3940
&& let Some(param_snippet) = snippet_opt(cx, param.span)
4041
&& let Some(filter) = snippet_opt(cx, recv.span)
4142
&& let Some(map) = snippet_opt(cx, then_body.span)
@@ -46,7 +47,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &
4647
call_span,
4748
"usage of `bool::then` in `filter_map`",
4849
"use `filter` then `map` instead",
49-
format!("filter(|&{param_snippet}| {filter}).map(|{param_snippet}| {map})"),
50+
format!("filter(|&{param_snippet}| {}{filter}).map(|{param_snippet}| {map})", if ref_bool { "*" } else { "" }),
5051
Applicability::MachineApplicable,
5152
);
5253
}

tests/ui/filter_map_bool_then.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,8 @@ fn main() {
5555
fn issue11309<'a>(iter: impl Iterator<Item = (&'a str, &'a str)>) -> Vec<&'a str> {
5656
iter.filter_map(|(_, s): (&str, _)| Some(s)).collect()
5757
}
58+
59+
fn issue11503() {
60+
let bools: &[bool] = &[true, false, false, true];
61+
let _: Vec<usize> = bools.iter().enumerate().filter(|&(i, b)| *b).map(|(i, b)| i).collect();
62+
}

tests/ui/filter_map_bool_then.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,8 @@ fn main() {
5555
fn issue11309<'a>(iter: impl Iterator<Item = (&'a str, &'a str)>) -> Vec<&'a str> {
5656
iter.filter_map(|(_, s): (&str, _)| Some(s)).collect()
5757
}
58+
59+
fn issue11503() {
60+
let bools: &[bool] = &[true, false, false, true];
61+
let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
62+
}

tests/ui/filter_map_bool_then.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,11 @@ error: usage of `bool::then` in `filter_map`
3737
LL | v.clone().iter().filter_map(|i| (i == &NonCopy).then(|| i));
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i == &NonCopy)).map(|i| i)`
3939

40-
error: aborting due to 6 previous errors
40+
error: usage of `bool::then` in `filter_map`
41+
--> $DIR/filter_map_bool_then.rs:61:50
42+
|
43+
LL | let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&(i, b)| *b).map(|(i, b)| i)`
45+
46+
error: aborting due to 7 previous errors
4147

0 commit comments

Comments
 (0)