Refactor suspicious_else_formatting using if_chain

This commit is contained in:
Philipp Hansch 2019-04-14 11:12:51 +02:00
parent 96c34e85c4
commit ad27e3ff9b
No known key found for this signature in database
GPG Key ID: 82AA61CAA11397E6

View File

@ -3,6 +3,7 @@ use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, Lin
use rustc::{declare_tool_lint, lint_array}; use rustc::{declare_tool_lint, lint_array};
use syntax::ast; use syntax::ast;
use syntax::ptr::P; use syntax::ptr::P;
use if_chain::if_chain;
declare_clippy_lint! { declare_clippy_lint! {
/// **What it does:** Checks for use of the non-existent `=*`, `=!` and `=-` /// **What it does:** Checks for use of the non-existent `=*`, `=!` and `=-`
@ -146,16 +147,14 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &ast::Expr) {
/// Implementation of the `SUSPICIOUS_ELSE_FORMATTING` lint for weird `else`. /// Implementation of the `SUSPICIOUS_ELSE_FORMATTING` lint for weird `else`.
fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) { fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) {
if let Some((then, &Some(ref else_))) = unsugar_if(expr) { if_chain! {
if (is_block(else_) || unsugar_if(else_).is_some()) if let Some((then, &Some(ref else_))) = unsugar_if(expr);
&& !differing_macro_contexts(then.span, else_.span) if is_block(else_) || unsugar_if(else_).is_some();
&& !in_macro(then.span) if !differing_macro_contexts(then.span, else_.span);
&& !in_external_macro(cx.sess, expr.span) if !in_macro(then.span) && !in_external_macro(cx.sess, expr.span);
{
// workaround for rust-lang/rust#43081 // workaround for rust-lang/rust#43081
if expr.span.lo().0 == 0 && expr.span.hi().0 == 0 { if expr.span.lo().0 != 0 && expr.span.hi().0 != 0;
return;
}
// this will be a span from the closing } of the “then” block (excluding) to // this will be a span from the closing } of the “then” block (excluding) to
// the // the
@ -164,11 +163,12 @@ fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) {
// the snippet should look like " else \n " with maybe comments anywhere // the snippet should look like " else \n " with maybe comments anywhere
// its bad when there is a \n after the “else” // its bad when there is a \n after the “else”
if let Some(else_snippet) = snippet_opt(cx, else_span) { if let Some(else_snippet) = snippet_opt(cx, else_span);
if let Some(else_pos) = else_snippet.find("else") { if let Some(else_pos) = else_snippet.find("else");
if else_snippet[else_pos..].contains('\n') { if else_snippet[else_pos..].contains('\n');
let else_desc = if unsugar_if(else_).is_some() { "if" } else { "{..}" }; let else_desc = if unsugar_if(else_).is_some() { "if" } else { "{..}" };
then {
span_note_and_lint( span_note_and_lint(
cx, cx,
SUSPICIOUS_ELSE_FORMATTING, SUSPICIOUS_ELSE_FORMATTING,
@ -184,9 +184,6 @@ fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) {
} }
} }
} }
}
}
}
fn has_unary_equivalent(bin_op: ast::BinOpKind) -> bool { fn has_unary_equivalent(bin_op: ast::BinOpKind) -> bool {
// &, *, - // &, *, -