Skip to content

Commit ef778e7

Browse files
committed
Fix span in non_fmt_panic for panic!(some_macro!()).
1 parent 07194ff commit ef778e7

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

compiler/rustc_lint/src/non_fmt_panic.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,30 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
6969

7070
let (span, panic) = panic_call(cx, f);
7171

72-
cx.struct_span_lint(NON_FMT_PANIC, arg.span, |lint| {
72+
// Find the span of the argument to `panic!()`, before expansion in the
73+
// case of `panic!(some_macro!())`.
74+
let mut arg_span = arg.span;
75+
while !span.contains(arg_span) {
76+
let expn = arg_span.ctxt().outer_expn_data();
77+
if expn.is_root() {
78+
break;
79+
}
80+
arg_span = expn.call_site;
81+
}
82+
83+
cx.struct_span_lint(NON_FMT_PANIC, arg_span, |lint| {
7384
let mut l = lint.build("panic message is not a string literal");
7485
l.note("this is no longer accepted in Rust 2021");
75-
if span.contains(arg.span) {
86+
if span.contains(arg_span) {
7687
l.span_suggestion_verbose(
77-
arg.span.shrink_to_lo(),
88+
arg_span.shrink_to_lo(),
7889
"add a \"{}\" format string to Display the message",
7990
"\"{}\", ".into(),
8091
Applicability::MaybeIncorrect,
8192
);
8293
if panic == sym::std_panic_macro {
8394
l.span_suggestion_verbose(
84-
span.until(arg.span),
95+
span.until(arg_span),
8596
"or use std::panic::panic_any instead",
8697
"std::panic::panic_any(".into(),
8798
Applicability::MachineApplicable,

0 commit comments

Comments
 (0)