Skip to content

Commit 3235d9d

Browse files
committed
Only lint Copy types
1 parent 978b1da commit 3235d9d

File tree

4 files changed

+59
-25
lines changed

4 files changed

+59
-25
lines changed

clippy_lints/src/methods/filter_map_bool_then.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::paths::BOOL_THEN;
33
use clippy_utils::source::snippet_opt;
4-
use clippy_utils::{is_from_proc_macro, match_def_path, peel_blocks};
4+
use clippy_utils::ty::is_copy;
5+
use clippy_utils::{is_from_proc_macro, is_trait_method, match_def_path, peel_blocks};
56
use rustc_errors::Applicability;
67
use rustc_hir::{Expr, ExprKind};
78
use rustc_lint::{LateContext, LintContext};
89
use rustc_middle::lint::in_external_macro;
9-
use rustc_span::Span;
10+
use rustc_span::{sym, Span};
1011

1112
use super::FILTER_MAP_BOOL_THEN;
1213

1314
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &Expr<'_>, call_span: Span) {
1415
if !in_external_macro(cx.sess(), expr.span)
16+
&& is_trait_method(cx, expr, sym::Iterator)
1517
&& let ExprKind::Closure(closure) = arg.kind
16-
&& let body = peel_blocks(cx.tcx.hir().body(closure.body).value)
17-
&& let ExprKind::MethodCall(_, recv, [then_arg], _) = body.kind
18+
&& let body = cx.tcx.hir().body(closure.body)
19+
&& let value = peel_blocks(body.value)
20+
// Indexing should be fine as `filter_map` always has 1 input, we unfortunately need both
21+
// `inputs` and `params` here as we need both the type and the span
22+
&& let param_ty = closure.fn_decl.inputs[0]
23+
&& let param = body.params[0]
24+
&& is_copy(cx, cx.typeck_results().node_type(param_ty.hir_id).peel_refs())
25+
&& let ExprKind::MethodCall(_, recv, [then_arg], _) = value.kind
1826
&& let ExprKind::Closure(then_closure) = then_arg.kind
1927
&& let then_body = peel_blocks(cx.tcx.hir().body(then_closure.body).value)
20-
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(body.hir_id)
28+
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(value.hir_id)
2129
&& match_def_path(cx, def_id, &BOOL_THEN)
2230
&& !is_from_proc_macro(cx, expr)
23-
&& let Some(decl_snippet) = closure.fn_arg_span.and_then(|s| snippet_opt(cx, s))
24-
// NOTE: This will include the `()` (parenthesis) around it. Maybe there's some utils method
25-
// to remove them? `unused_parens` will already take care of this but it may be nice.
31+
&& let Some(param_snippet) = snippet_opt(cx, param.span)
2632
&& let Some(filter) = snippet_opt(cx, recv.span)
2733
&& let Some(map) = snippet_opt(cx, then_body.span)
2834
{
@@ -32,7 +38,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &
3238
call_span,
3339
"usage of `bool::then` in `filter_map`",
3440
"use `filter` then `map` instead",
35-
format!("filter({decl_snippet} {filter}).map({decl_snippet} {map})"),
41+
format!("filter(|&{param_snippet}| {filter}).map(|{param_snippet}| {map})"),
3642
Applicability::MachineApplicable,
3743
);
3844
}

tests/ui/filter_map_bool_then.fixed

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
//@run-rustfix
22
//@aux-build:proc_macros.rs:proc-macro
3-
#![allow(clippy::clone_on_copy, clippy::unnecessary_lazy_evaluations, unused)]
3+
#![allow(
4+
clippy::clone_on_copy,
5+
clippy::map_identity,
6+
clippy::unnecessary_lazy_evaluations,
7+
unused
8+
)]
49
#![warn(clippy::filter_map_bool_then)]
510

611
#[macro_use]
712
extern crate proc_macros;
813

14+
#[derive(Clone, PartialEq)]
15+
struct NonCopy;
16+
917
fn main() {
1018
let v = vec![1, 2, 3, 4, 5, 6];
11-
v.clone().into_iter().filter(|i| (i % 2 == 0)).map(|i| i + 1);
19+
v.clone().iter().filter(|&i| (i % 2 == 0)).map(|i| i + 1);
20+
v.clone().into_iter().filter(|&i| (i % 2 == 0)).map(|i| i + 1);
1221
v.clone()
1322
.into_iter()
14-
.filter(|i| (i % 2 == 0)).map(|i| i + 1);
23+
.filter(|&i| (i % 2 == 0)).map(|i| i + 1);
1524
v.clone()
1625
.into_iter()
1726
.filter(|&i| i != 1000)
18-
.filter(|i| (i % 2 == 0)).map(|i| i + 1);
27+
.filter(|&i| (i % 2 == 0)).map(|i| i + 1);
1928
v.iter()
2029
.copied()
2130
.filter(|&i| i != 1000)
22-
.filter(|i| (i.clone() % 2 == 0)).map(|i| i + 1);
31+
.filter(|&i| (i.clone() % 2 == 0)).map(|i| i + 1);
2332
// Do not lint
33+
let v = vec![NonCopy, NonCopy];
34+
v.clone().iter().filter_map(|i| (i == &NonCopy).then(|| i));
2435
external! {
2536
let v = vec![1, 2, 3, 4, 5, 6];
2637
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));

tests/ui/filter_map_bool_then.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
//@run-rustfix
22
//@aux-build:proc_macros.rs:proc-macro
3-
#![allow(clippy::clone_on_copy, clippy::unnecessary_lazy_evaluations, unused)]
3+
#![allow(
4+
clippy::clone_on_copy,
5+
clippy::map_identity,
6+
clippy::unnecessary_lazy_evaluations,
7+
unused
8+
)]
49
#![warn(clippy::filter_map_bool_then)]
510

611
#[macro_use]
712
extern crate proc_macros;
813

14+
#[derive(Clone, PartialEq)]
15+
struct NonCopy;
16+
917
fn main() {
1018
let v = vec![1, 2, 3, 4, 5, 6];
19+
v.clone().iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
1120
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
1221
v.clone()
1322
.into_iter()
@@ -21,6 +30,8 @@ fn main() {
2130
.filter(|&i| i != 1000)
2231
.filter_map(|i| (i.clone() % 2 == 0).then(|| i + 1));
2332
// Do not lint
33+
let v = vec![NonCopy, NonCopy];
34+
v.clone().iter().filter_map(|i| (i == &NonCopy).then(|| i));
2435
external! {
2536
let v = vec![1, 2, 3, 4, 5, 6];
2637
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));

tests/ui/filter_map_bool_then.stderr

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
error: usage of `bool::then` in `filter_map`
2-
--> $DIR/filter_map_bool_then.rs:11:27
2+
--> $DIR/filter_map_bool_then.rs:19:22
33
|
4-
LL | v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|i| (i % 2 == 0)).map(|i| i + 1)`
4+
LL | v.clone().iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)`
66
|
77
= note: `-D clippy::filter-map-bool-then` implied by `-D warnings`
88

99
error: usage of `bool::then` in `filter_map`
10-
--> $DIR/filter_map_bool_then.rs:14:10
10+
--> $DIR/filter_map_bool_then.rs:20:27
11+
|
12+
LL | v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)`
14+
15+
error: usage of `bool::then` in `filter_map`
16+
--> $DIR/filter_map_bool_then.rs:23:10
1117
|
1218
LL | .filter_map(|i| -> Option<_> { (i % 2 == 0).then(|| i + 1) });
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|i| (i % 2 == 0)).map(|i| i + 1)`
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)`
1420

1521
error: usage of `bool::then` in `filter_map`
16-
--> $DIR/filter_map_bool_then.rs:18:10
22+
--> $DIR/filter_map_bool_then.rs:27:10
1723
|
1824
LL | .filter_map(|i| (i % 2 == 0).then(|| i + 1));
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|i| (i % 2 == 0)).map(|i| i + 1)`
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)`
2026

2127
error: usage of `bool::then` in `filter_map`
22-
--> $DIR/filter_map_bool_then.rs:22:10
28+
--> $DIR/filter_map_bool_then.rs:31:10
2329
|
2430
LL | .filter_map(|i| (i.clone() % 2 == 0).then(|| i + 1));
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|i| (i.clone() % 2 == 0)).map(|i| i + 1)`
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i.clone() % 2 == 0)).map(|i| i + 1)`
2632

27-
error: aborting due to 4 previous errors
33+
error: aborting due to 5 previous errors
2834

0 commit comments

Comments
 (0)