Skip to content

Commit be8c8e9

Browse files
Rollup merge of rust-lang#87967 - m-ou-se:non-fmt-panic-detect-fake-spans, r=cjgillot
Detect fake spans in non_fmt_panic lint. This addresses rust-lang#87621 Some proc_macros claim that the user wrote all of the tokens it outputs, by applying a span from the input to all of the produced tokens. That can result in confusing suggestions, as in rust-lang#87621. This is a simple patch that avoids suggesting anything for `panic!("{}")` if the span of `"{}"` and `panic!(..)` are identical, which is normally not possible.
2 parents 8feadd7 + 079bf75 commit be8c8e9

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

compiler/rustc_lint/src/non_fmt_panic.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
101101
let mut l = lint.build("panic message is not a string literal");
102102
l.note("this usage of panic!() is deprecated; it will be a hard error in Rust 2021");
103103
l.note("for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/panic-macro-consistency.html>");
104-
if !span.contains(arg_span) {
104+
if !is_arg_inside_call(arg_span, span) {
105105
// No clue where this argument is coming from.
106106
l.emit();
107107
return;
@@ -191,7 +191,7 @@ fn check_panic_str<'tcx>(
191191
_ => "panic message contains unused formatting placeholders",
192192
});
193193
l.note("this message is not used as a format string when given without arguments, but will be in Rust 2021");
194-
if span.contains(arg.span) {
194+
if is_arg_inside_call(arg.span, span) {
195195
l.span_suggestion(
196196
arg.span.shrink_to_hi(),
197197
&format!("add the missing argument{}", pluralize!(n_arguments)),
@@ -222,7 +222,7 @@ fn check_panic_str<'tcx>(
222222
cx.struct_span_lint(NON_FMT_PANICS, brace_spans.unwrap_or_else(|| vec![span]), |lint| {
223223
let mut l = lint.build(msg);
224224
l.note("this message is not used as a format string, but will be in Rust 2021");
225-
if span.contains(arg.span) {
225+
if is_arg_inside_call(arg.span, span) {
226226
l.span_suggestion(
227227
arg.span.shrink_to_lo(),
228228
"add a \"{}\" format string to use the message literally",
@@ -270,3 +270,11 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
270270
if let hygiene::ExpnKind::Macro(_, symbol) = expn.kind { symbol } else { sym::panic };
271271
(expn.call_site, panic_macro, macro_symbol.as_str())
272272
}
273+
274+
fn is_arg_inside_call(arg: Span, call: Span) -> bool {
275+
// We only add suggestions if the argument we're looking at appears inside the
276+
// panic call in the source file, to avoid invalid suggestions when macros are involved.
277+
// We specifically check for the spans to not be identical, as that happens sometimes when
278+
// proc_macros lie about spans and apply the same span to all the tokens they produce.
279+
call.contains(arg) && !call.source_equal(&arg)
280+
}

0 commit comments

Comments
 (0)