Skip to content

Commit 2acc3e3

Browse files
authored
Add brackets around unsafe or labeled block used in else (#16603)
Fixes #16602 ---- changelog: [`manual_let_else`]: use brackets around unsafe or labeled block used in `else`
2 parents 18e5b2d + 5991fd9 commit 2acc3e3

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

clippy_lints/src/manual_let_else.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use rustc_ast::BindingMode;
99
use rustc_data_structures::fx::FxHashMap;
1010
use rustc_errors::Applicability;
1111
use rustc_hir::def::{CtorOf, DefKind, Res};
12-
use rustc_hir::{Arm, Expr, ExprKind, MatchSource, Pat, PatExpr, PatExprKind, PatKind, QPath, Stmt, StmtKind};
12+
use rustc_hir::{
13+
Arm, BlockCheckMode, Expr, ExprKind, MatchSource, Pat, PatExpr, PatExprKind, PatKind, QPath, Stmt, StmtKind,
14+
};
1315
use rustc_lint::{LateContext, LintContext};
1416
use rustc_span::Span;
1517
use rustc_span::symbol::{Symbol, sym};
@@ -177,7 +179,10 @@ fn emit_manual_let_else(
177179
let (sn_expr, _) = snippet_with_context(cx, expr.span, span.ctxt(), "", &mut app);
178180
let (sn_else, else_is_mac_call) = snippet_with_context(cx, else_body.span, span.ctxt(), "", &mut app);
179181

180-
let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) && !else_is_mac_call {
182+
let else_bl = if let ExprKind::Block(block, None) = else_body.kind
183+
&& matches!(block.rules, BlockCheckMode::DefaultBlock)
184+
&& !else_is_mac_call
185+
{
181186
sn_else.into_owned()
182187
} else {
183188
format!("{{ {sn_else} }}")

tests/ui/manual_let_else.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,3 +571,21 @@ mod issue15914 {
571571
};
572572
}
573573
}
574+
575+
fn issue16602(i: Result<i32, i32>) {
576+
//~v manual_let_else
577+
_ = match i {
578+
Ok(i) => i,
579+
Err(_) => unsafe {
580+
core::hint::unreachable_unchecked();
581+
},
582+
};
583+
584+
//~v manual_let_else
585+
_ = match i {
586+
Ok(i) => i,
587+
Err(_) => 'useless_label: {
588+
panic!();
589+
},
590+
};
591+
}

tests/ui/manual_let_else.stderr

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,5 +585,41 @@ LL + return;
585585
LL + };
586586
|
587587

588-
error: aborting due to 37 previous errors
588+
error: this could be rewritten as `let...else`
589+
--> tests/ui/manual_let_else.rs:577:5
590+
|
591+
LL | / _ = match i {
592+
LL | | Ok(i) => i,
593+
LL | | Err(_) => unsafe {
594+
LL | | core::hint::unreachable_unchecked();
595+
LL | | },
596+
LL | | };
597+
| |_____^
598+
|
599+
help: consider writing
600+
|
601+
LL ~ let Ok(_) = i else { unsafe {
602+
LL + core::hint::unreachable_unchecked();
603+
LL ~ } };;
604+
|
605+
606+
error: this could be rewritten as `let...else`
607+
--> tests/ui/manual_let_else.rs:585:5
608+
|
609+
LL | / _ = match i {
610+
LL | | Ok(i) => i,
611+
LL | | Err(_) => 'useless_label: {
612+
LL | | panic!();
613+
LL | | },
614+
LL | | };
615+
| |_____^
616+
|
617+
help: consider writing
618+
|
619+
LL ~ let Ok(_) = i else { 'useless_label: {
620+
LL + panic!();
621+
LL ~ } };;
622+
|
623+
624+
error: aborting due to 39 previous errors
589625

0 commit comments

Comments
 (0)