-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix if_then_panic for
#![no_std]
and Rust 2021
- Loading branch information
Showing
8 changed files
with
286 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// run-rustfix | ||
#![crate_type = "lib"] | ||
#![no_std] | ||
#![warn(clippy::if_then_panic)] | ||
|
||
pub fn main() { | ||
let a = &[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 { | ||
format_args!("qwq"); | ||
format_args!("qwq"); | ||
format_args!("qwq"); | ||
} | ||
if let Some(b) = c { | ||
panic!("orz {}", b); | ||
} | ||
if a.len() == 3 { | ||
panic!("qaqaq"); | ||
} else { | ||
format_args!("qwq"); | ||
} | ||
let b = &[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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// run-rustfix | ||
#![crate_type = "lib"] | ||
#![no_std] | ||
#![warn(clippy::if_then_panic)] | ||
|
||
pub fn main() { | ||
let a = &[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); | ||
} | ||
if !a.is_empty() { | ||
panic!("qaqaq{:?}", a); | ||
} | ||
if !a.is_empty() { | ||
panic!("qwqwq"); | ||
} | ||
if a.len() == 3 { | ||
format_args!("qwq"); | ||
format_args!("qwq"); | ||
format_args!("qwq"); | ||
} | ||
if let Some(b) = c { | ||
panic!("orz {}", b); | ||
} | ||
if a.len() == 3 { | ||
panic!("qaqaq"); | ||
} else { | ||
format_args!("qwq"); | ||
} | ||
let b = &[1, 2, 3]; | ||
if b.is_empty() { | ||
panic!("panic1"); | ||
} | ||
if b.is_empty() && a.is_empty() { | ||
panic!("panic2"); | ||
} | ||
if a.is_empty() && !b.is_empty() { | ||
panic!("panic3"); | ||
} | ||
if b.is_empty() || a.is_empty() { | ||
panic!("panic4"); | ||
} | ||
if a.is_empty() || !b.is_empty() { | ||
panic!("panic5"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_core.rs:21: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_core.rs:24: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_core.rs:41: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_core.rs:44: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_core.rs:47: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_core.rs:50: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_core.rs:53: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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#![crate_type = "lib"] | ||
#![no_std] | ||
#![feature(exclusive_range_pattern)] | ||
#![allow(clippy::match_same_arms)] | ||
#![warn(clippy::match_wild_err_arm)] | ||
|
||
fn foo() {} | ||
|
||
fn match_wild_err_arm() { | ||
let x: Result<i32, &str> = Ok(3); | ||
|
||
match x { | ||
Ok(3) => (), | ||
Ok(_) => (), | ||
Err(_) => panic!("err"), | ||
} | ||
|
||
match x { | ||
Ok(3) => (), | ||
Ok(_) => (), | ||
Err(_) => panic!(), | ||
} | ||
|
||
match x { | ||
Ok(3) => (), | ||
Ok(_) => (), | ||
Err(_) => { | ||
panic!(); | ||
}, | ||
} | ||
|
||
match x { | ||
Ok(3) => (), | ||
Ok(_) => (), | ||
Err(_e) => panic!(), | ||
} | ||
|
||
// Allowed when used in `panic!`. | ||
match x { | ||
Ok(3) => (), | ||
Ok(_) => (), | ||
Err(_e) => panic!("{}", _e), | ||
} | ||
|
||
// Allowed when not with `panic!` block. | ||
match x { | ||
Ok(3) => (), | ||
Ok(_) => (), | ||
Err(_) => foo(), | ||
} | ||
|
||
// Allowed when used with `unreachable!`. | ||
match x { | ||
Ok(3) => (), | ||
Ok(_) => (), | ||
Err(_) => unreachable!(), | ||
} | ||
|
||
// Allowed when used with `unreachable!`. | ||
match x { | ||
Ok(3) => (), | ||
Ok(_) => (), | ||
Err(_) => { | ||
unreachable!(); | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
error: `Err(_)` matches all errors | ||
--> $DIR/match_wild_err_arm_core.rs:15: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_core.rs:21: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_core.rs:27: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_core.rs:35: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 | ||
|