Skip to content

Commit

Permalink
Fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdd0121 committed Oct 20, 2021
1 parent 22c0127 commit 8ec8372
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
4 changes: 1 addition & 3 deletions src/tools/clippy/clippy_lints/src/if_then_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ impl LateLintPass<'_> for IfThenPanic {
}
} else {
if_chain! {
if let ExprKind::Block(block, _) = semi.kind;
if let Some(init) = block.expr;
if let ExprKind::Call(_, [format_args]) = init.kind;
if let ExprKind::Call(_, [format_args]) = semi.kind;

then {
format_args.span
Expand Down
24 changes: 14 additions & 10 deletions src/tools/clippy/clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,8 +968,7 @@ fn check_wild_err_arm<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm
}
if_chain! {
if matching_wild;
if let ExprKind::Block(block, _) = arm.body.kind;
if is_panic_block(block);
if is_panic_call(arm.body);
then {
// `Err(_)` or `Err(_e)` arm with `panic!` found
span_lint_and_note(cx,
Expand Down Expand Up @@ -1172,14 +1171,19 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])
}

// If the block contains only a `panic!` macro (as expression or statement)
fn is_panic_block(block: &Block<'_>) -> bool {
match (&block.expr, block.stmts.len(), block.stmts.first()) {
(&Some(exp), 0, _) => is_expn_of(exp.span, "panic").is_some() && is_expn_of(exp.span, "unreachable").is_none(),
(&None, 1, Some(stmt)) => {
is_expn_of(stmt.span, "panic").is_some() && is_expn_of(stmt.span, "unreachable").is_none()
},
_ => false,
}
fn is_panic_call(expr: &Expr<'_>) -> bool {
// Unwrap any wrapping blocks
let span = if let ExprKind::Block(block, _) = expr.kind {
match (&block.expr, block.stmts.len(), block.stmts.first()) {
(&Some(exp), 0, _) => exp.span,
(&None, 1, Some(stmt)) => stmt.span,
_ => return false,
}
} else {
expr.span
};

is_expn_of(span, "panic").is_some() && is_expn_of(span, "unreachable").is_none()
}

fn check_match_ref_pats<'a, 'b, I>(cx: &LateContext<'_>, ex: &Expr<'_>, pats: I, expr: &Expr<'_>)
Expand Down
4 changes: 1 addition & 3 deletions src/tools/clippy/clippy_utils/src/higher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,7 @@ impl PanicExpn<'tcx> {
/// Parses an expanded `panic!` invocation
pub fn parse(expr: &'tcx Expr<'tcx>) -> Option<Self> {
if_chain! {
if let ExprKind::Block(block, _) = expr.kind;
if let Some(init) = block.expr;
if let ExprKind::Call(_, [format_args]) = init.kind;
if let ExprKind::Call(_, [format_args]) = expr.kind;
let expn_data = expr.span.ctxt().outer_expn_data();
if let Some(format_args) = FormatArgsExpn::parse(format_args);
then {
Expand Down
10 changes: 9 additions & 1 deletion src/tools/clippy/tests/ui/diverging_sub_expression.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ error: sub-expression diverges
LL | 3 => true || diverge(),
| ^^^^^^^^^

error: sub-expression diverges
--> $DIR/diverging_sub_expression.rs:37:30
|
LL | _ => true || panic!("boo"),
| ^^^^^^^^^^^^^
|
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)

error: sub-expression diverges
--> $DIR/diverging_sub_expression.rs:39:26
|
LL | _ => true || break,
| ^^^^^

error: aborting due to 6 previous errors
error: aborting due to 7 previous errors

1 change: 1 addition & 0 deletions src/tools/clippy/tests/ui/issue-7447.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct ByteView<'a> {
backing: Arc<ByteViewBacking<'a>>,
}

#[allow(clippy::diverging_sub_expression)]
fn main() {
byte_view(panic!());
group_entries(panic!());
Expand Down

0 comments on commit 8ec8372

Please sign in to comment.