Skip to content

Commit a9fa2f5

Browse files
committed
Auto merge of rust-lang#12074 - ARandomDev99:12044-include-comments-while-checking-duplicate-code, r=Jarcho
Make `HirEqInterExpr::eq_block` take comments into account while checking if two blocks are equal This PR: - now makes `HirEqInterExpr::eq_block` take comments into account. Identical code with varying comments will no longer be considered equal. - makes necessary adjustments to UI tests. Closes rust-lang#12044 **Lintcheck Changes** - `match_same_arms` 53 => 52 - `if_same_then_else` 3 => 0 changelog: [`if_same_then_else`]: Blocks with different comments will no longer trigger this lint. changelog: [`match_same_arms`]: Arms with different comments will no longer trigger this lint. ```
2 parents a71211d + 543d569 commit a9fa2f5

11 files changed

+73
-138
lines changed

clippy_utils/src/hir_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl HirEqInterExpr<'_, '_, '_> {
134134
/// Checks whether two blocks are the same.
135135
#[expect(clippy::similar_names)]
136136
fn eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool {
137-
use TokenKind::{BlockComment, LineComment, Semi, Whitespace};
137+
use TokenKind::{Semi, Whitespace};
138138
if left.stmts.len() != right.stmts.len() {
139139
return false;
140140
}
@@ -177,7 +177,7 @@ impl HirEqInterExpr<'_, '_, '_> {
177177
return false;
178178
}
179179
if !eq_span_tokens(self.inner.cx, lstart..lstmt_span.lo, rstart..rstmt_span.lo, |t| {
180-
!matches!(t, Whitespace | LineComment { .. } | BlockComment { .. } | Semi)
180+
!matches!(t, Whitespace | Semi)
181181
}) {
182182
return false;
183183
}
@@ -212,7 +212,7 @@ impl HirEqInterExpr<'_, '_, '_> {
212212
return false;
213213
}
214214
eq_span_tokens(self.inner.cx, lstart..lend, rstart..rend, |t| {
215-
!matches!(t, Whitespace | LineComment { .. } | BlockComment { .. } | Semi)
215+
!matches!(t, Whitespace | Semi)
216216
})
217217
}
218218

tests/ui/branches_sharing_code/shared_at_top.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@ fn simple_examples() {
99

1010
// Simple
1111
if true {
12-
//~^ ERROR: all if blocks contain the same code at the start
1312
println!("Hello World!");
1413
println!("I'm branch nr: 1");
1514
} else {
1615
println!("Hello World!");
1716
println!("I'm branch nr: 2");
1817
}
18+
//~^^^^^^^ ERROR: all if blocks contain the same code at the start
1919

2020
// Else if
2121
if x == 0 {
22-
//~^ ERROR: all if blocks contain the same code at the start
2322
let y = 9;
2423
println!("The value y was set to: `{}`", y);
2524
let _z = y;
@@ -38,6 +37,7 @@ fn simple_examples() {
3837

3938
println!("Ha, Pascal allows you to start the array where you want")
4039
}
40+
//~^^^^^^^^^^^^^^^^^^^ ERROR: all if blocks contain the same code at the start
4141

4242
// Return a value
4343
let _ = if x == 7 {
@@ -60,7 +60,6 @@ fn simple_but_suggestion_is_invalid() {
6060
// Can't be automatically moved because used_value_name is getting used again
6161
let used_value_name = 19;
6262
if x == 10 {
63-
//~^ ERROR: all if blocks contain the same code at the start
6463
let used_value_name = "Different type";
6564
println!("Str: {}", used_value_name);
6665
let _ = 1;
@@ -69,6 +68,7 @@ fn simple_but_suggestion_is_invalid() {
6968
println!("Str: {}", used_value_name);
7069
let _ = 2;
7170
}
71+
//~^^^^^^^^^ ERROR: all if blocks contain the same code at the start
7272
let _ = used_value_name;
7373

7474
// This can be automatically moved as `can_be_overridden` is not used again
@@ -101,11 +101,11 @@ fn check_if_same_than_else_mask() {
101101
}
102102

103103
if x == 2019 {
104-
//~^ ERROR: this `if` has identical blocks
105104
println!("This should trigger `IS_SAME_THAN_ELSE` as usual");
106105
} else {
107106
println!("This should trigger `IS_SAME_THAN_ELSE` as usual");
108107
}
108+
//~^^^^^ ERROR: this `if` has identical blocks
109109
}
110110

111111
#[allow(clippy::vec_init_then_push)]

tests/ui/branches_sharing_code/shared_at_top.stderr

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ error: all if blocks contain the same code at the start
22
--> $DIR/shared_at_top.rs:11:5
33
|
44
LL | / if true {
5-
LL | |
65
LL | | println!("Hello World!");
76
| |_________________________________^
87
|
@@ -21,7 +20,6 @@ error: all if blocks contain the same code at the start
2120
--> $DIR/shared_at_top.rs:21:5
2221
|
2322
LL | / if x == 0 {
24-
LL | |
2523
LL | | let y = 9;
2624
LL | | println!("The value y was set to: `{}`", y);
2725
LL | | let _z = y;
@@ -54,7 +52,6 @@ error: all if blocks contain the same code at the start
5452
--> $DIR/shared_at_top.rs:62:5
5553
|
5654
LL | / if x == 10 {
57-
LL | |
5855
LL | | let used_value_name = "Different type";
5956
LL | | println!("Str: {}", used_value_name);
6057
| |_____________________________________________^
@@ -105,13 +102,12 @@ error: this `if` has identical blocks
105102
|
106103
LL | if x == 2019 {
107104
| __________________^
108-
LL | |
109105
LL | | println!("This should trigger `IS_SAME_THAN_ELSE` as usual");
110106
LL | | } else {
111107
| |_____^
112108
|
113109
note: same as this
114-
--> $DIR/shared_at_top.rs:106:12
110+
--> $DIR/shared_at_top.rs:105:12
115111
|
116112
LL | } else {
117113
| ____________^

tests/ui/branches_sharing_code/valid_if_blocks.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ fn valid_examples() {
107107

108108
// Let's test empty blocks
109109
if false {
110-
//~^ ERROR: this `if` has identical blocks
111110
} else {
112111
}
112+
//~^^^ ERROR: this `if` has identical blocks
113113
}
114114

115115
/// This makes sure that the `if_same_then_else` masks the `shared_code_in_if_blocks` lint
@@ -119,7 +119,6 @@ fn trigger_other_lint() {
119119

120120
// Same block
121121
if x == 0 {
122-
//~^ ERROR: this `if` has identical blocks
123122
let u = 19;
124123
println!("How are u today?");
125124
let _ = "This is a string";
@@ -128,6 +127,7 @@ fn trigger_other_lint() {
128127
println!("How are u today?");
129128
let _ = "This is a string";
130129
}
130+
//~^^^^^^^^^ ERROR: this `if` has identical blocks
131131

132132
// Only same expression
133133
let _ = if x == 6 { 7 } else { 7 };
@@ -138,28 +138,24 @@ fn trigger_other_lint() {
138138
println!("Well I'm the most important block");
139139
"I'm a pretty string"
140140
} else if x == 68 {
141-
//~^ ERROR: this `if` has identical blocks
142141
println!("I'm a doppelgänger");
143-
// Don't listen to my clone below
144142

145143
if y == 90 { "=^.^=" } else { ":D" }
146144
} else {
147-
// Don't listen to my clone above
148145
println!("I'm a doppelgänger");
149146

150147
if y == 90 { "=^.^=" } else { ":D" }
151148
};
149+
//~^^^^^^^^^ ERROR: this `if` has identical blocks
152150

153151
if x == 0 {
154152
println!("I'm single");
155153
} else if x == 68 {
156-
//~^ ERROR: this `if` has identical blocks
157154
println!("I'm a doppelgänger");
158-
// Don't listen to my clone below
159155
} else {
160-
// Don't listen to my clone above
161156
println!("I'm a doppelgänger");
162157
}
158+
//~^^^^^ ERROR: this `if` has identical blocks
163159
}
164160

165161
fn main() {}

tests/ui/branches_sharing_code/valid_if_blocks.stderr

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ error: this `if` has identical blocks
33
|
44
LL | if false {
55
| ______________^
6-
LL | |
76
LL | | } else {
87
| |_____^
98
|
109
note: same as this
11-
--> $DIR/valid_if_blocks.rs:111:12
10+
--> $DIR/valid_if_blocks.rs:110:12
1211
|
1312
LL | } else {
1413
| ____________^
@@ -25,15 +24,14 @@ error: this `if` has identical blocks
2524
|
2625
LL | if x == 0 {
2726
| _______________^
28-
LL | |
2927
LL | | let u = 19;
3028
LL | | println!("How are u today?");
3129
LL | | let _ = "This is a string";
3230
LL | | } else {
3331
| |_____^
3432
|
3533
note: same as this
36-
--> $DIR/valid_if_blocks.rs:126:12
34+
--> $DIR/valid_if_blocks.rs:125:12
3735
|
3836
LL | } else {
3937
| ____________^
@@ -60,43 +58,37 @@ error: this `if` has identical blocks
6058
|
6159
LL | } else if x == 68 {
6260
| _______________________^
63-
LL | |
6461
LL | | println!("I'm a doppelgänger");
65-
LL | | // Don't listen to my clone below
6662
LL | |
6763
LL | | if y == 90 { "=^.^=" } else { ":D" }
6864
LL | | } else {
6965
| |_____^
7066
|
7167
note: same as this
72-
--> $DIR/valid_if_blocks.rs:146:12
68+
--> $DIR/valid_if_blocks.rs:144:12
7369
|
7470
LL | } else {
7571
| ____________^
76-
LL | | // Don't listen to my clone above
7772
LL | | println!("I'm a doppelgänger");
7873
LL | |
7974
LL | | if y == 90 { "=^.^=" } else { ":D" }
8075
LL | | };
8176
| |_____^
8277

8378
error: this `if` has identical blocks
84-
--> $DIR/valid_if_blocks.rs:155:23
79+
--> $DIR/valid_if_blocks.rs:153:23
8580
|
8681
LL | } else if x == 68 {
8782
| _______________________^
88-
LL | |
8983
LL | | println!("I'm a doppelgänger");
90-
LL | | // Don't listen to my clone below
9184
LL | | } else {
9285
| |_____^
9386
|
9487
note: same as this
95-
--> $DIR/valid_if_blocks.rs:159:12
88+
--> $DIR/valid_if_blocks.rs:155:12
9689
|
9790
LL | } else {
9891
| ____________^
99-
LL | | // Don't listen to my clone above
10092
LL | | println!("I'm a doppelgänger");
10193
LL | | }
10294
| |_____^

tests/ui/if_same_then_else.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ fn foo() -> bool {
2121

2222
fn if_same_then_else() {
2323
if true {
24-
//~^ ERROR: this `if` has identical blocks
2524
Foo { bar: 42 };
2625
0..10;
2726
..;
@@ -38,6 +37,7 @@ fn if_same_then_else() {
3837
0..=10;
3938
foo();
4039
}
40+
//~^^^^^^^^^^^^^^^^^ ERROR: this `if` has identical blocks
4141

4242
if true {
4343
Foo { bar: 42 };
@@ -64,19 +64,11 @@ fn if_same_then_else() {
6464
foo();
6565
}
6666

67-
let _ = if true {
68-
//~^ ERROR: this `if` has identical blocks
69-
0.0
70-
} else {
71-
0.0
72-
};
67+
let _ = if true { 0.0 } else { 0.0 };
68+
//~^ ERROR: this `if` has identical blocks
7369

74-
let _ = if true {
75-
//~^ ERROR: this `if` has identical blocks
76-
-0.0
77-
} else {
78-
-0.0
79-
};
70+
let _ = if true { -0.0 } else { -0.0 };
71+
//~^ ERROR: this `if` has identical blocks
8072

8173
let _ = if true { 0.0 } else { -0.0 };
8274

@@ -87,15 +79,10 @@ fn if_same_then_else() {
8779
foo();
8880
}
8981

90-
let _ = if true {
91-
//~^ ERROR: this `if` has identical blocks
92-
42
93-
} else {
94-
42
95-
};
82+
let _ = if true { 42 } else { 42 };
83+
//~^ ERROR: this `if` has identical blocks
9684

9785
if true {
98-
//~^ ERROR: this `if` has identical blocks
9986
let bar = if true { 42 } else { 43 };
10087

10188
while foo() {
@@ -110,6 +97,7 @@ fn if_same_then_else() {
11097
}
11198
bar + 1;
11299
}
100+
//~^^^^^^^^^^^^^^^ ERROR: this `if` has identical blocks
113101

114102
if true {
115103
let _ = match 42 {

0 commit comments

Comments
 (0)