Skip to content

Commit aff682a

Browse files
committed
Fix diagnostics being cancelled even with unused arguments
1 parent 59fc6e8 commit aff682a

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};
@@ -607,9 +609,13 @@ fn report_missing_placeholders(
607609
.collect::<Vec<_>>();
608610

609611
if !placeholders.is_empty() {
610-
report_redundant_format_arguments(ecx, fmt_span, &args, used, placeholders);
611-
diag.cancel();
612-
return;
612+
if let Some(mut new_diag) =
613+
report_redundant_format_arguments(ecx, fmt_span, &args, used, placeholders)
614+
{
615+
diag.cancel();
616+
new_diag.emit();
617+
return;
618+
}
613619
}
614620

615621
// Used to ensure we only report translations for *one* kind of foreign format.
@@ -701,13 +707,13 @@ fn report_missing_placeholders(
701707

702708
/// This function detects and reports unused format!() arguments that are
703709
/// redundant due to implicit captures (e.g. `format!("{x}", x)`).
704-
fn report_redundant_format_arguments(
705-
ecx: &mut ExtCtxt<'_>,
710+
fn report_redundant_format_arguments<'a>(
711+
ecx: &mut ExtCtxt<'a>,
706712
fmt_span: Span,
707713
args: &FormatArguments,
708714
used: &[bool],
709715
placeholders: Vec<(Span, &str)>,
710-
) {
716+
) -> Option<DiagnosticBuilder<'a, ErrorGuaranteed>> {
711717
let mut fmt_arg_indices = vec![];
712718
let mut args_spans = vec![];
713719
let mut fmt_spans = vec![];
@@ -753,15 +759,15 @@ fn report_redundant_format_arguments(
753759
suggestion_spans.push(span);
754760
}
755761

756-
let mut diag = ecx.create_err(errors::FormatRedundantArgs {
762+
return Some(ecx.create_err(errors::FormatRedundantArgs {
757763
fmt_span,
758764
note: multispan,
759765
n: args_spans.len(),
760766
sugg: errors::FormatRedundantArgsSugg { spans: suggestion_spans },
761-
});
762-
763-
diag.emit();
767+
}));
764768
}
769+
770+
None
765771
}
766772

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

0 commit comments

Comments
 (0)