mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 23:24:24 +00:00
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
This commit is contained in:
commit
eaf640dcf0
4 changed files with 20 additions and 3 deletions
|
@ -8,7 +8,7 @@ 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::Binder;
|
||||
use rustc_middle::ty::{self, Binder};
|
||||
use rustc_span::{sym, Span};
|
||||
|
||||
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: &
|
|||
&& 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(..))
|
||||
&& 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)
|
||||
|
@ -46,7 +47,7 @@ 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})"),
|
||||
format!("filter(|&{param_snippet}| {}{filter}).map(|{param_snippet}| {map})", if ref_bool { "*" } else { "" }),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -55,3 +55,8 @@ fn main() {
|
|||
fn issue11309<'a>(iter: impl Iterator<Item = (&'a str, &'a str)>) -> Vec<&'a str> {
|
||||
iter.filter_map(|(_, s): (&str, _)| Some(s)).collect()
|
||||
}
|
||||
|
||||
fn issue11503() {
|
||||
let bools: &[bool] = &[true, false, false, true];
|
||||
let _: Vec<usize> = bools.iter().enumerate().filter(|&(i, b)| *b).map(|(i, b)| i).collect();
|
||||
}
|
||||
|
|
|
@ -55,3 +55,8 @@ fn main() {
|
|||
fn issue11309<'a>(iter: impl Iterator<Item = (&'a str, &'a str)>) -> Vec<&'a str> {
|
||||
iter.filter_map(|(_, s): (&str, _)| Some(s)).collect()
|
||||
}
|
||||
|
||||
fn issue11503() {
|
||||
let bools: &[bool] = &[true, false, false, true];
|
||||
let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
|
||||
}
|
||||
|
|
|
@ -37,5 +37,11 @@ error: usage of `bool::then` in `filter_map`
|
|||
LL | v.clone().iter().filter_map(|i| (i == &NonCopy).then(|| i));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i == &NonCopy)).map(|i| i)`
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
error: usage of `bool::then` in `filter_map`
|
||||
--> $DIR/filter_map_bool_then.rs:61: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 7 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue