Skip to content

Commit

Permalink
Auto merge of #5011 - JohnTitor:split-collapsible-if, r=flip1995
Browse files Browse the repository at this point in the history
Split up `collapsible_if` ui test

Part of #2038

changelog: none
  • Loading branch information
bors committed Jan 7, 2020
2 parents cdd1347 + 175c78b commit fdccfe7
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 283 deletions.
66 changes: 66 additions & 0 deletions tests/ui/collapsible_else_if.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// run-rustfix
#![allow(clippy::cognitive_complexity, clippy::assertions_on_constants)]

#[rustfmt::skip]
#[warn(clippy::collapsible_if)]
fn main() {
let x = "hello";
let y = "world";
// Collapse `else { if .. }` to `else if ..`
if x == "hello" {
print!("Hello ");
} else if y == "world" {
println!("world!")
}

if x == "hello" {
print!("Hello ");
} else if let Some(42) = Some(42) {
println!("world!")
}

if x == "hello" {
print!("Hello ");
} else if y == "world" {
println!("world")
}
else {
println!("!")
}

if x == "hello" {
print!("Hello ");
} else if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}

if let Some(42) = Some(42) {
print!("Hello ");
} else if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}

if let Some(42) = Some(42) {
print!("Hello ");
} else if x == "hello" {
println!("world")
}
else {
println!("!")
}

if let Some(42) = Some(42) {
print!("Hello ");
} else if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}
}
80 changes: 80 additions & 0 deletions tests/ui/collapsible_else_if.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// run-rustfix
#![allow(clippy::cognitive_complexity, clippy::assertions_on_constants)]

#[rustfmt::skip]
#[warn(clippy::collapsible_if)]
fn main() {
let x = "hello";
let y = "world";
// Collapse `else { if .. }` to `else if ..`
if x == "hello" {
print!("Hello ");
} else {
if y == "world" {
println!("world!")
}
}

if x == "hello" {
print!("Hello ");
} else {
if let Some(42) = Some(42) {
println!("world!")
}
}

if x == "hello" {
print!("Hello ");
} else {
if y == "world" {
println!("world")
}
else {
println!("!")
}
}

if x == "hello" {
print!("Hello ");
} else {
if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}
}

if let Some(42) = Some(42) {
print!("Hello ");
} else {
if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}
}

if let Some(42) = Some(42) {
print!("Hello ");
} else {
if x == "hello" {
println!("world")
}
else {
println!("!")
}
}

if let Some(42) = Some(42) {
print!("Hello ");
} else {
if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}
}
}
154 changes: 154 additions & 0 deletions tests/ui/collapsible_else_if.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_else_if.rs:12:12
|
LL | } else {
| ____________^
LL | | if y == "world" {
LL | | println!("world!")
LL | | }
LL | | }
| |_____^
|
= note: `-D clippy::collapsible-if` implied by `-D warnings`
help: try
|
LL | } else if y == "world" {
LL | println!("world!")
LL | }
|

error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_else_if.rs:20:12
|
LL | } else {
| ____________^
LL | | if let Some(42) = Some(42) {
LL | | println!("world!")
LL | | }
LL | | }
| |_____^
|
help: try
|
LL | } else if let Some(42) = Some(42) {
LL | println!("world!")
LL | }
|

error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_else_if.rs:28:12
|
LL | } else {
| ____________^
LL | | if y == "world" {
LL | | println!("world")
LL | | }
... |
LL | | }
LL | | }
| |_____^
|
help: try
|
LL | } else if y == "world" {
LL | println!("world")
LL | }
LL | else {
LL | println!("!")
LL | }
|

error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_else_if.rs:39:12
|
LL | } else {
| ____________^
LL | | if let Some(42) = Some(42) {
LL | | println!("world")
LL | | }
... |
LL | | }
LL | | }
| |_____^
|
help: try
|
LL | } else if let Some(42) = Some(42) {
LL | println!("world")
LL | }
LL | else {
LL | println!("!")
LL | }
|

error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_else_if.rs:50:12
|
LL | } else {
| ____________^
LL | | if let Some(42) = Some(42) {
LL | | println!("world")
LL | | }
... |
LL | | }
LL | | }
| |_____^
|
help: try
|
LL | } else if let Some(42) = Some(42) {
LL | println!("world")
LL | }
LL | else {
LL | println!("!")
LL | }
|

error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_else_if.rs:61:12
|
LL | } else {
| ____________^
LL | | if x == "hello" {
LL | | println!("world")
LL | | }
... |
LL | | }
LL | | }
| |_____^
|
help: try
|
LL | } else if x == "hello" {
LL | println!("world")
LL | }
LL | else {
LL | println!("!")
LL | }
|

error: this `else { if .. }` block can be collapsed
--> $DIR/collapsible_else_if.rs:72:12
|
LL | } else {
| ____________^
LL | | if let Some(42) = Some(42) {
LL | | println!("world")
LL | | }
... |
LL | | }
LL | | }
| |_____^
|
help: try
|
LL | } else if let Some(42) = Some(42) {
LL | println!("world")
LL | }
LL | else {
LL | println!("!")
LL | }
|

error: aborting due to 7 previous errors

58 changes: 0 additions & 58 deletions tests/ui/collapsible_if.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -30,64 +30,6 @@ fn main() {
println!("world!")
}

// Collapse `else { if .. }` to `else if ..`
if x == "hello" {
print!("Hello ");
} else if y == "world" {
println!("world!")
}

if x == "hello" {
print!("Hello ");
} else if let Some(42) = Some(42) {
println!("world!")
}

if x == "hello" {
print!("Hello ");
} else if y == "world" {
println!("world")
}
else {
println!("!")
}

if x == "hello" {
print!("Hello ");
} else if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}

if let Some(42) = Some(42) {
print!("Hello ");
} else if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}

if let Some(42) = Some(42) {
print!("Hello ");
} else if x == "hello" {
println!("world")
}
else {
println!("!")
}

if let Some(42) = Some(42) {
print!("Hello ");
} else if let Some(42) = Some(42) {
println!("world")
}
else {
println!("!")
}

// Works because any if with an else statement cannot be collapsed.
if x == "hello" {
if y == "world" {
Expand Down
Loading

0 comments on commit fdccfe7

Please sign in to comment.