diff options
| author | francorbacho <francorbacho@proton.me> | 2023-10-05 14:23:02 +0200 |
|---|---|---|
| committer | francorbacho <francorbacho@proton.me> | 2023-10-05 16:09:57 +0200 |
| commit | 4a7a98cf3058f68f94c3cac1e6dfaa2f3282680c (patch) | |
| tree | 56f10f91f95cdb15b7d8d254c5193e4ce33d029b | |
| parent | dc75c99226f948c3459f9970dbd6fe0b5d2bbef2 (diff) | |
| download | rust-4a7a98cf3058f68f94c3cac1e6dfaa2f3282680c.tar.gz rust-4a7a98cf3058f68f94c3cac1e6dfaa2f3282680c.zip | |
Fix diagnostics being cancelled even with unused arguments
| -rw-r--r-- | compiler/rustc_builtin_macros/src/format.rs | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs index cdd5e7225bb..d7fc48026cb 100644 --- a/compiler/rustc_builtin_macros/src/format.rs +++ b/compiler/rustc_builtin_macros/src/format.rs @@ -8,7 +8,9 @@ use rustc_ast::{ FormatDebugHex, FormatOptions, FormatPlaceholder, FormatSign, FormatTrait, }; use rustc_data_structures::fx::FxHashSet; -use rustc_errors::{Applicability, MultiSpan, PResult, SingleLabelManySpans}; +use rustc_errors::{ + Applicability, DiagnosticBuilder, ErrorGuaranteed, MultiSpan, PResult, SingleLabelManySpans, +}; use rustc_expand::base::{self, *}; use rustc_parse_format as parse; use rustc_span::symbol::{Ident, Symbol}; @@ -616,9 +618,13 @@ fn report_missing_placeholders( .collect::<Vec<_>>(); if !placeholders.is_empty() { - report_redundant_format_arguments(ecx, fmt_span, &args, used, placeholders); - diag.cancel(); - return; + if let Some(mut new_diag) = + report_redundant_format_arguments(ecx, fmt_span, &args, used, placeholders) + { + diag.cancel(); + new_diag.emit(); + return; + } } // Used to ensure we only report translations for *one* kind of foreign format. @@ -710,13 +716,13 @@ fn report_missing_placeholders( /// This function detects and reports unused format!() arguments that are /// redundant due to implicit captures (e.g. `format!("{x}", x)`). -fn report_redundant_format_arguments( - ecx: &mut ExtCtxt<'_>, +fn report_redundant_format_arguments<'a>( + ecx: &mut ExtCtxt<'a>, fmt_span: Span, args: &FormatArguments, used: &[bool], placeholders: Vec<(Span, &str)>, -) { +) -> Option<DiagnosticBuilder<'a, ErrorGuaranteed>> { let mut fmt_arg_indices = vec![]; let mut args_spans = vec![]; let mut fmt_spans = vec![]; @@ -762,15 +768,15 @@ fn report_redundant_format_arguments( suggestion_spans.push(span); } - let mut diag = ecx.create_err(errors::FormatRedundantArgs { + return Some(ecx.create_err(errors::FormatRedundantArgs { fmt_span, note: multispan, n: args_spans.len(), sugg: errors::FormatRedundantArgsSugg { spans: suggestion_spans }, - }); - - diag.emit(); + })); } + + None } /// Handle invalid references to positional arguments. Output different |
