Skip to content

Commit 10ad06e

Browse files
committed
Allow allman style braces in suspicious_else_formatting
1 parent b1c675f commit 10ad06e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

clippy_lints/src/formatting.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) {
200200
/// Implementation of the `SUSPICIOUS_ELSE_FORMATTING` lint for weird `else`.
201201
fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
202202
if_chain! {
203-
if let ExprKind::If(_, then, Some(else_)) = &expr.kind;
203+
if let ExprKind::If(cond, then, Some(else_)) = &expr.kind;
204204
if is_block(else_) || is_if(else_);
205205
if !differing_macro_contexts(then.span, else_.span);
206206
if !then.span.from_expansion() && !in_external_macro(cx.sess, expr.span);
@@ -218,6 +218,17 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
218218
if let Some(else_pos) = else_snippet.find("else");
219219
if else_snippet[else_pos..].contains('\n');
220220
then {
221+
// Allow allman style braces `if .. \n { \n .. \n } \n else \n { \n .. \n }`
222+
if_chain! {
223+
if is_block(else_);
224+
if else_snippet.starts_with('\n');
225+
if let Some(if_snippet) = snippet_opt(cx, cond.span.between(then.span));
226+
if if_snippet.contains('\n');
227+
then {
228+
return;
229+
}
230+
}
231+
221232
let else_desc = if is_if(else_) { "if" } else { "{..}" };
222233
span_lint_and_note(
223234
cx,

tests/ui/suspicious_else_formatting.rs

+10
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,14 @@ fn main() {
7676
}
7777
if foo() {
7878
}
79+
80+
// #3864 - allman style braces
81+
if foo()
82+
{
83+
// stuff
84+
}
85+
else
86+
{
87+
// other stuff
88+
}
7989
}

0 commit comments

Comments
 (0)