From 2902b92769a29d24f9107d2e322ed9c92990da98 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 15 Jul 2022 17:32:15 +0000 Subject: [PATCH] Only suggest if span is not erroneous --- compiler/rustc_builtin_macros/src/format.rs | 2 +- compiler/rustc_lint/src/context.rs | 19 ++++++++++--------- compiler/rustc_lint_defs/src/lib.rs | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs index d5eb8d4eeb9..3b7e2102ffa 100644 --- a/compiler/rustc_builtin_macros/src/format.rs +++ b/compiler/rustc_builtin_macros/src/format.rs @@ -997,7 +997,7 @@ fn lint_named_arguments_used_positionally( for (symbol, (index, span)) in names { if !used_argument_names.contains(symbol.as_str()) { let msg = format!("named argument `{}` is not used by name", symbol.as_str()); - let arg_span = cx.arg_spans.get(index).copied().unwrap_or(span); + let arg_span = cx.arg_spans.get(index).copied(); cx.ecx.buffered_early_lint.push(BufferedEarlyLint { span: MultiSpan::from_span(span), msg: msg.clone(), diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index 50e9383cacc..b4b472fe2df 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -858,15 +858,16 @@ fn lookup_with_diagnostics( }, BuiltinLintDiagnostics::NamedArgumentUsedPositionally(positional_arg, named_arg, name) => { db.span_label(named_arg, "this named argument is only referred to by position in formatting string"); - let msg = format!("this formatting argument uses named argument `{}` by position", name); - db.span_label(positional_arg, msg); - db.span_suggestion_verbose( - positional_arg, - "use the named argument by name to avoid ambiguity", - format!("{{{}}}", name), - Applicability::MaybeIncorrect, - ); - + if let Some(positional_arg) = positional_arg { + let msg = format!("this formatting argument uses named argument `{}` by position", name); + db.span_label(positional_arg, msg); + db.span_suggestion_verbose( + positional_arg, + "use the named argument by name to avoid ambiguity", + format!("{{{}}}", name), + Applicability::MaybeIncorrect, + ); + } } } // Rewrap `db`, and pass control to the user. diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 1bc7e7de660..4fd57ed8533 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -467,7 +467,7 @@ pub enum BuiltinLintDiagnostics { /// If true, the lifetime will be fully elided. use_span: Option<(Span, bool)>, }, - NamedArgumentUsedPositionally(Span, Span, String), + NamedArgumentUsedPositionally(Option, Span, String), } /// Lints that are buffered up early on in the `Session` before the