Don't lint suspicious_else_formatting
inside proc-macros
This commit is contained in:
parent
848e5518d6
commit
5efd6bc6c3
@ -286,34 +286,39 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
|
fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
|
||||||
if !differing_macro_contexts(first.span, second.span)
|
if_chain! {
|
||||||
&& !first.span.from_expansion()
|
if !differing_macro_contexts(first.span, second.span);
|
||||||
&& is_if(first)
|
if !first.span.from_expansion();
|
||||||
&& (is_block(second) || is_if(second))
|
if let ExprKind::If(cond_expr, ..) = &first.kind;
|
||||||
{
|
if is_block(second) || is_if(second);
|
||||||
// where the else would be
|
|
||||||
|
// Proc-macros can give weird spans. Make sure this is actually an `if`.
|
||||||
|
if let Some(if_snip) = snippet_opt(cx, first.span.until(cond_expr.span));
|
||||||
|
if if_snip.starts_with("if");
|
||||||
|
|
||||||
|
// If there is a line break between the two expressions, don't lint.
|
||||||
|
// If there is a non-whitespace character, this span came from a proc-macro.
|
||||||
let else_span = first.span.between(second.span);
|
let else_span = first.span.between(second.span);
|
||||||
|
if let Some(else_snippet) = snippet_opt(cx, else_span);
|
||||||
|
if !else_snippet.chars().any(|c| c == '\n' || !c.is_whitespace());
|
||||||
|
then {
|
||||||
|
let (looks_like, next_thing) = if is_if(second) {
|
||||||
|
("an `else if`", "the second `if`")
|
||||||
|
} else {
|
||||||
|
("an `else {..}`", "the next block")
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(else_snippet) = snippet_opt(cx, else_span) {
|
span_lint_and_note(
|
||||||
if !else_snippet.contains('\n') {
|
cx,
|
||||||
let (looks_like, next_thing) = if is_if(second) {
|
SUSPICIOUS_ELSE_FORMATTING,
|
||||||
("an `else if`", "the second `if`")
|
else_span,
|
||||||
} else {
|
&format!("this looks like {} but the `else` is missing", looks_like),
|
||||||
("an `else {..}`", "the next block")
|
None,
|
||||||
};
|
&format!(
|
||||||
|
"to remove this lint, add the missing `else` or add a new line before {}",
|
||||||
span_lint_and_note(
|
next_thing,
|
||||||
cx,
|
),
|
||||||
SUSPICIOUS_ELSE_FORMATTING,
|
);
|
||||||
else_span,
|
|
||||||
&format!("this looks like {} but the `else` is missing", looks_like),
|
|
||||||
None,
|
|
||||||
&format!(
|
|
||||||
"to remove this lint, add the missing `else` or add a new line before {}",
|
|
||||||
next_thing,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user