Skip to content

Commit 2d2bc94

Browse files
committed
Auto merge of #87982 - m-ou-se:non-fmt-panic-assert-str, r=cjgillot
Add automatic migration for assert!(.., string). Fixes part of #87313.
2 parents c0490a2 + 8fedb31 commit 2d2bc94

File tree

4 files changed

+90
-29
lines changed

4 files changed

+90
-29
lines changed

compiler/rustc_lint/src/non_fmt_panic.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,26 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
120120
);
121121
}
122122
} else {
123+
let ty = cx.typeck_results().expr_ty(arg);
124+
// If this is a &str or String, we can confidently give the `"{}", ` suggestion.
125+
let is_str = matches!(
126+
ty.kind(),
127+
ty::Ref(_, r, _) if *r.kind() == ty::Str,
128+
) || matches!(
129+
ty.ty_adt_def(),
130+
Some(ty_def) if cx.tcx.is_diagnostic_item(sym::string_type, ty_def.did),
131+
);
123132
l.span_suggestion_verbose(
124133
arg_span.shrink_to_lo(),
125134
"add a \"{}\" format string to Display the message",
126135
"\"{}\", ".into(),
127-
Applicability::MaybeIncorrect,
136+
if is_str {
137+
Applicability::MachineApplicable
138+
} else {
139+
Applicability::MaybeIncorrect
140+
},
128141
);
129-
if panic == sym::std_panic_macro {
142+
if !is_str && panic == sym::std_panic_macro {
130143
if let Some((open, close, del)) = find_delimiters(cx, span) {
131144
l.multipart_suggestion(
132145
"or use std::panic::panic_any instead",

src/test/ui/non-fmt-panic.fixed

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
debug_assert!(false, "{}", "{{}} bla"); //~ WARN panic message contains braces
21+
panic!("{}", C); //~ WARN panic message is not a string literal
22+
panic!("{}", S); //~ WARN panic message is not a string literal
23+
std::panic::panic_any(123); //~ WARN panic message is not a string literal
24+
core::panic!("{}", &*"abc"); //~ WARN panic message is not a string literal
25+
panic!("{}", concat!("{", "}")); //~ WARN panic message contains an unused formatting placeholder
26+
panic!("{}", concat!("{", "{")); //~ WARN panic message contains braces
27+
28+
fancy_panic::fancy_panic!("test {} 123");
29+
//~^ WARN panic message contains an unused formatting placeholder
30+
31+
fancy_panic::fancy_panic!(); // OK
32+
fancy_panic::fancy_panic!(S); // OK
33+
34+
macro_rules! a {
35+
() => { 123 };
36+
}
37+
38+
std::panic::panic_any(a!()); //~ WARN panic message is not a string literal
39+
40+
panic!("{}", 1); //~ WARN panic message is not a string literal
41+
assert!(false, "{}", 1); //~ WARN panic message is not a string literal
42+
debug_assert!(false, "{}", 1); //~ WARN panic message is not a string literal
43+
44+
std::panic::panic_any(123); //~ WARN panic message is not a string literal
45+
std::panic::panic_any(123); //~ WARN panic message is not a string literal
46+
47+
// Check that the lint only triggers for std::panic and core::panic,
48+
// not any panic macro:
49+
macro_rules! panic {
50+
($e:expr) => ();
51+
}
52+
panic!("{}"); // OK
53+
panic!(S); // OK
54+
}

src/test/ui/non-fmt-panic.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
// rustfix-only-machine-applicable
13
// build-pass (FIXME(62277): should be check-pass)
24
// aux-build:fancy-panic.rs
35

src/test/ui/non-fmt-panic.stderr

+19-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: panic message contains a brace
2-
--> $DIR/non-fmt-panic.rs:11:29
2+
--> $DIR/non-fmt-panic.rs:13:29
33
|
44
LL | panic!("here's a brace: {");
55
| ^
@@ -12,7 +12,7 @@ LL | panic!("{}", "here's a brace: {");
1212
| +++++
1313

1414
warning: panic message contains a brace
15-
--> $DIR/non-fmt-panic.rs:12:31
15+
--> $DIR/non-fmt-panic.rs:14:31
1616
|
1717
LL | std::panic!("another one: }");
1818
| ^
@@ -24,7 +24,7 @@ LL | std::panic!("{}", "another one: }");
2424
| +++++
2525

2626
warning: panic message contains an unused formatting placeholder
27-
--> $DIR/non-fmt-panic.rs:13:25
27+
--> $DIR/non-fmt-panic.rs:15:25
2828
|
2929
LL | core::panic!("Hello {}");
3030
| ^^
@@ -40,7 +40,7 @@ LL | core::panic!("{}", "Hello {}");
4040
| +++++
4141

4242
warning: panic message contains unused formatting placeholders
43-
--> $DIR/non-fmt-panic.rs:14:21
43+
--> $DIR/non-fmt-panic.rs:16:21
4444
|
4545
LL | assert!(false, "{:03x} {test} bla");
4646
| ^^^^^^ ^^^^^^
@@ -56,7 +56,7 @@ LL | assert!(false, "{}", "{:03x} {test} bla");
5656
| +++++
5757

5858
warning: panic message is not a string literal
59-
--> $DIR/non-fmt-panic.rs:16:20
59+
--> $DIR/non-fmt-panic.rs:18:20
6060
|
6161
LL | assert!(false, S);
6262
| ^
@@ -69,7 +69,7 @@ LL | assert!(false, "{}", S);
6969
| +++++
7070

7171
warning: panic message contains braces
72-
--> $DIR/non-fmt-panic.rs:18:27
72+
--> $DIR/non-fmt-panic.rs:20:27
7373
|
7474
LL | debug_assert!(false, "{{}} bla");
7575
| ^^^^
@@ -81,7 +81,7 @@ LL | debug_assert!(false, "{}", "{{}} bla");
8181
| +++++
8282

8383
warning: panic message is not a string literal
84-
--> $DIR/non-fmt-panic.rs:19:12
84+
--> $DIR/non-fmt-panic.rs:21:12
8585
|
8686
LL | panic!(C);
8787
| ^
@@ -92,13 +92,9 @@ help: add a "{}" format string to Display the message
9292
|
9393
LL | panic!("{}", C);
9494
| +++++
95-
help: or use std::panic::panic_any instead
96-
|
97-
LL | std::panic::panic_any(C);
98-
| ~~~~~~~~~~~~~~~~~~~~~
9995

10096
warning: panic message is not a string literal
101-
--> $DIR/non-fmt-panic.rs:20:12
97+
--> $DIR/non-fmt-panic.rs:22:12
10298
|
10399
LL | panic!(S);
104100
| ^
@@ -109,13 +105,9 @@ help: add a "{}" format string to Display the message
109105
|
110106
LL | panic!("{}", S);
111107
| +++++
112-
help: or use std::panic::panic_any instead
113-
|
114-
LL | std::panic::panic_any(S);
115-
| ~~~~~~~~~~~~~~~~~~~~~
116108

117109
warning: panic message is not a string literal
118-
--> $DIR/non-fmt-panic.rs:21:17
110+
--> $DIR/non-fmt-panic.rs:23:17
119111
|
120112
LL | std::panic!(123);
121113
| ^^^
@@ -132,7 +124,7 @@ LL | std::panic::panic_any(123);
132124
| ~~~~~~~~~~~~~~~~~~~~~
133125

134126
warning: panic message is not a string literal
135-
--> $DIR/non-fmt-panic.rs:22:18
127+
--> $DIR/non-fmt-panic.rs:24:18
136128
|
137129
LL | core::panic!(&*"abc");
138130
| ^^^^^^^
@@ -145,7 +137,7 @@ LL | core::panic!("{}", &*"abc");
145137
| +++++
146138

147139
warning: panic message contains an unused formatting placeholder
148-
--> $DIR/non-fmt-panic.rs:23:12
140+
--> $DIR/non-fmt-panic.rs:25:12
149141
|
150142
LL | panic!(concat!("{", "}"));
151143
| ^^^^^^^^^^^^^^^^^
@@ -161,7 +153,7 @@ LL | panic!("{}", concat!("{", "}"));
161153
| +++++
162154

163155
warning: panic message contains braces
164-
--> $DIR/non-fmt-panic.rs:24:5
156+
--> $DIR/non-fmt-panic.rs:26:5
165157
|
166158
LL | panic!(concat!("{", "{"));
167159
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -173,15 +165,15 @@ LL | panic!("{}", concat!("{", "{"));
173165
| +++++
174166

175167
warning: panic message contains an unused formatting placeholder
176-
--> $DIR/non-fmt-panic.rs:26:37
168+
--> $DIR/non-fmt-panic.rs:28:37
177169
|
178170
LL | fancy_panic::fancy_panic!("test {} 123");
179171
| ^^
180172
|
181173
= note: this message is not used as a format string when given without arguments, but will be in Rust 2021
182174

183175
warning: panic message is not a string literal
184-
--> $DIR/non-fmt-panic.rs:36:12
176+
--> $DIR/non-fmt-panic.rs:38:12
185177
|
186178
LL | panic!(a!());
187179
| ^^^^
@@ -198,7 +190,7 @@ LL | std::panic::panic_any(a!());
198190
| ~~~~~~~~~~~~~~~~~~~~~
199191

200192
warning: panic message is not a string literal
201-
--> $DIR/non-fmt-panic.rs:38:12
193+
--> $DIR/non-fmt-panic.rs:40:12
202194
|
203195
LL | panic!(format!("{}", 1));
204196
| ^^^^^^^^^^^^^^^^
@@ -213,7 +205,7 @@ LL + panic!("{}", 1);
213205
|
214206

215207
warning: panic message is not a string literal
216-
--> $DIR/non-fmt-panic.rs:39:20
208+
--> $DIR/non-fmt-panic.rs:41:20
217209
|
218210
LL | assert!(false, format!("{}", 1));
219211
| ^^^^^^^^^^^^^^^^
@@ -228,7 +220,7 @@ LL + assert!(false, "{}", 1);
228220
|
229221

230222
warning: panic message is not a string literal
231-
--> $DIR/non-fmt-panic.rs:40:26
223+
--> $DIR/non-fmt-panic.rs:42:26
232224
|
233225
LL | debug_assert!(false, format!("{}", 1));
234226
| ^^^^^^^^^^^^^^^^
@@ -243,7 +235,7 @@ LL + debug_assert!(false, "{}", 1);
243235
|
244236

245237
warning: panic message is not a string literal
246-
--> $DIR/non-fmt-panic.rs:42:12
238+
--> $DIR/non-fmt-panic.rs:44:12
247239
|
248240
LL | panic![123];
249241
| ^^^
@@ -260,7 +252,7 @@ LL | std::panic::panic_any(123);
260252
| ~~~~~~~~~~~~~~~~~~~~~~ ~
261253

262254
warning: panic message is not a string literal
263-
--> $DIR/non-fmt-panic.rs:43:12
255+
--> $DIR/non-fmt-panic.rs:45:12
264256
|
265257
LL | panic!{123};
266258
| ^^^

0 commit comments

Comments
 (0)