Skip to content

Commit

Permalink
Fix if_then_panic for #![no_std] and Rust 2021
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdd0121 committed Oct 24, 2021
1 parent ba0cfc7 commit 95e3565
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 28 deletions.
15 changes: 11 additions & 4 deletions clippy_lints/src/if_then_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,23 @@ impl LateLintPass<'_> for IfThenPanic {
if !cx.tcx.sess.source_map().is_multiline(cond.span);

then {
let span = if let Some(panic_expn) = PanicExpn::parse(semi) {
let call = if_chain! {
if let ExprKind::Block(block, _) = semi.kind;
if let Some(init) = block.expr;
then {
init
} else {
semi
}
};
let span = if let Some(panic_expn) = PanicExpn::parse(call) {
match *panic_expn.format_args.value_args {
[] => panic_expn.format_args.format_string_span,
[.., last] => panic_expn.format_args.format_string_span.to(last.span),
}
} 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]) = call.kind;

then {
format_args.span
Expand Down
24 changes: 14 additions & 10 deletions 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 clippy_utils/src/higher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,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
43 changes: 43 additions & 0 deletions tests/ui/if_then_panic.edition2018.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// revisions: edition2018 edition2021
// [edition2018] edition:2018
// [edition2021] edition:2021
// run-rustfix
#![warn(clippy::if_then_panic)]

fn main() {
let a = vec![1, 2, 3];
let c = Some(2);
if !a.is_empty()
&& a.len() == 3
&& c != None
&& !a.is_empty()
&& a.len() == 3
&& !a.is_empty()
&& a.len() == 3
&& !a.is_empty()
&& a.len() == 3
{
panic!("qaqaq{:?}", a);
}
assert!(a.is_empty(), "qaqaq{:?}", a);
assert!(a.is_empty(), "qwqwq");
if a.len() == 3 {
println!("qwq");
println!("qwq");
println!("qwq");
}
if let Some(b) = c {
panic!("orz {}", b);
}
if a.len() == 3 {
panic!("qaqaq");
} else {
println!("qwq");
}
let b = vec![1, 2, 3];
assert!(!b.is_empty(), "panic1");
assert!(!(b.is_empty() && a.is_empty()), "panic2");
assert!(!(a.is_empty() && !b.is_empty()), "panic3");
assert!(!(b.is_empty() || a.is_empty()), "panic4");
assert!(!(a.is_empty() || !b.is_empty()), "panic5");
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:19:5
--> $DIR/if_then_panic.rs:22:5
|
LL | / if !a.is_empty() {
LL | | panic!("qaqaq{:?}", a);
Expand All @@ -9,47 +9,47 @@ LL | | }
= note: `-D clippy::if-then-panic` implied by `-D warnings`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:22:5
--> $DIR/if_then_panic.rs:25:5
|
LL | / if !a.is_empty() {
LL | | panic!("qwqwq");
LL | | }
| |_____^ help: try: `assert!(a.is_empty(), "qwqwq");`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:39:5
--> $DIR/if_then_panic.rs:42:5
|
LL | / if b.is_empty() {
LL | | panic!("panic1");
LL | | }
| |_____^ help: try: `assert!(!b.is_empty(), "panic1");`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:42:5
--> $DIR/if_then_panic.rs:45:5
|
LL | / if b.is_empty() && a.is_empty() {
LL | | panic!("panic2");
LL | | }
| |_____^ help: try: `assert!(!(b.is_empty() && a.is_empty()), "panic2");`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:45:5
--> $DIR/if_then_panic.rs:48:5
|
LL | / if a.is_empty() && !b.is_empty() {
LL | | panic!("panic3");
LL | | }
| |_____^ help: try: `assert!(!(a.is_empty() && !b.is_empty()), "panic3");`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:48:5
--> $DIR/if_then_panic.rs:51:5
|
LL | / if b.is_empty() || a.is_empty() {
LL | | panic!("panic4");
LL | | }
| |_____^ help: try: `assert!(!(b.is_empty() || a.is_empty()), "panic4");`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:51:5
--> $DIR/if_then_panic.rs:54:5
|
LL | / if a.is_empty() || !b.is_empty() {
LL | | panic!("panic5");
Expand Down
43 changes: 43 additions & 0 deletions tests/ui/if_then_panic.edition2021.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// revisions: edition2018 edition2021
// [edition2018] edition:2018
// [edition2021] edition:2021
// run-rustfix
#![warn(clippy::if_then_panic)]

fn main() {
let a = vec![1, 2, 3];
let c = Some(2);
if !a.is_empty()
&& a.len() == 3
&& c != None
&& !a.is_empty()
&& a.len() == 3
&& !a.is_empty()
&& a.len() == 3
&& !a.is_empty()
&& a.len() == 3
{
panic!("qaqaq{:?}", a);
}
assert!(a.is_empty(), "qaqaq{:?}", a);
assert!(a.is_empty(), "qwqwq");
if a.len() == 3 {
println!("qwq");
println!("qwq");
println!("qwq");
}
if let Some(b) = c {
panic!("orz {}", b);
}
if a.len() == 3 {
panic!("qaqaq");
} else {
println!("qwq");
}
let b = vec![1, 2, 3];
assert!(!b.is_empty(), "panic1");
assert!(!(b.is_empty() && a.is_empty()), "panic2");
assert!(!(a.is_empty() && !b.is_empty()), "panic3");
assert!(!(b.is_empty() || a.is_empty()), "panic4");
assert!(!(a.is_empty() || !b.is_empty()), "panic5");
}
60 changes: 60 additions & 0 deletions tests/ui/if_then_panic.edition2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:22:5
|
LL | / if !a.is_empty() {
LL | | panic!("qaqaq{:?}", a);
LL | | }
| |_____^ help: try: `assert!(a.is_empty(), "qaqaq{:?}", a);`
|
= note: `-D clippy::if-then-panic` implied by `-D warnings`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:25:5
|
LL | / if !a.is_empty() {
LL | | panic!("qwqwq");
LL | | }
| |_____^ help: try: `assert!(a.is_empty(), "qwqwq");`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:42:5
|
LL | / if b.is_empty() {
LL | | panic!("panic1");
LL | | }
| |_____^ help: try: `assert!(!b.is_empty(), "panic1");`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:45:5
|
LL | / if b.is_empty() && a.is_empty() {
LL | | panic!("panic2");
LL | | }
| |_____^ help: try: `assert!(!(b.is_empty() && a.is_empty()), "panic2");`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:48:5
|
LL | / if a.is_empty() && !b.is_empty() {
LL | | panic!("panic3");
LL | | }
| |_____^ help: try: `assert!(!(a.is_empty() && !b.is_empty()), "panic3");`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:51:5
|
LL | / if b.is_empty() || a.is_empty() {
LL | | panic!("panic4");
LL | | }
| |_____^ help: try: `assert!(!(b.is_empty() || a.is_empty()), "panic4");`

error: only a `panic!` in `if`-then statement
--> $DIR/if_then_panic.rs:54:5
|
LL | / if a.is_empty() || !b.is_empty() {
LL | | panic!("panic5");
LL | | }
| |_____^ help: try: `assert!(!(a.is_empty() || !b.is_empty()), "panic5");`

error: aborting due to 7 previous errors

3 changes: 3 additions & 0 deletions tests/ui/if_then_panic.fixed
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: edition2018 edition2021
// [edition2018] edition:2018
// [edition2021] edition:2021
// run-rustfix
#![warn(clippy::if_then_panic)]

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/if_then_panic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: edition2018 edition2021
// [edition2018] edition:2018
// [edition2021] edition:2021
// run-rustfix
#![warn(clippy::if_then_panic)]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: `Err(_)` matches all errors
--> $DIR/match_wild_err_arm.rs:11:9
--> $DIR/match_wild_err_arm.rs:14:9
|
LL | Err(_) => panic!("err"),
| ^^^^^^
Expand All @@ -8,23 +8,23 @@ LL | Err(_) => panic!("err"),
= note: match each error separately or use the error output, or use `.except(msg)` if the error case is unreachable

error: `Err(_)` matches all errors
--> $DIR/match_wild_err_arm.rs:17:9
--> $DIR/match_wild_err_arm.rs:20:9
|
LL | Err(_) => panic!(),
| ^^^^^^
|
= note: match each error separately or use the error output, or use `.except(msg)` if the error case is unreachable

error: `Err(_)` matches all errors
--> $DIR/match_wild_err_arm.rs:23:9
--> $DIR/match_wild_err_arm.rs:26:9
|
LL | Err(_) => {
| ^^^^^^
|
= note: match each error separately or use the error output, or use `.except(msg)` if the error case is unreachable

error: `Err(_e)` matches all errors
--> $DIR/match_wild_err_arm.rs:31:9
--> $DIR/match_wild_err_arm.rs:34:9
|
LL | Err(_e) => panic!(),
| ^^^^^^^
Expand Down
35 changes: 35 additions & 0 deletions tests/ui/match_wild_err_arm.edition2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
error: `Err(_)` matches all errors
--> $DIR/match_wild_err_arm.rs:14:9
|
LL | Err(_) => panic!("err"),
| ^^^^^^
|
= note: `-D clippy::match-wild-err-arm` implied by `-D warnings`
= note: match each error separately or use the error output, or use `.except(msg)` if the error case is unreachable

error: `Err(_)` matches all errors
--> $DIR/match_wild_err_arm.rs:20:9
|
LL | Err(_) => panic!(),
| ^^^^^^
|
= note: match each error separately or use the error output, or use `.except(msg)` if the error case is unreachable

error: `Err(_)` matches all errors
--> $DIR/match_wild_err_arm.rs:26:9
|
LL | Err(_) => {
| ^^^^^^
|
= note: match each error separately or use the error output, or use `.except(msg)` if the error case is unreachable

error: `Err(_e)` matches all errors
--> $DIR/match_wild_err_arm.rs:34:9
|
LL | Err(_e) => panic!(),
| ^^^^^^^
|
= note: match each error separately or use the error output, or use `.except(msg)` if the error case is unreachable

error: aborting due to 4 previous errors

3 changes: 3 additions & 0 deletions tests/ui/match_wild_err_arm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: edition2018 edition2021
// [edition2018] edition:2018
// [edition2021] edition:2021
#![feature(exclusive_range_pattern)]
#![allow(clippy::match_same_arms)]
#![warn(clippy::match_wild_err_arm)]
Expand Down

0 comments on commit 95e3565

Please sign in to comment.