Skip to content

Commit 4a7a98c

Browse files
committed
Fix diagnostics being cancelled even with unused arguments
1 parent dc75c99 commit 4a7a98c

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use rustc_ast::{
88
FormatDebugHex, FormatOptions, FormatPlaceholder, FormatSign, FormatTrait,
99
};
1010
use rustc_data_structures::fx::FxHashSet;
11-
use rustc_errors::{Applicability, MultiSpan, PResult, SingleLabelManySpans};
11+
use rustc_errors::{
12+
Applicability, DiagnosticBuilder, ErrorGuaranteed, MultiSpan, PResult, SingleLabelManySpans,
13+
};
1214
use rustc_expand::base::{self, *};
1315
use rustc_parse_format as parse;
1416
use rustc_span::symbol::{Ident, Symbol};
@@ -616,9 +618,13 @@ fn report_missing_placeholders(
616618
.collect::<Vec<_>>();
617619

618620
if !placeholders.is_empty() {
619-
report_redundant_format_arguments(ecx, fmt_span, &args, used, placeholders);
620-
diag.cancel();
621-
return;
621+
if let Some(mut new_diag) =
622+
report_redundant_format_arguments(ecx, fmt_span, &args, used, placeholders)
623+
{
624+
diag.cancel();
625+
new_diag.emit();
626+
return;
627+
}
622628
}
623629

624630
// Used to ensure we only report translations for *one* kind of foreign format.
@@ -710,13 +716,13 @@ fn report_missing_placeholders(
710716

711717
/// This function detects and reports unused format!() arguments that are
712718
/// redundant due to implicit captures (e.g. `format!("{x}", x)`).
713-
fn report_redundant_format_arguments(
714-
ecx: &mut ExtCtxt<'_>,
719+
fn report_redundant_format_arguments<'a>(
720+
ecx: &mut ExtCtxt<'a>,
715721
fmt_span: Span,
716722
args: &FormatArguments,
717723
used: &[bool],
718724
placeholders: Vec<(Span, &str)>,
719-
) {
725+
) -> Option<DiagnosticBuilder<'a, ErrorGuaranteed>> {
720726
let mut fmt_arg_indices = vec![];
721727
let mut args_spans = vec![];
722728
let mut fmt_spans = vec![];
@@ -762,15 +768,15 @@ fn report_redundant_format_arguments(
762768
suggestion_spans.push(span);
763769
}
764770

765-
let mut diag = ecx.create_err(errors::FormatRedundantArgs {
771+
return Some(ecx.create_err(errors::FormatRedundantArgs {
766772
fmt_span,
767773
note: multispan,
768774
n: args_spans.len(),
769775
sugg: errors::FormatRedundantArgsSugg { spans: suggestion_spans },
770-
});
771-
772-
diag.emit();
776+
}));
773777
}
778+
779+
None
774780
}
775781

776782
/// Handle invalid references to positional arguments. Output different

0 commit comments

Comments
 (0)