Use new util function in suspicious_else_formatting

This commit is contained in:
Jason Newcomb 2022-04-13 01:13:47 -04:00
parent 2ae8b300a7
commit 37e838f759
3 changed files with 10 additions and 5 deletions

View File

@ -1,4 +1,5 @@
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note};
use clippy_utils::is_span_if;
use clippy_utils::source::snippet_opt;
use if_chain::if_chain;
use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp};
@ -297,12 +298,11 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
if_chain! {
if !first.span.from_expansion() && !second.span.from_expansion();
if let ExprKind::If(cond_expr, ..) = &first.kind;
if matches!(first.kind, ExprKind::If(..));
if is_block(second) || is_if(second);
// 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 is_span_if(cx, first.span);
// 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.

View File

@ -162,6 +162,11 @@ pub fn is_expr_from_proc_macro(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
}
/// Checks if the span actually refers to a match expression
pub fn is_span_match(cx: &LateContext<'_>, span: Span) -> bool {
pub fn is_span_match(cx: &impl LintContext, span: Span) -> bool {
span_matches_pat(cx.sess(), span, Pat::Str("match"), Pat::Str("}"))
}
/// Checks if the span actually refers to an if expression
pub fn is_span_if(cx: &impl LintContext, span: Span) -> bool {
span_matches_pat(cx.sess(), span, Pat::Str("if"), Pat::Str("}"))
}

View File

@ -59,7 +59,7 @@ pub mod usage;
pub mod visitors;
pub use self::attrs::*;
pub use self::check_proc_macro::{is_expr_from_proc_macro, is_span_match};
pub use self::check_proc_macro::{is_expr_from_proc_macro, is_span_if, is_span_match};
pub use self::hir_utils::{
both, count_eq, eq_expr_value, hash_expr, hash_stmt, over, HirEqInterExpr, SpanlessEq, SpanlessHash,
};