Skip to content
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

single_match: suggest if over if let when possible #6574

Merged
merged 3 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 66 additions & 13 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use crate::consts::{constant, miri_to_const, Constant};
use crate::utils::sugg::Sugg;
use crate::utils::usage::is_unused;
use crate::utils::{
expr_block, get_arg_name, get_parent_expr, in_macro, indent_of, is_allowed, is_expn_of, is_refutable,
is_type_diagnostic_item, is_wild, match_qpath, match_type, match_var, meets_msrv, multispan_sugg, remove_blocks,
snippet, snippet_block, snippet_opt, snippet_with_applicability, span_lint_and_help, span_lint_and_note,
span_lint_and_sugg, span_lint_and_then,
expr_block, get_arg_name, get_parent_expr, implements_trait, in_macro, indent_of, is_allowed, is_expn_of,
is_refutable, is_type_diagnostic_item, is_wild, match_qpath, match_type, match_var, meets_msrv, multispan_sugg,
remove_blocks, snippet, snippet_block, snippet_opt, snippet_with_applicability, span_lint_and_help,
span_lint_and_note, span_lint_and_sugg, span_lint_and_then,
};
use crate::utils::{paths, search_same, SpanlessEq, SpanlessHash};
use if_chain::if_chain;
Expand Down Expand Up @@ -717,6 +717,28 @@ fn check_single_match_single_pattern(
}
}

fn peel_pat_refs(pat: &'a Pat<'a>) -> (&'a Pat<'a>, usize) {
fn peel(pat: &'a Pat<'a>, count: usize) -> (&'a Pat<'a>, usize) {
if let PatKind::Ref(pat, _) = pat.kind {
peel(pat, count + 1)
} else {
(pat, count)
}
}
peel(pat, 0)
}

fn peel_ty_refs(ty: Ty<'_>) -> (Ty<'_>, usize) {
Jarcho marked this conversation as resolved.
Show resolved Hide resolved
fn peel(ty: Ty<'_>, count: usize) -> (Ty<'_>, usize) {
if let ty::Ref(_, ty, _) = ty.kind() {
peel(ty, count + 1)
} else {
(ty, count)
}
}
peel(ty, 0)
}

fn report_single_match_single_pattern(
cx: &LateContext<'_>,
ex: &Expr<'_>,
Expand All @@ -728,20 +750,51 @@ fn report_single_match_single_pattern(
let els_str = els.map_or(String::new(), |els| {
format!(" else {}", expr_block(cx, els, None, "..", Some(expr.span)))
});

let (msg, sugg) = if_chain! {
let (pat, pat_ref_count) = peel_pat_refs(arms[0].pat);
if let PatKind::Path(_) | PatKind::Lit(_) = pat.kind;
let (ty, ty_ref_count) = peel_ty_refs(cx.typeck_results().expr_ty(ex));
if let Some(trait_id) = cx.tcx.lang_items().structural_peq_trait();
if ty.is_integral() || ty.is_char() || ty.is_str() || implements_trait(cx, ty, trait_id, &[]);
then {
// scrutinee derives PartialEq and the pattern is a constant.
let pat_ref_count = match pat.kind {
// string literals are already a reference.
PatKind::Lit(Expr { kind: ExprKind::Lit(lit), .. }) if lit.node.is_str() => pat_ref_count + 1,
_ => pat_ref_count,
};
let msg = "you seem to be trying to use match for an equality check. Consider using `if`";
Jarcho marked this conversation as resolved.
Show resolved Hide resolved
let sugg = format!(
"if {} == {}{} {}{}",
snippet(cx, ex.span, ".."),
// PartialEq for different reference counts may not exist.
"&".repeat(ty_ref_count - pat_ref_count),
snippet(cx, arms[0].pat.span, ".."),
expr_block(cx, &arms[0].body, None, "..", Some(expr.span)),
els_str,
);
(msg, sugg)
} else {
let msg = "you seem to be trying to use match for destructuring a single pattern. Consider using `if let`";
Jarcho marked this conversation as resolved.
Show resolved Hide resolved
let sugg = format!(
"if let {} = {} {}{}",
snippet(cx, arms[0].pat.span, ".."),
snippet(cx, ex.span, ".."),
expr_block(cx, &arms[0].body, None, "..", Some(expr.span)),
els_str,
);
(msg, sugg)
}
};

span_lint_and_sugg(
cx,
lint,
expr.span,
"you seem to be trying to use match for destructuring a single pattern. Consider using `if \
let`",
msg,
"try this",
format!(
"if let {} = {} {}{}",
snippet(cx, arms[0].pat.span, ".."),
snippet(cx, ex.span, ".."),
expr_block(cx, &arms[0].body, None, "..", Some(expr.span)),
els_str,
),
sugg,
Applicability::HasPlaceholders,
);
}
Expand Down
43 changes: 43 additions & 0 deletions tests/ui/single_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,49 @@ fn single_match_know_enum() {
}
}

fn issue_173() {
Jarcho marked this conversation as resolved.
Show resolved Hide resolved
let x = "test";
match x {
"test" => println!(),
_ => (),
}

#[derive(PartialEq, Eq)]
enum Foo {
A,
B,
C(u32),
}

let x = Foo::A;
match x {
Foo::A => println!(),
_ => (),
}

const FOO_C: Foo = Foo::C(0);
match x {
FOO_C => println!(),
_ => (),
}
enum Bar {
A,
B,
}
impl PartialEq for Bar {
fn eq(&self, rhs: &Self) -> bool {
matches!((self, rhs), (Self::A, Self::A) | (Self::B, Self::B))
}
}
impl Eq for Bar {}

let x = Bar::A;
match x {
Bar::A => println!(),
_ => (),
}
}

macro_rules! single_match {
($num:literal) => {
match $num {
Expand Down
38 changes: 37 additions & 1 deletion tests/ui/single_match.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,41 @@ LL | | Cow::Owned(..) => (),
LL | | };
| |_____^ help: try this: `if let Cow::Borrowed(..) = c { dummy() }`

error: aborting due to 6 previous errors
error: you seem to be trying to use match for an equality check. Consider using `if`
--> $DIR/single_match.rs:86:5
|
LL | / match x {
LL | | "test" => println!(),
LL | | _ => (),
LL | | }
| |_____^ help: try this: `if x == "test" { println!() }`

error: you seem to be trying to use match for an equality check. Consider using `if`
--> $DIR/single_match.rs:99:5
|
LL | / match x {
LL | | Foo::A => println!(),
LL | | _ => (),
LL | | }
| |_____^ help: try this: `if x == Foo::A { println!() }`

error: you seem to be trying to use match for an equality check. Consider using `if`
--> $DIR/single_match.rs:105:5
|
LL | / match x {
LL | | FOO_C => println!(),
LL | | _ => (),
LL | | }
| |_____^ help: try this: `if x == FOO_C { println!() }`

error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
--> $DIR/single_match.rs:121:5
|
LL | / match x {
LL | | Bar::A => println!(),
LL | | _ => (),
LL | | }
| |_____^ help: try this: `if let Bar::A = x { println!() }`

error: aborting due to 10 previous errors