Skip to content

Commit adfc54c

Browse files
authored
Rollup merge of rust-lang#81645 - m-ou-se:panic-lint, r=estebank,flip1995
Add lint for `panic!(123)` which is not accepted in Rust 2021. This extends the `panic_fmt` lint to warn for all cases where the first argument cannot be interpreted as a format string, as will happen in Rust 2021. It suggests to add `"{}",` to format the message as a string. In the case of `std::panic!()`, it also suggests the recently stabilized `std::panic::panic_any()` function as an alternative. It renames the lint to `non_fmt_panic` to match the lint naming guidelines. ![image](https://user-images.githubusercontent.com/783247/106520928-675ea680-64d5-11eb-81f7-d8fa48b93a0b.png) This is part of rust-lang#80162. r? ```@estebank```
2 parents cb2da73 + 65d9c30 commit adfc54c

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

core/src/macros/panic.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@ tests. `panic!` is closely tied with the `unwrap` method of both
1010
`panic!` when they are set to [`None`] or [`Err`] variants.
1111

1212
This macro is used to inject panic into a Rust thread, causing the thread to
13-
panic entirely. Each thread's panic can be reaped as the [`Box`]`<`[`Any`]`>` type,
14-
and the single-argument form of the `panic!` macro will be the value which
15-
is transmitted.
13+
panic entirely. This macro panics with a string and uses the [`format!`] syntax
14+
for building the message.
15+
16+
Each thread's panic can be reaped as the [`Box`]`<`[`Any`]`>` type,
17+
which contains either a `&str` or `String` for regular `panic!()` invocations.
18+
To panic with a value of another other type, [`panic_any`] can be used.
1619

1720
[`Result`] enum is often a better solution for recovering from errors than
1821
using the `panic!` macro. This macro should be used to avoid proceeding using
1922
incorrect values, such as from external sources. Detailed information about
2023
error handling is found in the [book].
2124

22-
The multi-argument form of this macro panics with a string and has the
23-
[`format!`] syntax for building a string.
24-
2525
See also the macro [`compile_error!`], for raising errors during compilation.
2626

2727
[ounwrap]: Option::unwrap
2828
[runwrap]: Result::unwrap
29+
[`panic_any`]: ../std/panic/fn.panic_any.html
2930
[`Box`]: ../std/boxed/struct.Box.html
3031
[`Any`]: crate::any::Any
3132
[`format!`]: ../std/macro.format.html
@@ -42,6 +43,6 @@ program with code `101`.
4243
# #![allow(unreachable_code)]
4344
panic!();
4445
panic!("this is a terrible mistake!");
45-
panic!(4); // panic with the value of 4 to be collected elsewhere
4646
panic!("this is a {} {message}", "fancy", message = "message");
47+
std::panic::panic_any(4); // panic with the value of 4 to be collected elsewhere
4748
```

term/src/terminfo/parm/tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ fn test_comparison_ops() {
7777
for &(op, bs) in v.iter() {
7878
let s = format!("%{{1}}%{{2}}%{}%d", op);
7979
let res = expand(s.as_bytes(), &[], &mut Variables::new());
80-
assert!(res.is_ok(), res.unwrap_err());
80+
assert!(res.is_ok(), "{}", res.unwrap_err());
8181
assert_eq!(res.unwrap(), vec![b'0' + bs[0]]);
8282
let s = format!("%{{1}}%{{1}}%{}%d", op);
8383
let res = expand(s.as_bytes(), &[], &mut Variables::new());
84-
assert!(res.is_ok(), res.unwrap_err());
84+
assert!(res.is_ok(), "{}", res.unwrap_err());
8585
assert_eq!(res.unwrap(), vec![b'0' + bs[1]]);
8686
let s = format!("%{{2}}%{{1}}%{}%d", op);
8787
let res = expand(s.as_bytes(), &[], &mut Variables::new());
88-
assert!(res.is_ok(), res.unwrap_err());
88+
assert!(res.is_ok(), "{}", res.unwrap_err());
8989
assert_eq!(res.unwrap(), vec![b'0' + bs[2]]);
9090
}
9191
}
@@ -95,13 +95,13 @@ fn test_conditionals() {
9595
let mut vars = Variables::new();
9696
let s = b"\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m";
9797
let res = expand(s, &[Number(1)], &mut vars);
98-
assert!(res.is_ok(), res.unwrap_err());
98+
assert!(res.is_ok(), "{}", res.unwrap_err());
9999
assert_eq!(res.unwrap(), "\\E[31m".bytes().collect::<Vec<_>>());
100100
let res = expand(s, &[Number(8)], &mut vars);
101-
assert!(res.is_ok(), res.unwrap_err());
101+
assert!(res.is_ok(), "{}", res.unwrap_err());
102102
assert_eq!(res.unwrap(), "\\E[90m".bytes().collect::<Vec<_>>());
103103
let res = expand(s, &[Number(42)], &mut vars);
104-
assert!(res.is_ok(), res.unwrap_err());
104+
assert!(res.is_ok(), "{}", res.unwrap_err());
105105
assert_eq!(res.unwrap(), "\\E[38;5;42m".bytes().collect::<Vec<_>>());
106106
}
107107

test/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn test_should_panic_bad_message() {
199199
fn test_should_panic_non_string_message_type() {
200200
use crate::tests::TrFailedMsg;
201201
fn f() {
202-
panic!(1i32);
202+
std::panic::panic_any(1i32);
203203
}
204204
let expected = "foobar";
205205
let failed_msg = format!(

0 commit comments

Comments
 (0)