Refine the logic to accurately assess if 'else' resides within comments

This commit is contained in:
granddaifuku 2024-03-26 00:46:57 +09:00
parent 2a62200b8d
commit b9da637655

View file

@ -214,7 +214,8 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
// the snippet should look like " else \n " with maybe comments anywhere
// its 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
}