Skip to content

Commit 3b43bba

Browse files
committed
Use new util function in suspicious_else_formatting
1 parent 95b1489 commit 3b43bba

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};
@@ -291,12 +292,11 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
291292
fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
292293
if_chain! {
293294
if !first.span.from_expansion() && !second.span.from_expansion();
294-
if let ExprKind::If(cond_expr, ..) = &first.kind;
295+
if matches!(first.kind, ExprKind::If(..));
295296
if is_block(second) || is_if(second);
296297

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

301301
// If there is a line break between the two expressions, don't lint.
302302
// 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
@@ -161,6 +161,11 @@ pub fn is_expr_from_proc_macro(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
161161
}
162162

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

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::{both, count_eq, eq_expr_value, over, SpanlessEq, SpanlessHash};
6464

6565
use std::collections::hash_map::Entry;

0 commit comments

Comments
 (0)