From 37e838f759b99dd3ac042a61f60a4f14272b99c2 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Wed, 13 Apr 2022 01:13:47 -0400 Subject: [PATCH] Use new util function in `suspicious_else_formatting` --- clippy_lints/src/formatting.rs | 6 +++--- clippy_utils/src/check_proc_macro.rs | 7 ++++++- clippy_utils/src/lib.rs | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/formatting.rs b/clippy_lints/src/formatting.rs index db0166da57f..01cefe4af85 100644 --- a/clippy_lints/src/formatting.rs +++ b/clippy_lints/src/formatting.rs @@ -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. diff --git a/clippy_utils/src/check_proc_macro.rs b/clippy_utils/src/check_proc_macro.rs index 387bf720008..51cd70f5e7c 100644 --- a/clippy_utils/src/check_proc_macro.rs +++ b/clippy_utils/src/check_proc_macro.rs @@ -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("}")) +} diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 2d051e3a815..086e6d6e3a2 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -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, };