Skip to content

Commit 6505794

Browse files
committed
Auto merge of #3960 - phansch:fix_except, r=flip1995
Remove `except` in suspicious_else_formatting 96c34e8 contains the fix: 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. Unfortunately I wasn't able to provide a minimal test case, but I think it's fine since it's just ignoring the `None` case now. And ad27e3f cleans up the lint code to use `if_chain`. Fixes #3741 once more.
2 parents 422ae77 + 289a9af commit 6505794

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

clippy_lints/src/formatting.rs

+30-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::utils::{differing_macro_contexts, in_macro, snippet_opt, span_note_and_lint};
2+
use if_chain::if_chain;
23
use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintPass};
34
use rustc::{declare_tool_lint, lint_array};
45
use syntax::ast;
@@ -146,44 +147,39 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &ast::Expr) {
146147

147148
/// Implementation of the `SUSPICIOUS_ELSE_FORMATTING` lint for weird `else`.
148149
fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) {
149-
if let Some((then, &Some(ref else_))) = unsugar_if(expr) {
150-
if (is_block(else_) || unsugar_if(else_).is_some())
151-
&& !differing_macro_contexts(then.span, else_.span)
152-
&& !in_macro(then.span)
153-
&& !in_external_macro(cx.sess, expr.span)
154-
{
155-
// workaround for rust-lang/rust#43081
156-
if expr.span.lo().0 == 0 && expr.span.hi().0 == 0 {
157-
return;
158-
}
150+
if_chain! {
151+
if let Some((then, &Some(ref else_))) = unsugar_if(expr);
152+
if is_block(else_) || unsugar_if(else_).is_some();
153+
if !differing_macro_contexts(then.span, else_.span);
154+
if !in_macro(then.span) && !in_external_macro(cx.sess, expr.span);
159155

160-
// this will be a span from the closing ‘}’ of the “then” block (excluding) to
161-
// the
162-
// “if” of the “else if” block (excluding)
163-
let else_span = then.span.between(else_.span);
156+
// workaround for rust-lang/rust#43081
157+
if expr.span.lo().0 != 0 && expr.span.hi().0 != 0;
164158

165-
// the snippet should look like " else \n " with maybe comments anywhere
166-
// it’s bad when there is a ‘\n’ after the “else”
167-
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");
159+
// this will be a span from the closing ‘}’ of the “then” block (excluding) to
160+
// the “if” of the “else if” block (excluding)
161+
let else_span = then.span.between(else_.span);
169162

170-
if else_snippet[else_pos..].contains('\n') {
171-
let else_desc = if unsugar_if(else_).is_some() { "if" } else { "{..}" };
163+
// the snippet should look like " else \n " with maybe comments anywhere
164+
// it’s bad when there is a ‘\n’ after the “else”
165+
if let Some(else_snippet) = snippet_opt(cx, else_span);
166+
if let Some(else_pos) = else_snippet.find("else");
167+
if else_snippet[else_pos..].contains('\n');
168+
let else_desc = if unsugar_if(else_).is_some() { "if" } else { "{..}" };
172169

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-
);
185-
}
186-
}
170+
then {
171+
span_note_and_lint(
172+
cx,
173+
SUSPICIOUS_ELSE_FORMATTING,
174+
else_span,
175+
&format!("this is an `else {}` but the formatting might hide it", else_desc),
176+
else_span,
177+
&format!(
178+
"to remove this lint, remove the `else` or remove the new line between \
179+
`else` and `{}`",
180+
else_desc,
181+
),
182+
);
187183
}
188184
}
189185
}

0 commit comments

Comments
 (0)