Skip to content

Commit 96c34e8

Browse files
committed
Remove except in suspicious_else_formatting
This was causing two different ICEs in #3741. The first was fixed in #3925. The second one is fixed with this commit: We just don't `expect` anymore. If the snippet doesn't contain an `else`, we stop emitting the lint because it's not a suspiciously formatted else anyway.
1 parent d516925 commit 96c34e8

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

clippy_lints/src/formatting.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -165,23 +165,23 @@ fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) {
165165
// the snippet should look like " else \n " with maybe comments anywhere
166166
// it’s bad when there is a ‘\n’ after the “else”
167167
if let Some(else_snippet) = snippet_opt(cx, else_span) {
168-
let else_pos = else_snippet.find("else").expect("there must be a `else` here");
168+
if let Some(else_pos) = else_snippet.find("else") {
169+
if else_snippet[else_pos..].contains('\n') {
170+
let else_desc = if unsugar_if(else_).is_some() { "if" } else { "{..}" };
169171

170-
if else_snippet[else_pos..].contains('\n') {
171-
let else_desc = if unsugar_if(else_).is_some() { "if" } else { "{..}" };
172-
173-
span_note_and_lint(
174-
cx,
175-
SUSPICIOUS_ELSE_FORMATTING,
176-
else_span,
177-
&format!("this is an `else {}` but the formatting might hide it", else_desc),
178-
else_span,
179-
&format!(
180-
"to remove this lint, remove the `else` or remove the new line between \
181-
`else` and `{}`",
182-
else_desc,
183-
),
184-
);
172+
span_note_and_lint(
173+
cx,
174+
SUSPICIOUS_ELSE_FORMATTING,
175+
else_span,
176+
&format!("this is an `else {}` but the formatting might hide it", else_desc),
177+
else_span,
178+
&format!(
179+
"to remove this lint, remove the `else` or remove the new line between \
180+
`else` and `{}`",
181+
else_desc,
182+
),
183+
);
184+
}
185185
}
186186
}
187187
}

0 commit comments

Comments
 (0)