-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New lint [filter_map_bool_then
]
#11115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,45 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::paths::BOOL_THEN; | ||
use clippy_utils::source::snippet_opt; | ||
use clippy_utils::ty::is_copy; | ||
use clippy_utils::{is_from_proc_macro, is_trait_method, match_def_path, peel_blocks}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_span::{sym, 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) | ||
&& is_trait_method(cx, expr, sym::Iterator) | ||
&& let ExprKind::Closure(closure) = arg.kind | ||
&& let body = cx.tcx.hir().body(closure.body) | ||
&& let value = peel_blocks(body.value) | ||
// Indexing should be fine as `filter_map` always has 1 input, we unfortunately need both | ||
// `inputs` and `params` here as we need both the type and the span | ||
&& let param_ty = closure.fn_decl.inputs[0] | ||
&& let param = body.params[0] | ||
&& is_copy(cx, cx.typeck_results().node_type(param_ty.hir_id).peel_refs()) | ||
&& let ExprKind::MethodCall(_, recv, [then_arg], _) = value.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(value.hir_id) | ||
&& match_def_path(cx, def_id, &BOOL_THEN) | ||
&& !is_from_proc_macro(cx, expr) | ||
&& 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) | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
FILTER_MAP_BOOL_THEN, | ||
call_span, | ||
"usage of `bool::then` in `filter_map`", | ||
"use `filter` then `map` instead", | ||
format!("filter(|&{param_snippet}| {filter}).map(|{param_snippet}| {map})"), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,44 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs:proc-macro | ||
#![allow( | ||
clippy::clone_on_copy, | ||
clippy::map_identity, | ||
clippy::unnecessary_lazy_evaluations, | ||
unused | ||
)] | ||
#![warn(clippy::filter_map_bool_then)] | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
#[derive(Clone, PartialEq)] | ||
struct NonCopy; | ||
|
||
fn main() { | ||
let v = vec![1, 2, 3, 4, 5, 6]; | ||
v.clone().iter().filter(|&i| (i % 2 == 0)).map(|i| i + 1); | ||
v.clone().into_iter().filter(|&i| (i % 2 == 0)).map(|i| i + 1); | ||
v.clone() | ||
.into_iter() | ||
.filter(|&i| (i % 2 == 0)).map(|i| i + 1); | ||
v.clone() | ||
.into_iter() | ||
.filter(|&i| i != 1000) | ||
.filter(|&i| (i % 2 == 0)).map(|i| i + 1); | ||
v.iter() | ||
.copied() | ||
.filter(|&i| i != 1000) | ||
.filter(|&i| (i.clone() % 2 == 0)).map(|i| i + 1); | ||
// Do not lint | ||
let v = vec![NonCopy, NonCopy]; | ||
v.clone().iter().filter_map(|i| (i == &NonCopy).then(|| i)); | ||
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 hidden or 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,44 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs:proc-macro | ||
#![allow( | ||
clippy::clone_on_copy, | ||
clippy::map_identity, | ||
clippy::unnecessary_lazy_evaluations, | ||
unused | ||
)] | ||
#![warn(clippy::filter_map_bool_then)] | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
#[derive(Clone, PartialEq)] | ||
struct NonCopy; | ||
|
||
fn main() { | ||
let v = vec![1, 2, 3, 4, 5, 6]; | ||
v.clone().iter().filter_map(|i| (i % 2 == 0).then(|| i + 1)); | ||
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 | ||
let v = vec![NonCopy, NonCopy]; | ||
v.clone().iter().filter_map(|i| (i == &NonCopy).then(|| i)); | ||
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 hidden or 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,34 @@ | ||
error: usage of `bool::then` in `filter_map` | ||
--> $DIR/filter_map_bool_then.rs:19:22 | ||
| | ||
LL | v.clone().iter().filter_map(|i| (i % 2 == 0).then(|| i + 1)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)` | ||
| | ||
= note: `-D clippy::filter-map-bool-then` implied by `-D warnings` | ||
|
||
error: usage of `bool::then` in `filter_map` | ||
--> $DIR/filter_map_bool_then.rs:20:27 | ||
| | ||
LL | v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)` | ||
|
||
error: usage of `bool::then` in `filter_map` | ||
--> $DIR/filter_map_bool_then.rs:23:10 | ||
| | ||
LL | .filter_map(|i| -> Option<_> { (i % 2 == 0).then(|| i + 1) }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)` | ||
|
||
error: usage of `bool::then` in `filter_map` | ||
--> $DIR/filter_map_bool_then.rs:27:10 | ||
| | ||
LL | .filter_map(|i| (i % 2 == 0).then(|| i + 1)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)` | ||
|
||
error: usage of `bool::then` in `filter_map` | ||
--> $DIR/filter_map_bool_then.rs:31:10 | ||
| | ||
LL | .filter_map(|i| (i.clone() % 2 == 0).then(|| i + 1)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i.clone() % 2 == 0)).map(|i| i + 1)` | ||
|
||
error: aborting due to 5 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.