about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2021-02-14 18:14:23 +0100
committerMara Bos <m-ou.se@m-ou.se>2021-02-14 18:14:23 +0100
commitef778e796502691ebfb1f3dd5c03248aea6d2ac8 (patch)
tree5ac094546ff4db983fd24942a078f501c852f4b1
parent07194ffcd25b0871ce560b9f702e52db27ac9f77 (diff)
downloadrust-ef778e796502691ebfb1f3dd5c03248aea6d2ac8.tar.gz
rust-ef778e796502691ebfb1f3dd5c03248aea6d2ac8.zip
Fix span in non_fmt_panic for panic!(some_macro!()).
-rw-r--r--compiler/rustc_lint/src/non_fmt_panic.rs19
1 files changed, 15 insertions, 4 deletions
diff --git a/compiler/rustc_lint/src/non_fmt_panic.rs b/compiler/rustc_lint/src/non_fmt_panic.rs
index e98297b692c..0c5456cf619 100644
--- a/compiler/rustc_lint/src/non_fmt_panic.rs
+++ b/compiler/rustc_lint/src/non_fmt_panic.rs
@@ -69,19 +69,30 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
 
     let (span, panic) = panic_call(cx, f);
 
-    cx.struct_span_lint(NON_FMT_PANIC, arg.span, |lint| {
+    // Find the span of the argument to `panic!()`, before expansion in the
+    // case of `panic!(some_macro!())`.
+    let mut arg_span = arg.span;
+    while !span.contains(arg_span) {
+        let expn = arg_span.ctxt().outer_expn_data();
+        if expn.is_root() {
+            break;
+        }
+        arg_span = expn.call_site;
+    }
+
+    cx.struct_span_lint(NON_FMT_PANIC, arg_span, |lint| {
         let mut l = lint.build("panic message is not a string literal");
         l.note("this is no longer accepted in Rust 2021");
-        if span.contains(arg.span) {
+        if span.contains(arg_span) {
             l.span_suggestion_verbose(
-                arg.span.shrink_to_lo(),
+                arg_span.shrink_to_lo(),
                 "add a \"{}\" format string to Display the message",
                 "\"{}\", ".into(),
                 Applicability::MaybeIncorrect,
             );
             if panic == sym::std_panic_macro {
                 l.span_suggestion_verbose(
-                    span.until(arg.span),
+                    span.until(arg_span),
                     "or use std::panic::panic_any instead",
                     "std::panic::panic_any(".into(),
                     Applicability::MachineApplicable,