-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use clippy_utils::{ | ||
diagnostics::span_lint_and_sugg, is_from_proc_macro, match_def_path, paths::BOOL_THEN, peel_blocks, | ||
source::snippet_opt, | ||
}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_span::Span; | ||
|
||
use super::FILTER_MAP_BOOL_THEN; | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &Expr<'_>, call_span: Span) { | ||
if !in_external_macro(cx.sess(), expr.span) | ||
&& let ExprKind::Closure(closure) = arg.kind | ||
&& let body = peel_blocks(cx.tcx.hir().body(closure.body).value) | ||
&& let ExprKind::MethodCall(_, recv, [then_arg], _) = body.kind | ||
&& let ExprKind::Closure(then_closure) = then_arg.kind | ||
&& let then_body = peel_blocks(cx.tcx.hir().body(then_closure.body).value) | ||
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(body.hir_id) | ||
&& match_def_path(cx, def_id, &BOOL_THEN) | ||
&& !is_from_proc_macro(cx, expr) | ||
&& let Some(decl_snippet) = closure.fn_arg_span.and_then(|s| snippet_opt(cx, s)) | ||
// NOTE: This will include the `()` (parenthesis) around it. Maybe there's some utils method | ||
// to remove them? `unused_parens` will already take care of this but it may be nice. | ||
&& let Some(filter) = snippet_opt(cx, recv.span) | ||
&& let Some(map) = snippet_opt(cx, then_body.span) | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
FILTER_MAP_BOOL_THEN, | ||
call_span, | ||
"usage of `bool::then` in `filter_map`", | ||
"use `filter` and `map` instead", | ||
format!("filter({decl_snippet} {filter}).map({decl_snippet} {map})"), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs:proc-macro | ||
#![allow(clippy::clone_on_copy, clippy::unnecessary_lazy_evaluations, unused)] | ||
#![warn(clippy::filter_map_bool_then)] | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
fn main() { | ||
let v = vec![1, 2, 3, 4, 5, 6]; | ||
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1)); | ||
v.clone() | ||
.into_iter() | ||
.filter_map(|i| -> Option<_> { (i % 2 == 0).then(|| i + 1) }); | ||
v.clone() | ||
.into_iter() | ||
.filter(|&i| i != 1000) | ||
.filter_map(|i| (i % 2 == 0).then(|| i + 1)); | ||
v.iter() | ||
.copied() | ||
.filter(|&i| i != 1000) | ||
.filter_map(|i| (i.clone() % 2 == 0).then(|| i + 1)); | ||
// Do not lint | ||
external! { | ||
let v = vec![1, 2, 3, 4, 5, 6]; | ||
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1)); | ||
} | ||
with_span! { | ||
span | ||
let v = vec![1, 2, 3, 4, 5, 6]; | ||
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs:proc-macro | ||
#![allow(clippy::clone_on_copy, clippy::unnecessary_lazy_evaluations, unused)] | ||
#![warn(clippy::filter_map_bool_then)] | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
fn main() { | ||
let v = vec![1, 2, 3, 4, 5, 6]; | ||
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1)); | ||
v.clone() | ||
.into_iter() | ||
.filter_map(|i| -> Option<_> { (i % 2 == 0).then(|| i + 1) }); | ||
v.clone() | ||
.into_iter() | ||
.filter(|&i| i != 1000) | ||
.filter_map(|i| (i % 2 == 0).then(|| i + 1)); | ||
v.iter() | ||
.copied() | ||
.filter(|&i| i != 1000) | ||
.filter_map(|i| (i.clone() % 2 == 0).then(|| i + 1)); | ||
// Do not lint | ||
external! { | ||
let v = vec![1, 2, 3, 4, 5, 6]; | ||
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1)); | ||
} | ||
with_span! { | ||
span | ||
let v = vec![1, 2, 3, 4, 5, 6]; | ||
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1)); | ||
} | ||
} |