Skip to content

Commit 559dedd

Browse files
committed
Allow allman style braces in suspicious_else_formatting
1 parent 586a993 commit 559dedd

File tree

3 files changed

+67
-15
lines changed

3 files changed

+67
-15
lines changed

clippy_lints/src/formatting.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,22 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
215215
// the snippet should look like " else \n " with maybe comments anywhere
216216
// it’s bad when there is a ‘\n’ after the “else”
217217
if let Some(else_snippet) = snippet_opt(cx, else_span);
218-
if let Some(else_pos) = else_snippet.find("else");
219-
if else_snippet[else_pos..].contains('\n');
218+
if let Some((pre_else, post_else)) = else_snippet.split_once("else");
219+
if let Some((_, post_else_post_eol)) = post_else.split_once('\n');
220+
220221
then {
222+
// Allow allman style braces `} \n else \n {`
223+
if_chain! {
224+
if is_block(else_);
225+
if let Some((_, pre_else_post_eol)) = pre_else.split_once('\n');
226+
// Exactly one eol before and after the else
227+
if !pre_else_post_eol.contains('\n');
228+
if !post_else_post_eol.contains('\n');
229+
then {
230+
return;
231+
}
232+
}
233+
221234
let else_desc = if is_if(else_) { "if" } else { "{..}" };
222235
span_lint_and_note(
223236
cx,

tests/ui/suspicious_else_formatting.rs

+26
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn main() {
4040
{
4141
}
4242

43+
// This is fine, though weird. Allman style braces on the else.
4344
if foo() {
4445
}
4546
else
@@ -76,4 +77,29 @@ fn main() {
7677
}
7778
if foo() {
7879
}
80+
81+
// Almost Allman style braces. Lint these.
82+
if foo() {
83+
}
84+
85+
else
86+
{
87+
88+
}
89+
90+
if foo() {
91+
}
92+
else
93+
94+
{
95+
96+
}
97+
98+
// #3864 - Allman style braces
99+
if foo()
100+
{
101+
}
102+
else
103+
{
104+
}
79105
}

tests/ui/suspicious_else_formatting.stderr

+26-13
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,50 @@ LL | | {
4141
|
4242
= note: to remove this lint, remove the `else` or remove the new line between `else` and `{..}`
4343

44-
error: this is an `else {..}` but the formatting might hide it
45-
--> $DIR/suspicious_else_formatting.rs:44:6
44+
error: this is an `else if` but the formatting might hide it
45+
--> $DIR/suspicious_else_formatting.rs:51:6
4646
|
47-
LL | }
47+
LL | } else
4848
| ______^
49-
LL | | else
50-
LL | | {
49+
LL | | if foo() { // the span of the above error should continue here
5150
| |____^
5251
|
53-
= note: to remove this lint, remove the `else` or remove the new line between `else` and `{..}`
52+
= note: to remove this lint, remove the `else` or remove the new line between `else` and `if`
5453

5554
error: this is an `else if` but the formatting might hide it
56-
--> $DIR/suspicious_else_formatting.rs:50:6
55+
--> $DIR/suspicious_else_formatting.rs:56:6
5756
|
58-
LL | } else
57+
LL | }
5958
| ______^
59+
LL | | else
6060
LL | | if foo() { // the span of the above error should continue here
6161
| |____^
6262
|
6363
= note: to remove this lint, remove the `else` or remove the new line between `else` and `if`
6464

65-
error: this is an `else if` but the formatting might hide it
66-
--> $DIR/suspicious_else_formatting.rs:55:6
65+
error: this is an `else {..}` but the formatting might hide it
66+
--> $DIR/suspicious_else_formatting.rs:83:6
6767
|
6868
LL | }
6969
| ______^
70+
LL | |
7071
LL | | else
71-
LL | | if foo() { // the span of the above error should continue here
72+
LL | | {
7273
| |____^
7374
|
74-
= note: to remove this lint, remove the `else` or remove the new line between `else` and `if`
75+
= note: to remove this lint, remove the `else` or remove the new line between `else` and `{..}`
76+
77+
error: this is an `else {..}` but the formatting might hide it
78+
--> $DIR/suspicious_else_formatting.rs:91:6
79+
|
80+
LL | }
81+
| ______^
82+
LL | | else
83+
LL | |
84+
LL | | {
85+
| |____^
86+
|
87+
= note: to remove this lint, remove the `else` or remove the new line between `else` and `{..}`
7588

76-
error: aborting due to 8 previous errors
89+
error: aborting due to 9 previous errors
7790

0 commit comments

Comments
 (0)