diff options
| author | bors <bors@rust-lang.org> | 2024-03-25 16:51:43 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-03-25 16:51:43 +0000 |
| commit | c3948d16b928616407f1eac19bfea9ba9e2df16e (patch) | |
| tree | 780e0ff9e5b6025749b3f46fe66e933f0ca2508a | |
| parent | be27f68d26042408b705dd00bce414966000cadc (diff) | |
| parent | b9da637655c4e1c66dde2fb291ae4786b2b7e057 (diff) | |
| download | rust-c3948d16b928616407f1eac19bfea9ba9e2df16e.tar.gz rust-c3948d16b928616407f1eac19bfea9ba9e2df16e.zip | |
Auto merge of #12549 - granddaifuku:fix/suspicious_else_formatting-false-positive-when-commented-else, r=Alexendoo
fix: `suspicious_else_formatting` false positive when else is included … This PR addresses an issue where invalid suggestions are generated for `if-else` formatting if comments contain the keyword `else`. The root of the problem is identified [here](https://github.com/rust-lang/rust-clippy/blob/95c62ffae9bbce793f68a6f1473e3fc24af19bdd/clippy_lints/src/formatting.rs#L217). Specifically, when a comment contains the word `else`, the lint mistakenly interprets it as part of an `if-else` clause. This misinterpretation leads to an incorrect splitting of the snippet, resulting in erroneous suggestions. fixes: #12497 changelog: [`suspicious_else_formatting`]: Fixes invalid suggestions when comments include word else
| -rw-r--r-- | clippy_lints/src/formatting.rs | 1 | ||||
| -rw-r--r-- | tests/ui/suspicious_else_formatting.rs | 28 |
2 files changed, 29 insertions, 0 deletions
diff --git a/clippy_lints/src/formatting.rs b/clippy_lints/src/formatting.rs index c3ef6f180c9..5f8d787357d 100644 --- a/clippy_lints/src/formatting.rs +++ b/clippy_lints/src/formatting.rs @@ -215,6 +215,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) { // it’s bad when there is a ‘\n’ after the “else” && let Some(else_snippet) = snippet_opt(cx, else_span) && let Some((pre_else, post_else)) = else_snippet.split_once("else") + && !else_snippet.contains('/') && let Some((_, post_else_post_eol)) = post_else.split_once('\n') { // Allow allman style braces `} \n else \n {` diff --git a/tests/ui/suspicious_else_formatting.rs b/tests/ui/suspicious_else_formatting.rs index c0856427eae..3d5c892eb60 100644 --- a/tests/ui/suspicious_else_formatting.rs +++ b/tests/ui/suspicious_else_formatting.rs @@ -120,6 +120,34 @@ fn main() { /* whelp */ { } + + // #12497 Don't trigger lint as rustfmt wants it + if true { + println!("true"); + } + /*else if false { +}*/ + else { + println!("false"); + } + + if true { + println!("true"); + } // else if false {} + else { + println!("false"); + } + + if true { + println!("true"); + } /* if true { + println!("true"); +} + */ + else { + println!("false"); + } + } // #7650 - Don't lint. Proc-macro using bad spans for `if` expressions. |
