Skip to content

Commit 7b5e019

Browse files
committed
Auto merge of #11515 - y21:filter_map_bool_then_peel_refs, r=Jarcho
[`filter_map_bool_then`]: include multiple derefs from adjustments In #11506 this lint was improved to suggest one deref if the bool is behind references (fixed the FP #11503), however it might need multiple dereferences if the bool is behind multiple layers of references or custom derefs. E.g. `&&&bool` needs `***b`. changelog: [`filter_map_bool_then`]: suggest as many dereferences as there are needed to get to the bool
2 parents ef73648 + 2ec6f3b commit 7b5e019

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

clippy_lints/src/methods/filter_map_bool_then.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ 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::{self, Binder};
11+
use rustc_middle::ty::adjustment::Adjust;
12+
use rustc_middle::ty::Binder;
1213
use rustc_span::{sym, Span};
1314

1415
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &Expr<'_>, call_span: Span) {
@@ -36,7 +37,11 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &
3637
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(value.hir_id)
3738
&& match_def_path(cx, def_id, &BOOL_THEN)
3839
&& !is_from_proc_macro(cx, expr)
39-
&& let ref_bool = matches!(cx.typeck_results().expr_ty(recv).kind(), ty::Ref(..))
40+
// Count the number of derefs needed to get to the bool because we need those in the suggestion
41+
&& let needed_derefs = cx.typeck_results().expr_adjustments(recv)
42+
.iter()
43+
.filter(|adj| matches!(adj.kind, Adjust::Deref(_)))
44+
.count()
4045
&& let Some(param_snippet) = snippet_opt(cx, param.span)
4146
&& let Some(filter) = snippet_opt(cx, recv.span)
4247
&& let Some(map) = snippet_opt(cx, then_body.span)
@@ -47,7 +52,10 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &
4752
call_span,
4853
"usage of `bool::then` in `filter_map`",
4954
"use `filter` then `map` instead",
50-
format!("filter(|&{param_snippet}| {}{filter}).map(|{param_snippet}| {map})", if ref_bool { "*" } else { "" }),
55+
format!(
56+
"filter(|&{param_snippet}| {derefs}{filter}).map(|{param_snippet}| {map})",
57+
derefs="*".repeat(needed_derefs)
58+
),
5159
Applicability::MachineApplicable,
5260
);
5361
}

tests/ui/filter_map_bool_then.fixed

+19
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,23 @@ fn issue11309<'a>(iter: impl Iterator<Item = (&'a str, &'a str)>) -> Vec<&'a str
5959
fn issue11503() {
6060
let bools: &[bool] = &[true, false, false, true];
6161
let _: Vec<usize> = bools.iter().enumerate().filter(|&(i, b)| *b).map(|(i, b)| i).collect();
62+
63+
// Need to insert multiple derefs if there is more than one layer of references
64+
let bools: &[&&bool] = &[&&true, &&false, &&false, &&true];
65+
let _: Vec<usize> = bools.iter().enumerate().filter(|&(i, b)| ***b).map(|(i, b)| i).collect();
66+
67+
// Should also suggest derefs when going through a mutable reference
68+
let bools: &[&mut bool] = &[&mut true];
69+
let _: Vec<usize> = bools.iter().enumerate().filter(|&(i, b)| **b).map(|(i, b)| i).collect();
70+
71+
// Should also suggest derefs when going through a custom deref
72+
struct DerefToBool;
73+
impl std::ops::Deref for DerefToBool {
74+
type Target = bool;
75+
fn deref(&self) -> &Self::Target {
76+
&true
77+
}
78+
}
79+
let bools: &[&&DerefToBool] = &[&&DerefToBool];
80+
let _: Vec<usize> = bools.iter().enumerate().filter(|&(i, b)| ****b).map(|(i, b)| i).collect();
6281
}

tests/ui/filter_map_bool_then.rs

+19
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,23 @@ fn issue11309<'a>(iter: impl Iterator<Item = (&'a str, &'a str)>) -> Vec<&'a str
5959
fn issue11503() {
6060
let bools: &[bool] = &[true, false, false, true];
6161
let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
62+
63+
// Need to insert multiple derefs if there is more than one layer of references
64+
let bools: &[&&bool] = &[&&true, &&false, &&false, &&true];
65+
let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
66+
67+
// Should also suggest derefs when going through a mutable reference
68+
let bools: &[&mut bool] = &[&mut true];
69+
let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
70+
71+
// Should also suggest derefs when going through a custom deref
72+
struct DerefToBool;
73+
impl std::ops::Deref for DerefToBool {
74+
type Target = bool;
75+
fn deref(&self) -> &Self::Target {
76+
&true
77+
}
78+
}
79+
let bools: &[&&DerefToBool] = &[&&DerefToBool];
80+
let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
6281
}

tests/ui/filter_map_bool_then.stderr

+19-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,23 @@ error: usage of `bool::then` in `filter_map`
4343
LL | let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&(i, b)| *b).map(|(i, b)| i)`
4545

46-
error: aborting due to 7 previous errors
46+
error: usage of `bool::then` in `filter_map`
47+
--> $DIR/filter_map_bool_then.rs:65:50
48+
|
49+
LL | let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&(i, b)| ***b).map(|(i, b)| i)`
51+
52+
error: usage of `bool::then` in `filter_map`
53+
--> $DIR/filter_map_bool_then.rs:69:50
54+
|
55+
LL | let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
56+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&(i, b)| **b).map(|(i, b)| i)`
57+
58+
error: usage of `bool::then` in `filter_map`
59+
--> $DIR/filter_map_bool_then.rs:80:50
60+
|
61+
LL | let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&(i, b)| ****b).map(|(i, b)| i)`
63+
64+
error: aborting due to 10 previous errors
4765

0 commit comments

Comments
 (0)