Skip to content

Commit

Permalink
Only lint Copy types
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jul 25, 2023
1 parent 978b1da commit 3235d9d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 25 deletions.
24 changes: 15 additions & 9 deletions clippy_lints/src/methods/filter_map_bool_then.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::paths::BOOL_THEN;
use clippy_utils::source::snippet_opt;
use clippy_utils::{is_from_proc_macro, match_def_path, peel_blocks};
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::Span;
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 = peel_blocks(cx.tcx.hir().body(closure.body).value)
&& let ExprKind::MethodCall(_, recv, [then_arg], _) = body.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(body.hir_id)
&& 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(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(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 @@ -32,7 +38,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({decl_snippet} {filter}).map({decl_snippet} {map})"),
format!("filter(|&{param_snippet}| {filter}).map(|{param_snippet}| {map})"),
Applicability::MachineApplicable,
);
}
Expand Down
21 changes: 16 additions & 5 deletions tests/ui/filter_map_bool_then.fixed
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
//@run-rustfix
//@aux-build:proc_macros.rs:proc-macro
#![allow(clippy::clone_on_copy, clippy::unnecessary_lazy_evaluations, unused)]
#![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().into_iter().filter(|i| (i % 2 == 0)).map(|i| i + 1);
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);
.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);
.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);
.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));
Expand Down
13 changes: 12 additions & 1 deletion tests/ui/filter_map_bool_then.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
//@run-rustfix
//@aux-build:proc_macros.rs:proc-macro
#![allow(clippy::clone_on_copy, clippy::unnecessary_lazy_evaluations, unused)]
#![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()
Expand All @@ -21,6 +30,8 @@ fn main() {
.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));
Expand Down
26 changes: 16 additions & 10 deletions tests/ui/filter_map_bool_then.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
error: usage of `bool::then` in `filter_map`
--> $DIR/filter_map_bool_then.rs:11:27
--> $DIR/filter_map_bool_then.rs:19:22
|
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)`
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:14:10
--> $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)`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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:18:10
--> $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)`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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:22:10
--> $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)`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i.clone() % 2 == 0)).map(|i| i + 1)`

error: aborting due to 4 previous errors
error: aborting due to 5 previous errors

0 comments on commit 3235d9d

Please sign in to comment.