Skip to content

Commit 8ec8372

Browse files
committed
Fix clippy
1 parent 22c0127 commit 8ec8372

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

src/tools/clippy/clippy_lints/src/if_then_panic.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ impl LateLintPass<'_> for IfThenPanic {
6161
}
6262
} else {
6363
if_chain! {
64-
if let ExprKind::Block(block, _) = semi.kind;
65-
if let Some(init) = block.expr;
66-
if let ExprKind::Call(_, [format_args]) = init.kind;
64+
if let ExprKind::Call(_, [format_args]) = semi.kind;
6765

6866
then {
6967
format_args.span

src/tools/clippy/clippy_lints/src/matches.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -968,8 +968,7 @@ fn check_wild_err_arm<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm
968968
}
969969
if_chain! {
970970
if matching_wild;
971-
if let ExprKind::Block(block, _) = arm.body.kind;
972-
if is_panic_block(block);
971+
if is_panic_call(arm.body);
973972
then {
974973
// `Err(_)` or `Err(_e)` arm with `panic!` found
975974
span_lint_and_note(cx,
@@ -1172,14 +1171,19 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])
11721171
}
11731172

11741173
// If the block contains only a `panic!` macro (as expression or statement)
1175-
fn is_panic_block(block: &Block<'_>) -> bool {
1176-
match (&block.expr, block.stmts.len(), block.stmts.first()) {
1177-
(&Some(exp), 0, _) => is_expn_of(exp.span, "panic").is_some() && is_expn_of(exp.span, "unreachable").is_none(),
1178-
(&None, 1, Some(stmt)) => {
1179-
is_expn_of(stmt.span, "panic").is_some() && is_expn_of(stmt.span, "unreachable").is_none()
1180-
},
1181-
_ => false,
1182-
}
1174+
fn is_panic_call(expr: &Expr<'_>) -> bool {
1175+
// Unwrap any wrapping blocks
1176+
let span = if let ExprKind::Block(block, _) = expr.kind {
1177+
match (&block.expr, block.stmts.len(), block.stmts.first()) {
1178+
(&Some(exp), 0, _) => exp.span,
1179+
(&None, 1, Some(stmt)) => stmt.span,
1180+
_ => return false,
1181+
}
1182+
} else {
1183+
expr.span
1184+
};
1185+
1186+
is_expn_of(span, "panic").is_some() && is_expn_of(span, "unreachable").is_none()
11831187
}
11841188

11851189
fn check_match_ref_pats<'a, 'b, I>(cx: &LateContext<'_>, ex: &Expr<'_>, pats: I, expr: &Expr<'_>)

src/tools/clippy/clippy_utils/src/higher.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,7 @@ impl PanicExpn<'tcx> {
615615
/// Parses an expanded `panic!` invocation
616616
pub fn parse(expr: &'tcx Expr<'tcx>) -> Option<Self> {
617617
if_chain! {
618-
if let ExprKind::Block(block, _) = expr.kind;
619-
if let Some(init) = block.expr;
620-
if let ExprKind::Call(_, [format_args]) = init.kind;
618+
if let ExprKind::Call(_, [format_args]) = expr.kind;
621619
let expn_data = expr.span.ctxt().outer_expn_data();
622620
if let Some(format_args) = FormatArgsExpn::parse(format_args);
623621
then {

src/tools/clippy/tests/ui/diverging_sub_expression.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,19 @@ error: sub-expression diverges
3030
LL | 3 => true || diverge(),
3131
| ^^^^^^^^^
3232

33+
error: sub-expression diverges
34+
--> $DIR/diverging_sub_expression.rs:37:30
35+
|
36+
LL | _ => true || panic!("boo"),
37+
| ^^^^^^^^^^^^^
38+
|
39+
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
40+
3341
error: sub-expression diverges
3442
--> $DIR/diverging_sub_expression.rs:39:26
3543
|
3644
LL | _ => true || break,
3745
| ^^^^^
3846

39-
error: aborting due to 6 previous errors
47+
error: aborting due to 7 previous errors
4048

src/tools/clippy/tests/ui/issue-7447.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct ByteView<'a> {
1919
backing: Arc<ByteViewBacking<'a>>,
2020
}
2121

22+
#[allow(clippy::diverging_sub_expression)]
2223
fn main() {
2324
byte_view(panic!());
2425
group_entries(panic!());

0 commit comments

Comments
 (0)