about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgranddaifuku <daifukuformac@gmail.com>2024-03-26 00:46:57 +0900
committergranddaifuku <daifukuformac@gmail.com>2024-03-26 00:46:57 +0900
commitb9da637655c4e1c66dde2fb291ae4786b2b7e057 (patch)
tree11d6e84dce14ca1ab81f2a004d39964db8795f38
parent2a62200b8d5ba65d583199a296c3fbbf8e58aa25 (diff)
downloadrust-b9da637655c4e1c66dde2fb291ae4786b2b7e057.tar.gz
rust-b9da637655c4e1c66dde2fb291ae4786b2b7e057.zip
Refine the logic to accurately assess if 'else' resides within comments
-rw-r--r--clippy_lints/src/formatting.rs44
1 files changed, 2 insertions, 42 deletions
diff --git a/clippy_lints/src/formatting.rs b/clippy_lints/src/formatting.rs
index 5b8d4ea8956..5f8d787357d 100644
--- a/clippy_lints/src/formatting.rs
+++ b/clippy_lints/src/formatting.rs
@@ -214,7 +214,8 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
         // the snippet should look like " else \n    " with maybe comments anywhere
         // 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)) = split_once_with_else(&else_snippet)
+        && 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 {`
@@ -323,44 +324,3 @@ fn is_block(expr: &Expr) -> bool {
 fn is_if(expr: &Expr) -> bool {
     matches!(expr.kind, ExprKind::If(..))
 }
-
-fn split_once_with_else(base: &str) -> Option<(&str, &str)> {
-    let else_str = "else";
-
-    let indices: Vec<_> = base.match_indices(else_str).map(|(i, _)| i).collect();
-
-    match indices.len() {
-        0 => return None,
-        1 => return base.split_once(else_str),
-        _ => {},
-    }
-
-    let mut i = 0;
-    let mut is_in_comment = false;
-
-    for line in base.lines() {
-        if let Some(else_pos) = line.find(else_str) {
-            if let Some(pos) = line.find("//") {
-                if pos > else_pos {
-                    return Some(base.split_at(indices[i]));
-                }
-            } else if let Some(pos) = line.find("/*") {
-                if pos > else_pos {
-                    return Some(base.split_at(indices[i]));
-                }
-                is_in_comment = true;
-            } else if let Some(pos) = line.find("*/") {
-                if pos < else_pos {
-                    return Some(base.split_at(indices[i]));
-                }
-                is_in_comment = false;
-            } else if !is_in_comment {
-                return Some(base.split_at(indices[i]));
-            }
-
-            i += 1;
-        }
-    }
-
-    None
-}