Skip to content

Commit

Permalink
Allow allman style braces in suspicious_else_formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Apr 15, 2021
1 parent b1c675f commit 10ad06e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion clippy_lints/src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/suspicious_else_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,14 @@ fn main() {
}
if foo() {
}

// #3864 - allman style braces
if foo()
{
// stuff
}
else
{
// other stuff
}
}

0 comments on commit 10ad06e

Please sign in to comment.