Skip to content

Commit 8f14592

Browse files
committed
Improve "panic message is not a string literal" warning
This warning always referenced panic! even in case of an assert. Related to #84656
1 parent 1773f14 commit 8f14592

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

compiler/rustc_lint/src/non_fmt_panic.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_errors::{pluralize, Applicability};
44
use rustc_hir as hir;
55
use rustc_middle::ty;
66
use rustc_parse_format::{ParseMode, Parser, Piece};
7-
use rustc_span::{sym, symbol::kw, InnerSpan, Span, Symbol};
7+
use rustc_span::{hygiene, sym, symbol::kw, symbol::SymbolStr, InnerSpan, Span, Symbol};
88

99
declare_lint! {
1010
/// The `non_fmt_panic` lint detects `panic!(..)` invocations where the first
@@ -67,7 +67,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
6767

6868
// The argument is *not* a string literal.
6969

70-
let (span, panic) = panic_call(cx, f);
70+
let (span, panic, symbol_str) = panic_call(cx, f);
7171

7272
// Find the span of the argument to `panic!()`, before expansion in the
7373
// case of `panic!(some_macro!())`.
@@ -95,7 +95,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
9595
}
9696
if arg_macro.map_or(false, |id| cx.tcx.is_diagnostic_item(sym::format_macro, id)) {
9797
// A case of `panic!(format!(..))`.
98-
l.note("the panic!() macro supports formatting, so there's no need for the format!() macro here");
98+
l.note(format!("the {}!() macro supports formatting, so there's no need for the format!() macro here", symbol_str).as_str());
9999
if let Some((open, close, _)) = find_delimiters(cx, arg_span) {
100100
l.multipart_suggestion(
101101
"remove the `format!(..)` macro call",
@@ -160,7 +160,7 @@ fn check_panic_str<'tcx>(
160160
Parser::new(fmt.as_ref(), style, snippet.clone(), false, ParseMode::Format);
161161
let n_arguments = (&mut fmt_parser).filter(|a| matches!(a, Piece::NextArgument(_))).count();
162162

163-
let (span, _) = panic_call(cx, f);
163+
let (span, _, _) = panic_call(cx, f);
164164

165165
if n_arguments > 0 && fmt_parser.errors.is_empty() {
166166
let arg_spans: Vec<_> = match &fmt_parser.arg_places[..] {
@@ -230,7 +230,7 @@ fn find_delimiters<'tcx>(cx: &LateContext<'tcx>, span: Span) -> Option<(Span, Sp
230230
))
231231
}
232232

233-
fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span, Symbol) {
233+
fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span, Symbol, SymbolStr) {
234234
let mut expn = f.span.ctxt().outer_expn_data();
235235

236236
let mut panic_macro = kw::Empty;
@@ -248,5 +248,10 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
248248
}
249249
}
250250

251-
(expn.call_site, panic_macro)
251+
let macro_symbol = if let hygiene::ExpnKind::Macro(_, symbol) = expn.kind {
252+
symbol
253+
} else {
254+
Symbol::intern("panic")
255+
};
256+
(expn.call_site, panic_macro, macro_symbol.as_str())
252257
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ fn main() {
3636
panic!(a!()); //~ WARN panic message is not a string literal
3737

3838
panic!(format!("{}", 1)); //~ WARN panic message is not a string literal
39+
assert!(false, format!("{}", 1)); //~ WARN panic message is not a string literal
40+
debug_assert!(false, format!("{}", 1)); //~ WARN panic message is not a string literal
3941

4042
panic![123]; //~ WARN panic message is not a string literal
4143
panic!{123}; //~ WARN panic message is not a string literal

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

+29-3
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,33 @@ LL | panic!("{}", 1);
213213
| -- --
214214

215215
warning: panic message is not a string literal
216-
--> $DIR/non-fmt-panic.rs:40:12
216+
--> $DIR/non-fmt-panic.rs:39:20
217+
|
218+
LL | assert!(false, format!("{}", 1));
219+
| ^^^^^^^^^^^^^^^^
220+
|
221+
= note: this is no longer accepted in Rust 2021
222+
= note: the assert!() macro supports formatting, so there's no need for the format!() macro here
223+
help: remove the `format!(..)` macro call
224+
|
225+
LL | assert!(false, "{}", 1);
226+
| -- --
227+
228+
warning: panic message is not a string literal
229+
--> $DIR/non-fmt-panic.rs:40:26
230+
|
231+
LL | debug_assert!(false, format!("{}", 1));
232+
| ^^^^^^^^^^^^^^^^
233+
|
234+
= note: this is no longer accepted in Rust 2021
235+
= note: the debug_assert!() macro supports formatting, so there's no need for the format!() macro here
236+
help: remove the `format!(..)` macro call
237+
|
238+
LL | debug_assert!(false, "{}", 1);
239+
| -- --
240+
241+
warning: panic message is not a string literal
242+
--> $DIR/non-fmt-panic.rs:42:12
217243
|
218244
LL | panic![123];
219245
| ^^^
@@ -229,7 +255,7 @@ LL | std::panic::panic_any(123);
229255
| ^^^^^^^^^^^^^^^^^^^^^^ ^
230256

231257
warning: panic message is not a string literal
232-
--> $DIR/non-fmt-panic.rs:41:12
258+
--> $DIR/non-fmt-panic.rs:43:12
233259
|
234260
LL | panic!{123};
235261
| ^^^
@@ -244,5 +270,5 @@ help: or use std::panic::panic_any instead
244270
LL | std::panic::panic_any(123);
245271
| ^^^^^^^^^^^^^^^^^^^^^^ ^
246272

247-
warning: 18 warnings emitted
273+
warning: 20 warnings emitted
248274

0 commit comments

Comments
 (0)