mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-14 17:07:17 +00:00
Refactor suspicious_else_formatting using if_chain
This commit is contained in:
parent
96c34e85c4
commit
ad27e3ff9b
1 changed files with 32 additions and 35 deletions
|
@ -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,44 +147,40 @@ 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
|
|
||||||
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
|
// workaround for rust-lang/rust#43081
|
||||||
// the
|
if expr.span.lo().0 != 0 && expr.span.hi().0 != 0;
|
||||||
// “if” of the “else if” block (excluding)
|
|
||||||
let else_span = then.span.between(else_.span);
|
|
||||||
|
|
||||||
// the snippet should look like " else \n " with maybe comments anywhere
|
// this will be a span from the closing ‘}’ of the “then” block (excluding) to
|
||||||
// it’s bad when there is a ‘\n’ after the “else”
|
// the
|
||||||
if let Some(else_snippet) = snippet_opt(cx, else_span) {
|
// “if” of the “else if” block (excluding)
|
||||||
if let Some(else_pos) = else_snippet.find("else") {
|
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 { "{..}" };
|
|
||||||
|
|
||||||
span_note_and_lint(
|
// the snippet should look like " else \n " with maybe comments anywhere
|
||||||
cx,
|
// it’s bad when there is a ‘\n’ after the “else”
|
||||||
SUSPICIOUS_ELSE_FORMATTING,
|
if let Some(else_snippet) = snippet_opt(cx, else_span);
|
||||||
else_span,
|
if let Some(else_pos) = else_snippet.find("else");
|
||||||
&format!("this is an `else {}` but the formatting might hide it", else_desc),
|
if else_snippet[else_pos..].contains('\n');
|
||||||
else_span,
|
let else_desc = if unsugar_if(else_).is_some() { "if" } else { "{..}" };
|
||||||
&format!(
|
|
||||||
"to remove this lint, remove the `else` or remove the new line between \
|
then {
|
||||||
`else` and `{}`",
|
span_note_and_lint(
|
||||||
else_desc,
|
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,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue