]> git.lizzy.rs Git - rust.git/blob - src/test/ui/non-fmt-panic.rs
Auto merge of #85556 - FabianWolff:issue-85071, r=estebank,jackh726
[rust.git] / src / test / ui / non-fmt-panic.rs
1 // run-rustfix
2 // rustfix-only-machine-applicable
3 // build-pass (FIXME(62277): should be check-pass)
4 // aux-build:fancy-panic.rs
5
6 extern crate fancy_panic;
7
8 const C: &str = "abc {}";
9 static S: &str = "{bla}";
10
11 #[allow(unreachable_code)]
12 fn main() {
13     panic!("here's a brace: {"); //~ WARN panic message contains a brace
14     std::panic!("another one: }"); //~ WARN panic message contains a brace
15     core::panic!("Hello {}"); //~ WARN panic message contains an unused formatting placeholder
16     assert!(false, "{:03x} {test} bla");
17     //~^ WARN panic message contains unused formatting placeholders
18     assert!(false, S);
19     //~^ WARN panic message is not a string literal
20     assert!(false, 123);
21     //~^ WARN panic message is not a string literal
22     assert!(false, Some(123));
23     //~^ WARN panic message is not a string literal
24     debug_assert!(false, "{{}} bla"); //~ WARN panic message contains braces
25     panic!(C); //~ WARN panic message is not a string literal
26     panic!(S); //~ WARN panic message is not a string literal
27     std::panic!(123); //~ WARN panic message is not a string literal
28     core::panic!(&*"abc"); //~ WARN panic message is not a string literal
29     panic!(Some(123)); //~ WARN panic message is not a string literal
30     panic!(concat!("{", "}")); //~ WARN panic message contains an unused formatting placeholder
31     panic!(concat!("{", "{")); //~ WARN panic message contains braces
32
33     fancy_panic::fancy_panic!("test {} 123");
34     //~^ WARN panic message contains an unused formatting placeholder
35
36     fancy_panic::fancy_panic!(); // OK
37     fancy_panic::fancy_panic!(S); // OK
38
39     macro_rules! a {
40         () => { 123 };
41     }
42
43     panic!(a!()); //~ WARN panic message is not a string literal
44
45     panic!(format!("{}", 1)); //~ WARN panic message is not a string literal
46     assert!(false, format!("{}", 1)); //~ WARN panic message is not a string literal
47     debug_assert!(false, format!("{}", 1)); //~ WARN panic message is not a string literal
48
49     panic![123]; //~ WARN panic message is not a string literal
50     panic!{123}; //~ WARN panic message is not a string literal
51
52     // Check that the lint only triggers for std::panic and core::panic,
53     // not any panic macro:
54     macro_rules! panic {
55         ($e:expr) => ();
56     }
57     panic!("{}"); // OK
58     panic!(S); // OK
59
60     a(1);
61     b(1);
62     c(1);
63     d(1);
64 }
65
66 fn a<T: Send + 'static>(v: T) {
67     panic!(v); //~ WARN panic message is not a string literal
68     assert!(false, v); //~ WARN panic message is not a string literal
69 }
70
71 fn b<T: std::fmt::Debug + Send + 'static>(v: T) {
72     panic!(v); //~ WARN panic message is not a string literal
73     assert!(false, v); //~ WARN panic message is not a string literal
74 }
75
76 fn c<T: std::fmt::Display + Send + 'static>(v: T) {
77     panic!(v); //~ WARN panic message is not a string literal
78     assert!(false, v); //~ WARN panic message is not a string literal
79 }
80
81 fn d<T: std::fmt::Display + std::fmt::Debug + Send + 'static>(v: T) {
82     panic!(v); //~ WARN panic message is not a string literal
83     assert!(false, v); //~ WARN panic message is not a string literal
84 }