@@ -69,19 +69,30 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
69
69
70
70
let ( span, panic) = panic_call ( cx, f) ;
71
71
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| {
73
84
let mut l = lint. build ( "panic message is not a string literal" ) ;
74
85
l. note ( "this is no longer accepted in Rust 2021" ) ;
75
- if span. contains ( arg . span ) {
86
+ if span. contains ( arg_span ) {
76
87
l. span_suggestion_verbose (
77
- arg . span . shrink_to_lo ( ) ,
88
+ arg_span . shrink_to_lo ( ) ,
78
89
"add a \" {}\" format string to Display the message" ,
79
90
"\" {}\" , " . into ( ) ,
80
91
Applicability :: MaybeIncorrect ,
81
92
) ;
82
93
if panic == sym:: std_panic_macro {
83
94
l. span_suggestion_verbose (
84
- span. until ( arg . span ) ,
95
+ span. until ( arg_span ) ,
85
96
"or use std::panic::panic_any instead" ,
86
97
"std::panic::panic_any(" . into ( ) ,
87
98
Applicability :: MachineApplicable ,
0 commit comments