Skip to content

Commit

Permalink
Auto merge of #3960 - phansch:fix_except, r=flip1995
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Apr 14, 2019
2 parents 422ae77 + 289a9af commit 6505794
Showing 1 changed file with 30 additions and 34 deletions.
64 changes: 30 additions & 34 deletions clippy_lints/src/formatting.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::utils::{differing_macro_contexts, in_macro, snippet_opt, span_note_and_lint};
use if_chain::if_chain;
use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintPass};
use rustc::{declare_tool_lint, lint_array};
use syntax::ast;
Expand Down Expand Up @@ -146,44 +147,39 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &ast::Expr) {

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

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

// the snippet should look like " else \n " with maybe comments anywhere
// it’s bad when there is a ‘\n’ after the “else”
if let Some(else_snippet) = snippet_opt(cx, else_span) {
let else_pos = else_snippet.find("else").expect("there must be a `else` here");
// this will be a span from the closing ‘}’ of the “then” block (excluding) to
// the “if” of the “else if” block (excluding)
let else_span = then.span.between(else_.span);

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

span_note_and_lint(
cx,
SUSPICIOUS_ELSE_FORMATTING,
else_span,
&format!("this is an `else {}` but the formatting might hide it", else_desc),
else_span,
&format!(
"to remove this lint, remove the `else` or remove the new line between \
`else` and `{}`",
else_desc,
),
);
}
}
then {
span_note_and_lint(
cx,
SUSPICIOUS_ELSE_FORMATTING,
else_span,
&format!("this is an `else {}` but the formatting might hide it", else_desc),
else_span,
&format!(
"to remove this lint, remove the `else` or remove the new line between \
`else` and `{}`",
else_desc,
),
);
}
}
}
Expand Down

0 comments on commit 6505794

Please sign in to comment.