Skip to content

Make HirEqInterExpr::eq_block take comments into account while checking if two blocks are equal #12074

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

Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions clippy_utils/src/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl HirEqInterExpr<'_, '_, '_> {
/// Checks whether two blocks are the same.
#[expect(clippy::similar_names)]
fn eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool {
use TokenKind::{BlockComment, LineComment, Semi, Whitespace};
use TokenKind::{Semi, Whitespace};
if left.stmts.len() != right.stmts.len() {
return false;
}
Expand Down Expand Up @@ -177,7 +177,7 @@ impl HirEqInterExpr<'_, '_, '_> {
return false;
}
if !eq_span_tokens(self.inner.cx, lstart..lstmt_span.lo, rstart..rstmt_span.lo, |t| {
!matches!(t, Whitespace | LineComment { .. } | BlockComment { .. } | Semi)
!matches!(t, Whitespace | Semi)
}) {
return false;
}
Expand Down Expand Up @@ -212,7 +212,7 @@ impl HirEqInterExpr<'_, '_, '_> {
return false;
}
eq_span_tokens(self.inner.cx, lstart..lend, rstart..rend, |t| {
!matches!(t, Whitespace | LineComment { .. } | BlockComment { .. } | Semi)
!matches!(t, Whitespace | Semi)
})
}

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/branches_sharing_code/shared_at_top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ fn simple_examples() {

// Simple
if true {
//~^ ERROR: all if blocks contain the same code at the start
println!("Hello World!");
println!("I'm branch nr: 1");
} else {
println!("Hello World!");
println!("I'm branch nr: 2");
}
//~^^^^^^^ ERROR: all if blocks contain the same code at the start

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

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

// Return a value
let _ = if x == 7 {
Expand All @@ -60,7 +60,6 @@ fn simple_but_suggestion_is_invalid() {
// Can't be automatically moved because used_value_name is getting used again
let used_value_name = 19;
if x == 10 {
//~^ ERROR: all if blocks contain the same code at the start
let used_value_name = "Different type";
println!("Str: {}", used_value_name);
let _ = 1;
Expand All @@ -69,6 +68,7 @@ fn simple_but_suggestion_is_invalid() {
println!("Str: {}", used_value_name);
let _ = 2;
}
//~^^^^^^^^^ ERROR: all if blocks contain the same code at the start
let _ = used_value_name;

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

if x == 2019 {
//~^ ERROR: this `if` has identical blocks
println!("This should trigger `IS_SAME_THAN_ELSE` as usual");
} else {
println!("This should trigger `IS_SAME_THAN_ELSE` as usual");
}
//~^^^^^ ERROR: this `if` has identical blocks
}

#[allow(clippy::vec_init_then_push)]
Expand Down
6 changes: 1 addition & 5 deletions tests/ui/branches_sharing_code/shared_at_top.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ error: all if blocks contain the same code at the start
--> $DIR/shared_at_top.rs:11:5
|
LL | / if true {
LL | |
LL | | println!("Hello World!");
| |_________________________________^
|
Expand All @@ -21,7 +20,6 @@ error: all if blocks contain the same code at the start
--> $DIR/shared_at_top.rs:21:5
|
LL | / if x == 0 {
LL | |
LL | | let y = 9;
LL | | println!("The value y was set to: `{}`", y);
LL | | let _z = y;
Expand Down Expand Up @@ -54,7 +52,6 @@ error: all if blocks contain the same code at the start
--> $DIR/shared_at_top.rs:62:5
|
LL | / if x == 10 {
LL | |
LL | | let used_value_name = "Different type";
LL | | println!("Str: {}", used_value_name);
| |_____________________________________________^
Expand Down Expand Up @@ -105,13 +102,12 @@ error: this `if` has identical blocks
|
LL | if x == 2019 {
| __________________^
LL | |
LL | | println!("This should trigger `IS_SAME_THAN_ELSE` as usual");
LL | | } else {
| |_____^
|
note: same as this
--> $DIR/shared_at_top.rs:106:12
--> $DIR/shared_at_top.rs:105:12
|
LL | } else {
| ____________^
Expand Down
12 changes: 4 additions & 8 deletions tests/ui/branches_sharing_code/valid_if_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ fn valid_examples() {

// Let's test empty blocks
if false {
//~^ ERROR: this `if` has identical blocks
} else {
}
//~^^^ ERROR: this `if` has identical blocks
}

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

// Same block
if x == 0 {
//~^ ERROR: this `if` has identical blocks
let u = 19;
println!("How are u today?");
let _ = "This is a string";
Expand All @@ -128,6 +127,7 @@ fn trigger_other_lint() {
println!("How are u today?");
let _ = "This is a string";
}
//~^^^^^^^^^ ERROR: this `if` has identical blocks

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

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

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

if x == 0 {
println!("I'm single");
} else if x == 68 {
//~^ ERROR: this `if` has identical blocks
println!("I'm a doppelgänger");
// Don't listen to my clone below
} else {
// Don't listen to my clone above
println!("I'm a doppelgänger");
}
//~^^^^^ ERROR: this `if` has identical blocks
}

fn main() {}
18 changes: 5 additions & 13 deletions tests/ui/branches_sharing_code/valid_if_blocks.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ error: this `if` has identical blocks
|
LL | if false {
| ______________^
LL | |
LL | | } else {
| |_____^
|
note: same as this
--> $DIR/valid_if_blocks.rs:111:12
--> $DIR/valid_if_blocks.rs:110:12
|
LL | } else {
| ____________^
Expand All @@ -25,15 +24,14 @@ error: this `if` has identical blocks
|
LL | if x == 0 {
| _______________^
LL | |
LL | | let u = 19;
LL | | println!("How are u today?");
LL | | let _ = "This is a string";
LL | | } else {
| |_____^
|
note: same as this
--> $DIR/valid_if_blocks.rs:126:12
--> $DIR/valid_if_blocks.rs:125:12
|
LL | } else {
| ____________^
Expand All @@ -60,43 +58,37 @@ error: this `if` has identical blocks
|
LL | } else if x == 68 {
| _______________________^
LL | |
LL | | println!("I'm a doppelgänger");
LL | | // Don't listen to my clone below
LL | |
LL | | if y == 90 { "=^.^=" } else { ":D" }
LL | | } else {
| |_____^
|
note: same as this
--> $DIR/valid_if_blocks.rs:146:12
--> $DIR/valid_if_blocks.rs:144:12
|
LL | } else {
| ____________^
LL | | // Don't listen to my clone above
LL | | println!("I'm a doppelgänger");
LL | |
LL | | if y == 90 { "=^.^=" } else { ":D" }
LL | | };
| |_____^

error: this `if` has identical blocks
--> $DIR/valid_if_blocks.rs:155:23
--> $DIR/valid_if_blocks.rs:153:23
|
LL | } else if x == 68 {
| _______________________^
LL | |
LL | | println!("I'm a doppelgänger");
LL | | // Don't listen to my clone below
LL | | } else {
| |_____^
|
note: same as this
--> $DIR/valid_if_blocks.rs:159:12
--> $DIR/valid_if_blocks.rs:155:12
|
LL | } else {
| ____________^
LL | | // Don't listen to my clone above
LL | | println!("I'm a doppelgänger");
LL | | }
| |_____^
Expand Down
28 changes: 8 additions & 20 deletions tests/ui/if_same_then_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ fn foo() -> bool {

fn if_same_then_else() {
if true {
//~^ ERROR: this `if` has identical blocks
Foo { bar: 42 };
0..10;
..;
Expand All @@ -38,6 +37,7 @@ fn if_same_then_else() {
0..=10;
foo();
}
//~^^^^^^^^^^^^^^^^^ ERROR: this `if` has identical blocks

if true {
Foo { bar: 42 };
Expand All @@ -64,19 +64,11 @@ fn if_same_then_else() {
foo();
}

let _ = if true {
//~^ ERROR: this `if` has identical blocks
0.0
} else {
0.0
};
let _ = if true { 0.0 } else { 0.0 };
//~^ ERROR: this `if` has identical blocks

let _ = if true {
//~^ ERROR: this `if` has identical blocks
-0.0
} else {
-0.0
};
let _ = if true { -0.0 } else { -0.0 };
//~^ ERROR: this `if` has identical blocks

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

Expand All @@ -87,15 +79,10 @@ fn if_same_then_else() {
foo();
}

let _ = if true {
//~^ ERROR: this `if` has identical blocks
42
} else {
42
};
let _ = if true { 42 } else { 42 };
//~^ ERROR: this `if` has identical blocks

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

while foo() {
Expand All @@ -110,6 +97,7 @@ fn if_same_then_else() {
}
bar + 1;
}
//~^^^^^^^^^^^^^^^ ERROR: this `if` has identical blocks

if true {
let _ = match 42 {
Expand Down
Loading