@@ -5,7 +5,7 @@ use clippy_utils::source::{IntoSpan as _, SpanRangeExt, snippet, snippet_block_w
5
5
use clippy_utils:: { span_contains_non_whitespace, tokenize_with_text} ;
6
6
use rustc_ast:: BinOpKind ;
7
7
use rustc_errors:: Applicability ;
8
- use rustc_hir:: { Block , Expr , ExprKind , Stmt , StmtKind } ;
8
+ use rustc_hir:: { Block , Expr , ExprKind , StmtKind } ;
9
9
use rustc_lexer:: TokenKind ;
10
10
use rustc_lint:: { LateContext , LateLintPass } ;
11
11
use rustc_session:: impl_lint_pass;
@@ -141,11 +141,7 @@ impl CollapsibleIf {
141
141
142
142
// Prevent "elseif"
143
143
// 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 ( ) ) ;
149
145
let mut applicability = Applicability :: MachineApplicable ;
150
146
diag. span_suggestion (
151
147
else_block. span ,
@@ -173,8 +169,7 @@ impl CollapsibleIf {
173
169
&& cx. tcx . hir_attrs ( inner. hir_id ) . is_empty ( )
174
170
&& let ExprKind :: If ( check_inner, _, None ) = & inner. kind
175
171
&& self . eligible_condition ( cx, check_inner)
176
- && let ctxt = expr. span . ctxt ( )
177
- && inner. span . ctxt ( ) == ctxt
172
+ && expr. span . eq_ctxt ( inner. span )
178
173
&& !block_starts_with_significant_tokens ( cx, then, inner, self . lint_commented_code )
179
174
{
180
175
span_lint_and_then (
@@ -262,14 +257,9 @@ fn block_starts_with_significant_tokens(
262
257
/// If `block` is a block with either one expression or a statement containing an expression,
263
258
/// return the expression. We don't peel blocks recursively, as extra blocks might be intentional.
264
259
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) ,
273
263
_ => None ,
274
264
}
275
265
}
0 commit comments