rust/src/test/ui/non-fmt-panic.rs
Mara Bos a616f8267e 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.
2021-02-03 22:42:53 +01:00

40 lines
1.5 KiB
Rust

// build-pass (FIXME(62277): should be check-pass)
// aux-build:fancy-panic.rs
extern crate fancy_panic;
const C: &str = "abc {}";
static S: &str = "{bla}";
#[allow(unreachable_code)]
fn main() {
panic!("here's a brace: {"); //~ WARN panic message contains a brace
std::panic!("another one: }"); //~ WARN panic message contains a brace
core::panic!("Hello {}"); //~ WARN panic message contains an unused formatting placeholder
assert!(false, "{:03x} {test} bla");
//~^ WARN panic message contains unused formatting placeholders
assert!(false, S);
//~^ WARN panic message is not a string literal
debug_assert!(false, "{{}} bla"); //~ WARN panic message contains braces
panic!(C); //~ WARN panic message is not a string literal
panic!(S); //~ WARN panic message is not a string literal
std::panic!(123); //~ WARN panic message is not a string literal
core::panic!(&*"abc"); //~ WARN panic message is not a string literal
panic!(concat!("{", "}")); //~ WARN panic message contains an unused formatting placeholder
panic!(concat!("{", "{")); //~ WARN panic message contains braces
fancy_panic::fancy_panic!("test {} 123");
//~^ WARN panic message contains an unused formatting placeholder
fancy_panic::fancy_panic!(S);
//~^ WARN panic message is not a string literal
// Check that the lint only triggers for std::panic and core::panic,
// not any panic macro:
macro_rules! panic {
($e:expr) => ();
}
panic!("{}"); // OK
panic!(S); // OK
}