Skip to content

Commit feb18ca

Browse files
authored
clean-up collapsible_if a bit (#15503)
changelog: none
2 parents fc42a20 + 8418fb8 commit feb18ca

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

clippy_lints/src/collapsible_if.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::source::{IntoSpan as _, SpanRangeExt, snippet, snippet_block_w
55
use clippy_utils::{span_contains_non_whitespace, tokenize_with_text};
66
use rustc_ast::BinOpKind;
77
use rustc_errors::Applicability;
8-
use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind};
8+
use rustc_hir::{Block, Expr, ExprKind, StmtKind};
99
use rustc_lexer::TokenKind;
1010
use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_session::impl_lint_pass;
@@ -141,11 +141,7 @@ impl CollapsibleIf {
141141

142142
// Prevent "elseif"
143143
// Check that the "else" is followed by whitespace
144-
let requires_space = if let Some(c) = snippet(cx, up_to_else, "..").chars().last() {
145-
!c.is_whitespace()
146-
} else {
147-
false
148-
};
144+
let requires_space = snippet(cx, up_to_else, "..").ends_with(|c: char| !c.is_whitespace());
149145
let mut applicability = Applicability::MachineApplicable;
150146
diag.span_suggestion(
151147
else_block.span,
@@ -173,8 +169,7 @@ impl CollapsibleIf {
173169
&& cx.tcx.hir_attrs(inner.hir_id).is_empty()
174170
&& let ExprKind::If(check_inner, _, None) = &inner.kind
175171
&& self.eligible_condition(cx, check_inner)
176-
&& let ctxt = expr.span.ctxt()
177-
&& inner.span.ctxt() == ctxt
172+
&& expr.span.eq_ctxt(inner.span)
178173
&& !block_starts_with_significant_tokens(cx, then, inner, self.lint_commented_code)
179174
{
180175
span_lint_and_then(
@@ -262,14 +257,9 @@ fn block_starts_with_significant_tokens(
262257
/// If `block` is a block with either one expression or a statement containing an expression,
263258
/// return the expression. We don't peel blocks recursively, as extra blocks might be intentional.
264259
fn expr_block<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<'tcx>> {
265-
match block.stmts {
266-
[] => block.expr,
267-
[
268-
Stmt {
269-
kind: StmtKind::Semi(expr),
270-
..
271-
},
272-
] if block.expr.is_none() => Some(expr),
260+
match (block.stmts, block.expr) {
261+
([], expr) => expr,
262+
([stmt], None) if let StmtKind::Semi(expr) = stmt.kind => Some(expr),
273263
_ => None,
274264
}
275265
}

0 commit comments

Comments
 (0)