@@ -5,7 +5,7 @@ use clippy_utils::source::{IntoSpan as _, SpanRangeExt, snippet, snippet_block_w
55use clippy_utils:: { span_contains_non_whitespace, tokenize_with_text} ;
66use rustc_ast:: BinOpKind ;
77use rustc_errors:: Applicability ;
8- use rustc_hir:: { Block , Expr , ExprKind , Stmt , StmtKind } ;
8+ use rustc_hir:: { Block , Expr , ExprKind , StmtKind } ;
99use rustc_lexer:: TokenKind ;
1010use rustc_lint:: { LateContext , LateLintPass } ;
1111use 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.
264259fn 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