Skip to content

Commit 85edd65

Browse files
committed
Address review comments
Add: attempt to remove address of expressions from the scrutinee expression before adding references to the pattern
1 parent 8d7417d commit 85edd65

File tree

5 files changed

+104
-48
lines changed

5 files changed

+104
-48
lines changed

clippy_lints/src/matches.rs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::utils::usage::is_unused;
44
use crate::utils::{
55
expr_block, get_arg_name, get_parent_expr, implements_trait, in_macro, indent_of, is_allowed, is_expn_of,
66
is_refutable, is_type_diagnostic_item, is_wild, match_qpath, match_type, match_var, meets_msrv, multispan_sugg,
7-
remove_blocks, snippet, snippet_block, snippet_opt, snippet_with_applicability, span_lint_and_help,
8-
span_lint_and_note, span_lint_and_sugg, span_lint_and_then,
7+
peel_hir_pat_refs, peel_mid_ty_refs, peeln_hir_expr_refs, remove_blocks, snippet, snippet_block, snippet_opt,
8+
snippet_with_applicability, span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then,
99
};
1010
use crate::utils::{paths, search_same, SpanlessEq, SpanlessHash};
1111
use if_chain::if_chain;
@@ -717,28 +717,6 @@ fn check_single_match_single_pattern(
717717
}
718718
}
719719

720-
fn peel_pat_refs(pat: &'a Pat<'a>) -> (&'a Pat<'a>, usize) {
721-
fn peel(pat: &'a Pat<'a>, count: usize) -> (&'a Pat<'a>, usize) {
722-
if let PatKind::Ref(pat, _) = pat.kind {
723-
peel(pat, count + 1)
724-
} else {
725-
(pat, count)
726-
}
727-
}
728-
peel(pat, 0)
729-
}
730-
731-
fn peel_ty_refs(ty: Ty<'_>) -> (Ty<'_>, usize) {
732-
fn peel(ty: Ty<'_>, count: usize) -> (Ty<'_>, usize) {
733-
if let ty::Ref(_, ty, _) = ty.kind() {
734-
peel(ty, count + 1)
735-
} else {
736-
(ty, count)
737-
}
738-
}
739-
peel(ty, 0)
740-
}
741-
742720
fn report_single_match_single_pattern(
743721
cx: &LateContext<'_>,
744722
ex: &Expr<'_>,
@@ -752,9 +730,9 @@ fn report_single_match_single_pattern(
752730
});
753731

754732
let (msg, sugg) = if_chain! {
755-
let (pat, pat_ref_count) = peel_pat_refs(arms[0].pat);
733+
let (pat, pat_ref_count) = peel_hir_pat_refs(arms[0].pat);
756734
if let PatKind::Path(_) | PatKind::Lit(_) = pat.kind;
757-
let (ty, ty_ref_count) = peel_ty_refs(cx.typeck_results().expr_ty(ex));
735+
let (ty, ty_ref_count) = peel_mid_ty_refs(cx.typeck_results().expr_ty(ex));
758736
if let Some(trait_id) = cx.tcx.lang_items().structural_peq_trait();
759737
if ty.is_integral() || ty.is_char() || ty.is_str() || implements_trait(cx, ty, trait_id, &[]);
760738
then {
@@ -764,19 +742,28 @@ fn report_single_match_single_pattern(
764742
PatKind::Lit(Expr { kind: ExprKind::Lit(lit), .. }) if lit.node.is_str() => pat_ref_count + 1,
765743
_ => pat_ref_count,
766744
};
767-
let msg = "you seem to be trying to use match for an equality check. Consider using `if`";
745+
// References are only implicitly added to the pattern, so no overflow here.
746+
// e.g. will work: match &Some(_) { Some(_) => () }
747+
// will not: match Some(_) { &Some(_) => () }
748+
let ref_count_diff = ty_ref_count - pat_ref_count;
749+
750+
// Try to remove address of expressions first.
751+
let (ex, removed) = peeln_hir_expr_refs(ex, ref_count_diff);
752+
let ref_count_diff = ref_count_diff - removed;
753+
754+
let msg = "you seem to be trying to use `match` for an equality check. Consider using `if`";
768755
let sugg = format!(
769756
"if {} == {}{} {}{}",
770757
snippet(cx, ex.span, ".."),
771758
// PartialEq for different reference counts may not exist.
772-
"&".repeat(ty_ref_count - pat_ref_count),
759+
"&".repeat(ref_count_diff),
773760
snippet(cx, arms[0].pat.span, ".."),
774761
expr_block(cx, &arms[0].body, None, "..", Some(expr.span)),
775762
els_str,
776763
);
777764
(msg, sugg)
778765
} else {
779-
let msg = "you seem to be trying to use match for destructuring a single pattern. Consider using `if let`";
766+
let msg = "you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`";
780767
let sugg = format!(
781768
"if let {} = {} {}{}",
782769
snippet(cx, arms[0].pat.span, ".."),

clippy_lints/src/utils/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,44 @@ where
16681668
match_expr_list
16691669
}
16701670

1671+
/// Peels off all references on the pattern. Returns the underlying pattern and the number of
1672+
/// references removed.
1673+
pub fn peel_hir_pat_refs(pat: &'a Pat<'a>) -> (&'a Pat<'a>, usize) {
1674+
fn peel(pat: &'a Pat<'a>, count: usize) -> (&'a Pat<'a>, usize) {
1675+
if let PatKind::Ref(pat, _) = pat.kind {
1676+
peel(pat, count + 1)
1677+
} else {
1678+
(pat, count)
1679+
}
1680+
}
1681+
peel(pat, 0)
1682+
}
1683+
1684+
/// Peels off up to the given number of references on the expression. Returns the underlying
1685+
/// expression and the number of references removed.
1686+
pub fn peeln_hir_expr_refs(expr: &'a Expr<'a>, count: usize) -> (&'a Expr<'a>, usize) {
1687+
fn f(expr: &'a Expr<'a>, count: usize, target: usize) -> (&'a Expr<'a>, usize) {
1688+
match expr.kind {
1689+
ExprKind::AddrOf(_, _, expr) if count != target => f(expr, count + 1, target),
1690+
_ => (expr, count),
1691+
}
1692+
}
1693+
f(expr, 0, count)
1694+
}
1695+
1696+
/// Peels off all references on the type. Returns the underlying type and the number of references
1697+
/// removed.
1698+
pub fn peel_mid_ty_refs(ty: Ty<'_>) -> (Ty<'_>, usize) {
1699+
fn peel(ty: Ty<'_>, count: usize) -> (Ty<'_>, usize) {
1700+
if let ty::Ref(_, ty, _) = ty.kind() {
1701+
peel(ty, count + 1)
1702+
} else {
1703+
(ty, count)
1704+
}
1705+
}
1706+
peel(ty, 0)
1707+
}
1708+
16711709
#[macro_export]
16721710
macro_rules! unwrap_cargo_metadata {
16731711
($cx: ident, $lint: ident, $deps: expr) => {{

tests/ui/single_match.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ fn single_match_know_enum() {
8181
}
8282
}
8383

84-
fn issue_173() {
84+
// issue #173
85+
fn if_suggestion() {
8586
let x = "test";
8687
match x {
8788
"test" => println!(),
@@ -106,6 +107,18 @@ fn issue_173() {
106107
FOO_C => println!(),
107108
_ => (),
108109
}
110+
111+
match &&x {
112+
Foo::A => println!(),
113+
_ => (),
114+
}
115+
116+
let x = &x;
117+
match &x {
118+
Foo::A => println!(),
119+
_ => (),
120+
}
121+
109122
enum Bar {
110123
A,
111124
B,

tests/ui/single_match.stderr

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
1+
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
22
--> $DIR/single_match.rs:8:5
33
|
44
LL | / match x {
@@ -17,7 +17,7 @@ LL | println!("{:?}", y);
1717
LL | };
1818
|
1919

20-
error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
20+
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
2121
--> $DIR/single_match.rs:16:5
2222
|
2323
LL | / match x {
@@ -29,7 +29,7 @@ LL | | _ => (),
2929
LL | | }
3030
| |_____^ help: try this: `if let Some(y) = x { println!("{:?}", y) }`
3131

32-
error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
32+
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
3333
--> $DIR/single_match.rs:25:5
3434
|
3535
LL | / match z {
@@ -38,7 +38,7 @@ LL | | _ => {},
3838
LL | | };
3939
| |_____^ help: try this: `if let (2..=3, 7..=9) = z { dummy() }`
4040

41-
error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
41+
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
4242
--> $DIR/single_match.rs:54:5
4343
|
4444
LL | / match x {
@@ -47,7 +47,7 @@ LL | | None => (),
4747
LL | | };
4848
| |_____^ help: try this: `if let Some(y) = x { dummy() }`
4949

50-
error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
50+
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
5151
--> $DIR/single_match.rs:59:5
5252
|
5353
LL | / match y {
@@ -56,7 +56,7 @@ LL | | Err(..) => (),
5656
LL | | };
5757
| |_____^ help: try this: `if let Ok(y) = y { dummy() }`
5858

59-
error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
59+
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
6060
--> $DIR/single_match.rs:66:5
6161
|
6262
LL | / match c {
@@ -65,41 +65,59 @@ LL | | Cow::Owned(..) => (),
6565
LL | | };
6666
| |_____^ help: try this: `if let Cow::Borrowed(..) = c { dummy() }`
6767

68-
error: you seem to be trying to use match for an equality check. Consider using `if`
69-
--> $DIR/single_match.rs:86:5
68+
error: you seem to be trying to use `match` for an equality check. Consider using `if`
69+
--> $DIR/single_match.rs:87:5
7070
|
7171
LL | / match x {
7272
LL | | "test" => println!(),
7373
LL | | _ => (),
7474
LL | | }
7575
| |_____^ help: try this: `if x == "test" { println!() }`
7676

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

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

95-
error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
96-
--> $DIR/single_match.rs:121:5
95+
error: you seem to be trying to use `match` for an equality check. Consider using `if`
96+
--> $DIR/single_match.rs:111:5
97+
|
98+
LL | / match &&x {
99+
LL | | Foo::A => println!(),
100+
LL | | _ => (),
101+
LL | | }
102+
| |_____^ help: try this: `if x == Foo::A { println!() }`
103+
104+
error: you seem to be trying to use `match` for an equality check. Consider using `if`
105+
--> $DIR/single_match.rs:117:5
106+
|
107+
LL | / match &x {
108+
LL | | Foo::A => println!(),
109+
LL | | _ => (),
110+
LL | | }
111+
| |_____^ help: try this: `if x == &Foo::A { println!() }`
112+
113+
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
114+
--> $DIR/single_match.rs:134:5
97115
|
98116
LL | / match x {
99117
LL | | Bar::A => println!(),
100118
LL | | _ => (),
101119
LL | | }
102120
| |_____^ help: try this: `if let Bar::A = x { println!() }`
103121

104-
error: aborting due to 10 previous errors
122+
error: aborting due to 12 previous errors
105123

tests/ui/single_match_else.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
1+
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
22
--> $DIR/single_match_else.rs:14:5
33
|
44
LL | / match ExprNode::Butterflies {
@@ -19,7 +19,7 @@ LL | None
1919
LL | }
2020
|
2121

22-
error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
22+
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
2323
--> $DIR/single_match_else.rs:70:5
2424
|
2525
LL | / match Some(1) {
@@ -39,7 +39,7 @@ LL | return
3939
LL | }
4040
|
4141

42-
error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
42+
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
4343
--> $DIR/single_match_else.rs:79:5
4444
|
4545
LL | / match Some(1) {

0 commit comments

Comments
 (0)