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 16, 2023
1 parent 734c74d commit fcb0d9a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
25 changes: 17 additions & 8 deletions clippy_lints/src/methods/filter_map_bool_then.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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, match_def_path, peel_blocks};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
Expand All @@ -13,17 +14,25 @@ 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 body = cx.tcx.hir().body(closure.body)
&& let value = peel_blocks(body.value)
// Indexing should be fine as `filter` 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))
&& 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(filter) = snippet_opt(cx, recv.span)
&& let Some(param_snippet) = snippet_opt(cx, param.span)
&& let Some(filter) =
// I'm fairly sure removing all parenthesis is fine, as something like
// `(true == true).then(<etc>)` would no longer need them as `bool::then` is removed. I
// can't think of any other case that would trigger the lint yet need parenthesis.
snippet_opt(cx, recv.span).map(|s| s.trim_start_matches('(').trim_end_matches(')').to_owned())
&& let Some(map) = snippet_opt(cx, then_body.span)
{
span_lint_and_sugg(
Expand All @@ -32,7 +41,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
4 changes: 3 additions & 1 deletion clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2516,7 +2516,9 @@ pub fn tokenize_with_text(s: &str) -> impl Iterator<Item = (TokenKind, &str)> {
/// Checks whether a given span has any comment token
/// This checks for all types of comment: line "//", block "/**", doc "///" "//!"
pub fn span_contains_comment(sm: &SourceMap, span: Span) -> bool {
let Ok(snippet) = sm.span_to_snippet(span) else { return false };
let Ok(snippet) = sm.span_to_snippet(span) else {
return false;
};
return tokenize(&snippet).any(|token| {
matches!(
token.kind,
Expand Down
9 changes: 5 additions & 4 deletions tests/ui/filter_map_bool_then.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ extern crate proc_macros;

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
external! {
let v = vec![1, 2, 3, 4, 5, 6];
Expand Down
1 change: 1 addition & 0 deletions tests/ui/filter_map_bool_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern crate proc_macros;

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 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:11: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:12: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:15: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:19: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:23: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 fcb0d9a

Please sign in to comment.