Skip to content

Commit 1e186cf

Browse files
committed
fix(equatable_if_let): don't lint if pattern or initializer come from expansion
1 parent e6549ec commit 1e186cf

File tree

4 files changed

+91
-40
lines changed

4 files changed

+91
-40
lines changed

clippy_lints/src/equatable_if_let.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ impl<'tcx> LateLintPass<'tcx> for PatternEquality {
104104
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
105105
if let ExprKind::Let(let_expr) = expr.kind
106106
&& is_unary_pattern(let_expr.pat)
107-
&& !expr.span.in_external_macro(cx.sess().source_map())
107+
&& !let_expr.pat.span.from_expansion()
108+
&& !let_expr.init.span.from_expansion()
108109
{
109110
let exp_ty = cx.typeck_results().expr_ty(let_expr.init);
110111
let pat_ty = cx.typeck_results().pat_ty(let_expr.pat);

tests/ui/equatable_if_let.fixed

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ impl PartialEq for NotStructuralEq {
4141
}
4242
}
4343

44-
#[inline_macros]
4544
fn main() {
4645
let a = 2;
4746
let b = 3;
@@ -88,13 +87,6 @@ fn main() {
8887
//~^ equatable_if_let
8988
if matches!(h, NoPartialEqStruct { a: 2, b: false }) {}
9089
//~^ equatable_if_let
91-
92-
if "abc" == inline!("abc") {
93-
//~^ equatable_if_let
94-
println!("OK");
95-
}
96-
97-
external!({ if let 2 = $a {} });
9890
}
9991

10092
mod issue8710 {
@@ -132,3 +124,39 @@ mod issue8710 {
132124
}
133125
}
134126
}
127+
128+
#[inline_macros]
129+
fn issue14548() {
130+
if let inline!("abc") = "abc" {
131+
println!("OK");
132+
}
133+
134+
let a = 2;
135+
external!({ if let 2 = $a {} });
136+
137+
macro_rules! generate_script {
138+
($($($font:literal)|*),+) => {
139+
pub fn script(font: &str) {
140+
$(
141+
if let $($font)|* = font {}
142+
)*
143+
}
144+
}
145+
}
146+
147+
generate_script! {
148+
"Sans" | "Serif" | "Sans Mono",
149+
"Znamenny Musical Notation"
150+
}
151+
152+
macro_rules! generate_script2 {
153+
($($font:literal)|*) => {{
154+
pub fn script2(font: &str) {
155+
if let $($font)|* = font {}
156+
}
157+
}}
158+
}
159+
160+
generate_script2!("Sans" | "Serif" | "Sans Mono");
161+
generate_script2!("Znamenny Musical Notation");
162+
}

tests/ui/equatable_if_let.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ impl PartialEq for NotStructuralEq {
4141
}
4242
}
4343

44-
#[inline_macros]
4544
fn main() {
4645
let a = 2;
4746
let b = 3;
@@ -88,13 +87,6 @@ fn main() {
8887
//~^ equatable_if_let
8988
if let NoPartialEqStruct { a: 2, b: false } = h {}
9089
//~^ equatable_if_let
91-
92-
if let inline!("abc") = "abc" {
93-
//~^ equatable_if_let
94-
println!("OK");
95-
}
96-
97-
external!({ if let 2 = $a {} });
9890
}
9991

10092
mod issue8710 {
@@ -132,3 +124,39 @@ mod issue8710 {
132124
}
133125
}
134126
}
127+
128+
#[inline_macros]
129+
fn issue14548() {
130+
if let inline!("abc") = "abc" {
131+
println!("OK");
132+
}
133+
134+
let a = 2;
135+
external!({ if let 2 = $a {} });
136+
137+
macro_rules! generate_script {
138+
($($($font:literal)|*),+) => {
139+
pub fn script(font: &str) {
140+
$(
141+
if let $($font)|* = font {}
142+
)*
143+
}
144+
}
145+
}
146+
147+
generate_script! {
148+
"Sans" | "Serif" | "Sans Mono",
149+
"Znamenny Musical Notation"
150+
}
151+
152+
macro_rules! generate_script2 {
153+
($($font:literal)|*) => {{
154+
pub fn script2(font: &str) {
155+
if let $($font)|* = font {}
156+
}
157+
}}
158+
}
159+
160+
generate_script2!("Sans" | "Serif" | "Sans Mono");
161+
generate_script2!("Znamenny Musical Notation");
162+
}

tests/ui/equatable_if_let.stderr

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this pattern matching can be expressed using equality
2-
--> tests/ui/equatable_if_let.rs:57:8
2+
--> tests/ui/equatable_if_let.rs:56:8
33
|
44
LL | if let 2 = a {}
55
| ^^^^^^^^^ help: try: `a == 2`
@@ -8,100 +8,94 @@ LL | if let 2 = a {}
88
= help: to override `-D warnings` add `#[allow(clippy::equatable_if_let)]`
99

1010
error: this pattern matching can be expressed using equality
11-
--> tests/ui/equatable_if_let.rs:59:8
11+
--> tests/ui/equatable_if_let.rs:58:8
1212
|
1313
LL | if let Ordering::Greater = a.cmp(&b) {}
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `a.cmp(&b) == Ordering::Greater`
1515

1616
error: this pattern matching can be expressed using equality
17-
--> tests/ui/equatable_if_let.rs:61:8
17+
--> tests/ui/equatable_if_let.rs:60:8
1818
|
1919
LL | if let Some(2) = c {}
2020
| ^^^^^^^^^^^^^^^ help: try: `c == Some(2)`
2121

2222
error: this pattern matching can be expressed using equality
23-
--> tests/ui/equatable_if_let.rs:63:8
23+
--> tests/ui/equatable_if_let.rs:62:8
2424
|
2525
LL | if let Struct { a: 2, b: false } = d {}
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `d == (Struct { a: 2, b: false })`
2727

2828
error: this pattern matching can be expressed using equality
29-
--> tests/ui/equatable_if_let.rs:65:8
29+
--> tests/ui/equatable_if_let.rs:64:8
3030
|
3131
LL | if let Enum::TupleVariant(32, 64) = e {}
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == Enum::TupleVariant(32, 64)`
3333

3434
error: this pattern matching can be expressed using equality
35-
--> tests/ui/equatable_if_let.rs:67:8
35+
--> tests/ui/equatable_if_let.rs:66:8
3636
|
3737
LL | if let Enum::RecordVariant { a: 64, b: 32 } = e {}
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == (Enum::RecordVariant { a: 64, b: 32 })`
3939

4040
error: this pattern matching can be expressed using equality
41-
--> tests/ui/equatable_if_let.rs:69:8
41+
--> tests/ui/equatable_if_let.rs:68:8
4242
|
4343
LL | if let Enum::UnitVariant = e {}
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `e == Enum::UnitVariant`
4545

4646
error: this pattern matching can be expressed using equality
47-
--> tests/ui/equatable_if_let.rs:71:8
47+
--> tests/ui/equatable_if_let.rs:70:8
4848
|
4949
LL | if let (Enum::UnitVariant, &Struct { a: 2, b: false }) = (e, &d) {}
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(e, &d) == (Enum::UnitVariant, &Struct { a: 2, b: false })`
5151

5252
error: this pattern matching can be expressed using `matches!`
53-
--> tests/ui/equatable_if_let.rs:81:8
53+
--> tests/ui/equatable_if_let.rs:80:8
5454
|
5555
LL | if let NotPartialEq::A = f {}
5656
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(f, NotPartialEq::A)`
5757

5858
error: this pattern matching can be expressed using equality
59-
--> tests/ui/equatable_if_let.rs:83:8
59+
--> tests/ui/equatable_if_let.rs:82:8
6060
|
6161
LL | if let NotStructuralEq::A = g {}
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `g == NotStructuralEq::A`
6363

6464
error: this pattern matching can be expressed using `matches!`
65-
--> tests/ui/equatable_if_let.rs:85:8
65+
--> tests/ui/equatable_if_let.rs:84:8
6666
|
6767
LL | if let Some(NotPartialEq::A) = Some(f) {}
6868
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(Some(f), Some(NotPartialEq::A))`
6969

7070
error: this pattern matching can be expressed using equality
71-
--> tests/ui/equatable_if_let.rs:87:8
71+
--> tests/ui/equatable_if_let.rs:86:8
7272
|
7373
LL | if let Some(NotStructuralEq::A) = Some(g) {}
7474
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(g) == Some(NotStructuralEq::A)`
7575

7676
error: this pattern matching can be expressed using `matches!`
77-
--> tests/ui/equatable_if_let.rs:89:8
77+
--> tests/ui/equatable_if_let.rs:88:8
7878
|
7979
LL | if let NoPartialEqStruct { a: 2, b: false } = h {}
8080
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(h, NoPartialEqStruct { a: 2, b: false })`
8181

82-
error: this pattern matching can be expressed using equality
83-
--> tests/ui/equatable_if_let.rs:92:8
84-
|
85-
LL | if let inline!("abc") = "abc" {
86-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"abc" == inline!("abc")`
87-
8882
error: this pattern matching can be expressed using `matches!`
89-
--> tests/ui/equatable_if_let.rs:102:12
83+
--> tests/ui/equatable_if_let.rs:94:12
9084
|
9185
LL | if let Some('i') = cs.iter().next() {
9286
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(cs.iter().next(), Some('i'))`
9387

9488
error: this pattern matching can be expressed using `matches!`
95-
--> tests/ui/equatable_if_let.rs:110:12
89+
--> tests/ui/equatable_if_let.rs:102:12
9690
|
9791
LL | if let Some(1) = cs.iter().next() {
9892
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(cs.iter().next(), Some(1))`
9993

10094
error: this pattern matching can be expressed using `matches!`
101-
--> tests/ui/equatable_if_let.rs:128:12
95+
--> tests/ui/equatable_if_let.rs:120:12
10296
|
10397
LL | if let Some(MyEnum::B) = get_enum() {
10498
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(get_enum(), Some(MyEnum::B))`
10599

106-
error: aborting due to 17 previous errors
100+
error: aborting due to 16 previous errors
107101

0 commit comments

Comments
 (0)