Skip to content

Commit b9da637

Browse files
committed
Refine the logic to accurately assess if 'else' resides within comments
1 parent 2a62200 commit b9da637

File tree

1 file changed

+2
-42
lines changed

1 file changed

+2
-42
lines changed

clippy_lints/src/formatting.rs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
214214
// the snippet should look like " else \n " with maybe comments anywhere
215215
// it’s bad when there is a ‘\n’ after the “else”
216216
&& let Some(else_snippet) = snippet_opt(cx, else_span)
217-
&& let Some((pre_else, post_else)) = split_once_with_else(&else_snippet)
217+
&& let Some((pre_else, post_else)) = else_snippet.split_once("else")
218+
&& !else_snippet.contains('/')
218219
&& let Some((_, post_else_post_eol)) = post_else.split_once('\n')
219220
{
220221
// Allow allman style braces `} \n else \n {`
@@ -323,44 +324,3 @@ fn is_block(expr: &Expr) -> bool {
323324
fn is_if(expr: &Expr) -> bool {
324325
matches!(expr.kind, ExprKind::If(..))
325326
}
326-
327-
fn split_once_with_else(base: &str) -> Option<(&str, &str)> {
328-
let else_str = "else";
329-
330-
let indices: Vec<_> = base.match_indices(else_str).map(|(i, _)| i).collect();
331-
332-
match indices.len() {
333-
0 => return None,
334-
1 => return base.split_once(else_str),
335-
_ => {},
336-
}
337-
338-
let mut i = 0;
339-
let mut is_in_comment = false;
340-
341-
for line in base.lines() {
342-
if let Some(else_pos) = line.find(else_str) {
343-
if let Some(pos) = line.find("//") {
344-
if pos > else_pos {
345-
return Some(base.split_at(indices[i]));
346-
}
347-
} else if let Some(pos) = line.find("/*") {
348-
if pos > else_pos {
349-
return Some(base.split_at(indices[i]));
350-
}
351-
is_in_comment = true;
352-
} else if let Some(pos) = line.find("*/") {
353-
if pos < else_pos {
354-
return Some(base.split_at(indices[i]));
355-
}
356-
is_in_comment = false;
357-
} else if !is_in_comment {
358-
return Some(base.split_at(indices[i]));
359-
}
360-
361-
i += 1;
362-
}
363-
}
364-
365-
None
366-
}

0 commit comments

Comments
 (0)