Skip to content

Commit

Permalink
Auto merge of #11515 - y21:filter_map_bool_then_peel_refs, r=Jarcho
Browse files Browse the repository at this point in the history
[`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
  • Loading branch information
bors committed Sep 17, 2023
2 parents ef73648 + 2ec6f3b commit 7b5e019
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
14 changes: 11 additions & 3 deletions clippy_lints/src/methods/filter_map_bool_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::{self, Binder};
use rustc_middle::ty::adjustment::Adjust;
use rustc_middle::ty::Binder;
use rustc_span::{sym, Span};

pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &Expr<'_>, call_span: Span) {
Expand Down Expand Up @@ -36,7 +37,11 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(value.hir_id)
&& match_def_path(cx, def_id, &BOOL_THEN)
&& !is_from_proc_macro(cx, expr)
&& let ref_bool = matches!(cx.typeck_results().expr_ty(recv).kind(), ty::Ref(..))
// Count the number of derefs needed to get to the bool because we need those in the suggestion
&& let needed_derefs = cx.typeck_results().expr_adjustments(recv)
.iter()
.filter(|adj| matches!(adj.kind, Adjust::Deref(_)))
.count()
&& let Some(param_snippet) = snippet_opt(cx, param.span)
&& let Some(filter) = snippet_opt(cx, recv.span)
&& let Some(map) = snippet_opt(cx, then_body.span)
Expand All @@ -47,7 +52,10 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &
call_span,
"usage of `bool::then` in `filter_map`",
"use `filter` then `map` instead",
format!("filter(|&{param_snippet}| {}{filter}).map(|{param_snippet}| {map})", if ref_bool { "*" } else { "" }),
format!(
"filter(|&{param_snippet}| {derefs}{filter}).map(|{param_snippet}| {map})",
derefs="*".repeat(needed_derefs)
),
Applicability::MachineApplicable,
);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/filter_map_bool_then.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,23 @@ fn issue11309<'a>(iter: impl Iterator<Item = (&'a str, &'a str)>) -> Vec<&'a str
fn issue11503() {
let bools: &[bool] = &[true, false, false, true];
let _: Vec<usize> = bools.iter().enumerate().filter(|&(i, b)| *b).map(|(i, b)| i).collect();

// Need to insert multiple derefs if there is more than one layer of references
let bools: &[&&bool] = &[&&true, &&false, &&false, &&true];
let _: Vec<usize> = bools.iter().enumerate().filter(|&(i, b)| ***b).map(|(i, b)| i).collect();

// Should also suggest derefs when going through a mutable reference
let bools: &[&mut bool] = &[&mut true];
let _: Vec<usize> = bools.iter().enumerate().filter(|&(i, b)| **b).map(|(i, b)| i).collect();

// Should also suggest derefs when going through a custom deref
struct DerefToBool;
impl std::ops::Deref for DerefToBool {
type Target = bool;
fn deref(&self) -> &Self::Target {
&true
}
}
let bools: &[&&DerefToBool] = &[&&DerefToBool];
let _: Vec<usize> = bools.iter().enumerate().filter(|&(i, b)| ****b).map(|(i, b)| i).collect();
}
19 changes: 19 additions & 0 deletions tests/ui/filter_map_bool_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,23 @@ fn issue11309<'a>(iter: impl Iterator<Item = (&'a str, &'a str)>) -> Vec<&'a str
fn issue11503() {
let bools: &[bool] = &[true, false, false, true];
let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();

// Need to insert multiple derefs if there is more than one layer of references
let bools: &[&&bool] = &[&&true, &&false, &&false, &&true];
let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();

// Should also suggest derefs when going through a mutable reference
let bools: &[&mut bool] = &[&mut true];
let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();

// Should also suggest derefs when going through a custom deref
struct DerefToBool;
impl std::ops::Deref for DerefToBool {
type Target = bool;
fn deref(&self) -> &Self::Target {
&true
}
}
let bools: &[&&DerefToBool] = &[&&DerefToBool];
let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
}
20 changes: 19 additions & 1 deletion tests/ui/filter_map_bool_then.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,23 @@ error: usage of `bool::then` in `filter_map`
LL | let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&(i, b)| *b).map(|(i, b)| i)`

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

error: usage of `bool::then` in `filter_map`
--> $DIR/filter_map_bool_then.rs:69:50
|
LL | let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&(i, b)| **b).map(|(i, b)| i)`

error: usage of `bool::then` in `filter_map`
--> $DIR/filter_map_bool_then.rs:80:50
|
LL | let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&(i, b)| ****b).map(|(i, b)| i)`

error: aborting due to 10 previous errors

0 comments on commit 7b5e019

Please sign in to comment.