Skip to content

Commit ad27e3f

Browse files
committed
Refactor suspicious_else_formatting using if_chain
1 parent 96c34e8 commit ad27e3f

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

clippy_lints/src/formatting.rs

+32-35
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, Lin
33
use rustc::{declare_tool_lint, lint_array};
44
use syntax::ast;
55
use syntax::ptr::P;
6+
use if_chain::if_chain;
67

78
declare_clippy_lint! {
89
/// **What it does:** Checks for use of the non-existent `=*`, `=!` and `=-`
@@ -146,44 +147,40 @@ 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-
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 { "{..}" };
159+
// this will be a span from the closing ‘}’ of the “then” block (excluding) to
160+
// the
161+
// “if” of the “else if” block (excluding)
162+
let else_span = then.span.between(else_.span);
171163

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-
}
185-
}
186-
}
164+
// the snippet should look like " else \n " with maybe comments anywhere
165+
// it’s bad when there is a ‘\n’ after the “else”
166+
if let Some(else_snippet) = snippet_opt(cx, else_span);
167+
if let Some(else_pos) = else_snippet.find("else");
168+
if else_snippet[else_pos..].contains('\n');
169+
let else_desc = if unsugar_if(else_).is_some() { "if" } else { "{..}" };
170+
171+
then {
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+
);
187184
}
188185
}
189186
}

0 commit comments

Comments
 (0)