Skip to content

Commit 585b21b

Browse files
committed
fix collapsable_else_if when the inner if is in parens
1 parent 25bf103 commit 585b21b

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

clippy_lints/src/collapsible_if.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ impl CollapsibleIf {
136136
return;
137137
}
138138

139+
// Peel off any parentheses.
140+
let (_, else_block_span, _) = peel_parens(cx.tcx.sess.source_map(), else_.span);
141+
139142
// Prevent "elseif"
140143
// Check that the "else" is followed by whitespace
141144
let requires_space = if let Some(c) = snippet(cx, up_to_else, "..").chars().last() {
@@ -152,7 +155,7 @@ impl CollapsibleIf {
152155
if requires_space { " " } else { "" },
153156
snippet_block_with_applicability(
154157
cx,
155-
else_.span,
158+
else_block_span,
156159
"..",
157160
Some(else_block.span),
158161
&mut applicability
@@ -298,6 +301,7 @@ fn span_extract_keyword(sm: &SourceMap, span: Span, keyword: &str) -> Option<Spa
298301
.next()
299302
}
300303

304+
/// Peel the parentheses of off an `if` expression, e.g. `((if true {} else {}))`.
301305
fn peel_parens(sm: &SourceMap, mut span: Span) -> (Span, Span, Span) {
302306
use crate::rustc_span::Pos;
303307
use rustc_span::SpanData;
@@ -330,7 +334,5 @@ fn peel_parens(sm: &SourceMap, mut span: Span) -> (Span, Span, Span) {
330334
break;
331335
}
332336

333-
(start.with_hi(span.lo()),
334-
span,
335-
end.with_lo(span.hi()))
337+
(start.with_hi(span.lo()), span, end.with_lo(span.hi()))
336338
}

tests/ui/collapsible_else_if.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,18 @@ fn issue14799() {
104104
}
105105
}
106106
}
107+
108+
fn in_parens() {
109+
let x = "hello";
110+
let y = "world";
111+
112+
if x == "hello" {
113+
print!("Hello ");
114+
} else if y == "world" {
115+
println!("world")
116+
}
117+
else {
118+
println!("!")
119+
}
120+
//~^^^^^^^^ collapsible_else_if
121+
}

tests/ui/collapsible_else_if.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,15 @@ fn issue14799() {
120120
}
121121
}
122122
}
123+
124+
fn in_parens() {
125+
let x = "hello";
126+
let y = "world";
127+
128+
if x == "hello" {
129+
print!("Hello ");
130+
} else {
131+
(if y == "world" { println!("world") } else { println!("!") })
132+
}
133+
//~^^^^^^^^ collapsible_else_if
134+
}

tests/ui/collapsible_else_if.stderr

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,27 @@ LL | | if false {}
150150
LL | | }
151151
| |_____^ help: collapse nested if block: `if false {}`
152152

153-
error: aborting due to 8 previous errors
153+
error: this `else { if .. }` block can be collapsed
154+
--> tests/ui/collapsible_else_if.rs:130:12
155+
|
156+
LL | } else {
157+
| ____________^
158+
LL | | (if y == "world" {
159+
LL | | println!("world")
160+
... |
161+
LL | | })
162+
LL | | }
163+
| |_____^
164+
|
165+
help: collapse nested if block
166+
|
167+
LL ~ } else if y == "world" {
168+
LL + println!("world")
169+
LL + }
170+
LL + else {
171+
LL + println!("!")
172+
LL + }
173+
|
174+
175+
error: aborting due to 9 previous errors
154176

0 commit comments

Comments
 (0)