Skip to content

Commit 2ec6f3b

Browse files
committed
also count derefs through custom Deref impls
1 parent 860e800 commit 2ec6f3b

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

clippy_lints/src/methods/filter_map_bool_then.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use super::FILTER_MAP_BOOL_THEN;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::paths::BOOL_THEN;
44
use clippy_utils::source::snippet_opt;
5-
use clippy_utils::ty::{is_copy, peel_mid_ty_refs_is_mutable};
5+
use clippy_utils::ty::is_copy;
66
use clippy_utils::{is_from_proc_macro, is_trait_method, match_def_path, peel_blocks};
77
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::adjustment::Adjust;
1112
use rustc_middle::ty::Binder;
1213
use rustc_span::{sym, Span};
1314

@@ -36,11 +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-
// Peel all refs (e.g. `&&&&mut &&&bool` -> `bool`) and get its count so we can suggest the exact
40-
// amount of derefs to get to the bool in the filter.
41-
// `peel_mid_ty_refs` alone doesn't handle mutable reference, so we use `_is_mutable`
42-
// instead which counts them too and just ignore the resulting mutability
43-
&& let (_, needed_derefs, _) = peel_mid_ty_refs_is_mutable(cx.typeck_results().expr_ty(recv))
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()
4445
&& let Some(param_snippet) = snippet_opt(cx, param.span)
4546
&& let Some(filter) = snippet_opt(cx, recv.span)
4647
&& let Some(map) = snippet_opt(cx, then_body.span)

tests/ui/filter_map_bool_then.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,15 @@ fn issue11503() {
6767
// Should also suggest derefs when going through a mutable reference
6868
let bools: &[&mut bool] = &[&mut true];
6969
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();
7081
}

tests/ui/filter_map_bool_then.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,15 @@ fn issue11503() {
6767
// Should also suggest derefs when going through a mutable reference
6868
let bools: &[&mut bool] = &[&mut true];
6969
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();
7081
}

tests/ui/filter_map_bool_then.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,11 @@ error: usage of `bool::then` in `filter_map`
5555
LL | let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
5656
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&(i, b)| **b).map(|(i, b)| i)`
5757

58-
error: aborting due to 9 previous errors
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
5965

0 commit comments

Comments
 (0)