Skip to content

Commit 37e838f

Browse files
committed
Use new util function in suspicious_else_formatting
1 parent 2ae8b30 commit 37e838f

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

clippy_lints/src/formatting.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note};
2+
use clippy_utils::is_span_if;
23
use clippy_utils::source::snippet_opt;
34
use if_chain::if_chain;
45
use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp};
@@ -297,12 +298,11 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
297298
fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
298299
if_chain! {
299300
if !first.span.from_expansion() && !second.span.from_expansion();
300-
if let ExprKind::If(cond_expr, ..) = &first.kind;
301+
if matches!(first.kind, ExprKind::If(..));
301302
if is_block(second) || is_if(second);
302303

303304
// Proc-macros can give weird spans. Make sure this is actually an `if`.
304-
if let Some(if_snip) = snippet_opt(cx, first.span.until(cond_expr.span));
305-
if if_snip.starts_with("if");
305+
if is_span_if(cx, first.span);
306306

307307
// If there is a line break between the two expressions, don't lint.
308308
// If there is a non-whitespace character, this span came from a proc-macro.

clippy_utils/src/check_proc_macro.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ pub fn is_expr_from_proc_macro(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
162162
}
163163

164164
/// Checks if the span actually refers to a match expression
165-
pub fn is_span_match(cx: &LateContext<'_>, span: Span) -> bool {
165+
pub fn is_span_match(cx: &impl LintContext, span: Span) -> bool {
166166
span_matches_pat(cx.sess(), span, Pat::Str("match"), Pat::Str("}"))
167167
}
168+
169+
/// Checks if the span actually refers to an if expression
170+
pub fn is_span_if(cx: &impl LintContext, span: Span) -> bool {
171+
span_matches_pat(cx.sess(), span, Pat::Str("if"), Pat::Str("}"))
172+
}

clippy_utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub mod usage;
5959
pub mod visitors;
6060

6161
pub use self::attrs::*;
62-
pub use self::check_proc_macro::{is_expr_from_proc_macro, is_span_match};
62+
pub use self::check_proc_macro::{is_expr_from_proc_macro, is_span_if, is_span_match};
6363
pub use self::hir_utils::{
6464
both, count_eq, eq_expr_value, hash_expr, hash_stmt, over, HirEqInterExpr, SpanlessEq, SpanlessHash,
6565
};

0 commit comments

Comments
 (0)