diff --git a/clippy_lints/src/formatting.rs b/clippy_lints/src/formatting.rs index 48612befc68d..99034ec53e09 100644 --- a/clippy_lints/src/formatting.rs +++ b/clippy_lints/src/formatting.rs @@ -200,7 +200,7 @@ fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) { /// Implementation of the `SUSPICIOUS_ELSE_FORMATTING` lint for weird `else`. fn check_else(cx: &EarlyContext<'_>, expr: &Expr) { if_chain! { - if let ExprKind::If(_, then, Some(else_)) = &expr.kind; + if let ExprKind::If(cond, then, Some(else_)) = &expr.kind; if is_block(else_) || is_if(else_); if !differing_macro_contexts(then.span, else_.span); if !then.span.from_expansion() && !in_external_macro(cx.sess, expr.span); @@ -218,6 +218,17 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) { if let Some(else_pos) = else_snippet.find("else"); if else_snippet[else_pos..].contains('\n'); then { + // Allow allman style braces `if .. \n { \n .. \n } \n else \n { \n .. \n }` + if_chain! { + if is_block(else_); + if else_snippet.starts_with('\n'); + if let Some(if_snippet) = snippet_opt(cx, cond.span.between(then.span)); + if if_snippet.contains('\n'); + then { + return; + } + } + let else_desc = if is_if(else_) { "if" } else { "{..}" }; span_lint_and_note( cx, diff --git a/tests/ui/suspicious_else_formatting.rs b/tests/ui/suspicious_else_formatting.rs index 226010ec6df3..4eea9d13886f 100644 --- a/tests/ui/suspicious_else_formatting.rs +++ b/tests/ui/suspicious_else_formatting.rs @@ -76,4 +76,14 @@ fn main() { } if foo() { } + + // #3864 - allman style braces + if foo() + { + // stuff + } + else + { + // other stuff + } }