1use std::ops::Range;
2
3use parse::Position::ArgumentNamed;
4use rustc_ast::tokenstream::TokenStream;
5use rustc_ast::{
6 Expr, ExprKind, FormatAlignment, FormatArgPosition, FormatArgPositionKind, FormatArgs,
7 FormatArgsPiece, FormatArgument, FormatArgumentKind, FormatArguments, FormatCount,
8 FormatDebugHex, FormatOptions, FormatPlaceholder, FormatSign, FormatTrait, Recovered, StmtKind,
9 token,
10};
11use rustc_data_structures::fx::FxHashSet;
12use rustc_errors::{
13 Applicability, BufferedEarlyLint, DecorateDiagCompat, Diag, Diagnostic, MultiSpan, PResult,
14 SingleLabelManySpans, listify, pluralize,
15};
16use rustc_expand::base::*;
17use rustc_lint_defs::LintId;
18use rustc_lint_defs::builtin::NAMED_ARGUMENTS_USED_POSITIONALLY;
19use rustc_parse::exp;
20use rustc_parse_format as parse;
21use rustc_span::{BytePos, ErrorGuaranteed, Ident, InnerSpan, Span, Symbol};
22
23use crate::diagnostics;
24use crate::util::{ExprToSpannedString, expr_to_spanned_string};
25
26#[derive(#[automatically_derived]
impl ::core::clone::Clone for PositionUsedAs {
#[inline]
fn clone(&self) -> PositionUsedAs {
let _: ::core::clone::AssertParamIsClone<Option<Span>>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for PositionUsedAs { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for PositionUsedAs {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
PositionUsedAs::Placeholder(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Placeholder", &__self_0),
PositionUsedAs::Precision =>
::core::fmt::Formatter::write_str(f, "Precision"),
PositionUsedAs::Width =>
::core::fmt::Formatter::write_str(f, "Width"),
}
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for PositionUsedAs {
#[inline]
fn eq(&self, other: &PositionUsedAs) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(PositionUsedAs::Placeholder(__self_0),
PositionUsedAs::Placeholder(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for PositionUsedAs {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Option<Span>>;
}
}Eq)]
39enum PositionUsedAs {
40 Placeholder(Option<Span>),
41 Precision,
42 Width,
43}
44use PositionUsedAs::*;
45
46#[derive(#[automatically_derived]
impl ::core::fmt::Debug for MacroInput {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "MacroInput",
"fmtstr", &self.fmtstr, "args", &self.args, "is_direct_literal",
&&self.is_direct_literal)
}
}Debug)]
47struct MacroInput {
48 fmtstr: Box<Expr>,
49 args: FormatArguments,
50 is_direct_literal: bool,
60}
61
62fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a, MacroInput> {
72 let mut p = ecx.new_parser_from_tts(tts);
73
74 let fmtstr = match p.token.kind {
76 token::Eof => {
77 return Err(ecx.dcx().create_err(diagnostics::FormatRequiresString { span: sp }));
78 }
79 token::Literal(token::Lit { kind: token::Str | token::StrRaw(_), .. }) => {
83 p.parse_literal_maybe_minus()?
84 }
85 _ => p.parse_expr()?,
87 };
88
89 let mut args = FormatArguments::new();
91 let mut first = true;
92 while p.token != token::Eof {
93 if !p.eat(::rustc_parse::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: ::rustc_parse::parser::token_type::TokenType::Comma,
}exp!(Comma)) {
95 if first {
96 p.clear_expected_token_types();
97 }
98
99 match p.expect(::rustc_parse::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: ::rustc_parse::parser::token_type::TokenType::Comma,
}exp!(Comma)) {
100 Err(err) => {
101 if token::TokenKind::Comma.similar_tokens().contains(&p.token.kind) {
102 err.emit();
105 p.bump();
106 } else {
107 return Err(err);
109 }
110 }
111 Ok(Recovered::Yes(_)) => (),
112 Ok(Recovered::No) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
113 }
114 }
115 first = false;
116 if p.token == token::Eof {
118 break;
119 }
120 match p.token.ident() {
122 Some((ident, _)) if p.look_ahead(1, |t| *t == token::Eq) => {
123 p.bump();
124 p.expect(::rustc_parse::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: ::rustc_parse::parser::token_type::TokenType::Eq,
}exp!(Eq))?;
125 let expr = p.parse_expr()?;
126 if let Some((_, prev)) = args.by_name(ident.name) {
127 ecx.dcx().emit_err(diagnostics::FormatDuplicateArg {
128 span: ident.span,
129 prev: prev.kind.ident().unwrap().span,
130 duplicate: ident.span,
131 ident,
132 });
133 continue;
134 }
135 args.add(FormatArgument { kind: FormatArgumentKind::Named(ident), expr });
136 }
137 _ => {
138 let expr = p.parse_expr()?;
139 if !args.named_args().is_empty() {
140 return Err(ecx.dcx().create_err(diagnostics::PositionalAfterNamed {
141 span: expr.span,
142 args: args
143 .named_args()
144 .iter()
145 .filter_map(|a| a.kind.ident().map(|ident| (a, ident)))
146 .map(|(arg, n)| n.span.to(arg.expr.span))
147 .collect(),
148 }));
149 }
150 args.add(FormatArgument { kind: FormatArgumentKind::Normal, expr });
151 }
152 }
153 }
154
155 let is_direct_literal = #[allow(non_exhaustive_omitted_patterns)] match fmtstr.kind {
ExprKind::Lit(_) => true,
_ => false,
}matches!(fmtstr.kind, ExprKind::Lit(_));
157
158 Ok(MacroInput { fmtstr, args, is_direct_literal })
159}
160
161fn make_format_args(
162 ecx: &mut ExtCtxt<'_>,
163 input: MacroInput,
164 append_newline: bool,
165 macro_span: Span,
166) -> ExpandResult<Result<FormatArgs, ErrorGuaranteed>, ()> {
167 let msg = "format argument must be a string literal";
168 let unexpanded_fmt_span = input.fmtstr.span;
169
170 let MacroInput { fmtstr: efmt, mut args, is_direct_literal } = input;
171
172 let ExprToSpannedString {
173 symbol: fmt_str,
174 span: fmt_span,
175 style: fmt_style,
176 uncooked_symbol: uncooked_fmt_str,
177 } = {
178 let snippet = if let ExprKind::Block(b, None) = &efmt.kind
181 && b.stmts.len() <= 1
182 {
183 Some(ecx.sess.source_map().span_to_snippet(unexpanded_fmt_span))
184 } else {
185 None
186 };
187
188 let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, efmt.clone(), msg) else {
189 return ExpandResult::Retry(());
190 };
191 match mac {
192 Ok(mut fmt) if append_newline => {
193 fmt.symbol = Symbol::intern(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}\n", fmt.symbol))
})format!("{}\n", fmt.symbol));
194 fmt
195 }
196 Ok(fmt) => fmt,
197 Err(err) => {
198 let guar = match err {
199 Ok((mut err, suggested)) => {
200 if !suggested {
201 if let ExprKind::Block(block, None) = &efmt.kind
202 && let [stmt] = block.stmts.as_slice()
203 && let StmtKind::Expr(expr) = &stmt.kind
204 && let ExprKind::Path(None, path) = &expr.kind
205 && path.segments.len() == 1
206 && path.segments[0].args.is_none()
207 {
208 err.multipart_suggestion(
209 "quote your inlined format argument to use as string literal",
210 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(unexpanded_fmt_span.shrink_to_hi(), "\"".to_string()),
(unexpanded_fmt_span.shrink_to_lo(), "\"".to_string())]))vec![
211 (unexpanded_fmt_span.shrink_to_hi(), "\"".to_string()),
212 (unexpanded_fmt_span.shrink_to_lo(), "\"".to_string()),
213 ],
214 Applicability::MaybeIncorrect,
215 );
216 } else {
217 let should_suggest = |kind: &ExprKind| -> bool {
219 match kind {
220 ExprKind::Block(b, None) if b.stmts.is_empty() => true,
221 ExprKind::Tup(v) if v.is_empty() => true,
222 _ => false,
223 }
224 };
225
226 let mut sugg_fmt = String::new();
227 for kind in std::iter::once(&efmt.kind)
228 .chain(args.explicit_args().into_iter().map(|a| &a.expr.kind))
229 {
230 sugg_fmt.push_str(if should_suggest(kind) {
231 "{:?} "
232 } else {
233 "{} "
234 });
235 }
236 sugg_fmt = sugg_fmt.trim_end().to_string();
237 err.span_suggestion_verbose(
238 unexpanded_fmt_span.shrink_to_lo(),
239 "you might be missing a string literal to format with",
240 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\"{0}\", ", sugg_fmt))
})format!("\"{sugg_fmt}\", "),
241 Applicability::MaybeIncorrect,
242 );
243
244 if let Some(Ok(snippet)) = snippet.as_ref() {
245 match snippet.as_str() {
246 "{}" | "{:?}" | "{:#?}" => {
247 err.span_suggestion_verbose(
248 unexpanded_fmt_span,
249 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you might want to enclose `{0}` with `\"\"`",
snippet))
})format!("you might want to enclose `{snippet}` with `\"\"`"),
250 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\"{0}\"", snippet))
})format!("\"{snippet}\""),
251 Applicability::MaybeIncorrect,
252 );
253 }
254 _ => {}
255 };
256 }
257 }
258 }
259 err.emit()
260 }
261 Err(guar) => guar,
262 };
263 return ExpandResult::Ready(Err(guar));
264 }
265 }
266 };
267
268 let str_style = match fmt_style {
269 rustc_ast::StrStyle::Cooked => None,
270 rustc_ast::StrStyle::Raw(raw) => Some(raw as usize),
271 };
272
273 let fmt_str = fmt_str.as_str(); let fmt_snippet = ecx.source_map().span_to_snippet(unexpanded_fmt_span).ok();
275 let mut parser = parse::Parser::new(
276 fmt_str,
277 str_style,
278 fmt_snippet,
279 append_newline,
280 parse::ParseMode::Format,
281 );
282
283 let mut pieces = Vec::new();
284 while let Some(piece) = parser.next() {
285 if !parser.errors.is_empty() {
286 break;
287 } else {
288 pieces.push(piece);
289 }
290 }
291
292 let is_source_literal = parser.is_source_literal;
293
294 if !parser.errors.is_empty() {
295 let err = parser.errors.remove(0);
296 let sp = if is_source_literal {
297 fmt_span.from_inner(InnerSpan::new(err.span.start, err.span.end))
298 } else {
299 fmt_span
308 };
309 let mut e = diagnostics::InvalidFormatString {
310 span: sp,
311 note_: None,
312 label_: None,
313 sugg_: None,
314 desc: err.description,
315 label1: err.label,
316 };
317 if let Some(note) = err.note {
318 e.note_ = Some(diagnostics::InvalidFormatStringNote { note });
319 }
320 if let Some((label, span)) = err.secondary_label
321 && is_source_literal
322 {
323 e.label_ = Some(diagnostics::InvalidFormatStringLabel {
324 span: fmt_span.from_inner(InnerSpan::new(span.start, span.end)),
325 label,
326 });
327 }
328 match err.suggestion {
329 parse::Suggestion::None => {}
330 parse::Suggestion::UsePositional => {
331 let captured_arg_span =
332 fmt_span.from_inner(InnerSpan::new(err.span.start, err.span.end));
333 if let Ok(arg) = ecx.source_map().span_to_snippet(captured_arg_span) {
334 let span = match args.unnamed_args().last() {
335 Some(arg) => arg.expr.span,
336 None => fmt_span,
337 };
338 e.sugg_ = Some(diagnostics::InvalidFormatStringSuggestion::UsePositional {
339 captured: captured_arg_span,
340 len: args.unnamed_args().len().to_string(),
341 span: span.shrink_to_hi(),
342 arg,
343 });
344 }
345 }
346 parse::Suggestion::RemoveRawIdent(span) => {
347 if is_source_literal {
348 let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
349 e.sugg_ =
350 Some(diagnostics::InvalidFormatStringSuggestion::RemoveRawIdent { span })
351 }
352 }
353 parse::Suggestion::ReorderFormatParameter(span, replacement) => {
354 let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
355 e.sugg_ =
356 Some(diagnostics::InvalidFormatStringSuggestion::ReorderFormatParameter {
357 span,
358 replacement,
359 });
360 }
361 parse::Suggestion::AddMissingColon(span) => {
362 let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
363 e.sugg_ =
364 Some(diagnostics::InvalidFormatStringSuggestion::AddMissingColon { span });
365 }
366 parse::Suggestion::UseRustDebugPrintingMacro => {
367 if let [arg] = args.all_args() {
369 let expr_span = arg.expr.span;
370 if let Ok(expr_snippet) = ecx.source_map().span_to_snippet(expr_span) {
371 let replacement = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}!({1})", "dbg", expr_snippet))
})format!("{}!({})", "dbg", expr_snippet);
372
373 let call_span = macro_span.source_callsite();
374 e.sugg_ = Some(
375 diagnostics::InvalidFormatStringSuggestion::UseRustDebugPrintingMacro {
376 macro_span: call_span,
377 replacement,
378 },
379 );
380 }
381 }
382 }
383 }
384 let guar = ecx.dcx().emit_err(e);
385 return ExpandResult::Ready(Err(guar));
386 }
387
388 let to_span = |inner_span: Range<usize>| {
389 is_source_literal.then(|| {
390 fmt_span.from_inner(InnerSpan { start: inner_span.start, end: inner_span.end })
391 })
392 };
393
394 let mut used = ::alloc::vec::from_elem(false, args.explicit_args().len())vec![false; args.explicit_args().len()];
395 let mut invalid_refs = Vec::new();
396 let mut numeric_references_to_named_arg = Vec::new();
397
398 enum ArgRef<'a> {
399 Index(usize),
400 Name(&'a str, Option<Span>),
401 }
402 use ArgRef::*;
403
404 let mut unnamed_arg_after_named_arg = false;
405
406 let mut lookup_arg = |arg: ArgRef<'_>,
407 span: Option<Span>,
408 used_as: PositionUsedAs,
409 kind: FormatArgPositionKind|
410 -> FormatArgPosition {
411 let index = match arg {
412 Index(index) => {
413 if let Some(arg) = args.by_index(index) {
414 used[index] = true;
415 if arg.kind.ident().is_some() {
416 numeric_references_to_named_arg.push((index, span, used_as));
418 }
419 Ok(index)
420 } else {
421 invalid_refs.push((index, span, used_as, kind));
423 Err(index)
424 }
425 }
426 Name(name, span) => {
427 let name = Symbol::intern(name);
428 if let Some((index, _)) = args.by_name(name) {
429 if index < args.explicit_args().len() {
431 used[index] = true;
433 }
434 Ok(index)
435 } else {
436 let span = span.unwrap_or(fmt_span);
438 let ident = Ident::new(name, span);
439 let expr = if is_direct_literal {
440 ecx.expr_ident(span, ident)
441 } else {
442 let guar = ecx.dcx().emit_err(diagnostics::FormatNoArgNamed { span, name });
445 unnamed_arg_after_named_arg = true;
446 DummyResult::raw_expr(span, Some(guar))
447 };
448 Ok(args.add(FormatArgument { kind: FormatArgumentKind::Captured(ident), expr }))
449 }
450 }
451 };
452 FormatArgPosition { index, kind, span }
453 };
454
455 let mut template = Vec::new();
456 let mut unfinished_literal = String::new();
457 let mut placeholder_index = 0;
458
459 for piece in &pieces {
460 match piece.clone() {
461 parse::Piece::Lit(s) => {
462 unfinished_literal.push_str(s);
463 }
464 parse::Piece::NextArgument(parse::Argument { position, position_span, format }) => {
465 if !unfinished_literal.is_empty() {
466 template.push(FormatArgsPiece::Literal(Symbol::intern(&unfinished_literal)));
467 unfinished_literal.clear();
468 }
469
470 let span =
471 parser.arg_places.get(placeholder_index).and_then(|s| to_span(s.clone()));
472 placeholder_index += 1;
473
474 let position_span = to_span(position_span);
475 let argument = match position {
476 parse::ArgumentImplicitlyIs(i) => lookup_arg(
477 Index(i),
478 position_span,
479 Placeholder(span),
480 FormatArgPositionKind::Implicit,
481 ),
482 parse::ArgumentIs(i) => lookup_arg(
483 Index(i),
484 position_span,
485 Placeholder(span),
486 FormatArgPositionKind::Number,
487 ),
488 parse::ArgumentNamed(name) => lookup_arg(
489 Name(name, position_span),
490 position_span,
491 Placeholder(span),
492 FormatArgPositionKind::Named,
493 ),
494 };
495
496 let alignment = match format.align {
497 parse::AlignUnknown => None,
498 parse::AlignLeft => Some(FormatAlignment::Left),
499 parse::AlignRight => Some(FormatAlignment::Right),
500 parse::AlignCenter => Some(FormatAlignment::Center),
501 };
502
503 let format_trait = match format.ty {
504 "" => FormatTrait::Display,
505 "?" => FormatTrait::Debug,
506 "e" => FormatTrait::LowerExp,
507 "E" => FormatTrait::UpperExp,
508 "o" => FormatTrait::Octal,
509 "p" => FormatTrait::Pointer,
510 "b" => FormatTrait::Binary,
511 "x" => FormatTrait::LowerHex,
512 "X" => FormatTrait::UpperHex,
513 _ => {
514 invalid_placeholder_type_error(ecx, format.ty, format.ty_span, fmt_span);
515 FormatTrait::Display
516 }
517 };
518
519 let precision_span = format.precision_span.and_then(to_span);
520 let precision = match format.precision {
521 parse::CountIs(n) => Some(FormatCount::Literal(n)),
522 parse::CountIsName(name, name_span) => Some(FormatCount::Argument(lookup_arg(
523 Name(name, to_span(name_span)),
524 precision_span,
525 Precision,
526 FormatArgPositionKind::Named,
527 ))),
528 parse::CountIsParam(i) => Some(FormatCount::Argument(lookup_arg(
529 Index(i),
530 precision_span,
531 Precision,
532 FormatArgPositionKind::Number,
533 ))),
534 parse::CountIsStar(i) => Some(FormatCount::Argument(lookup_arg(
535 Index(i),
536 precision_span,
537 Precision,
538 FormatArgPositionKind::Implicit,
539 ))),
540 parse::CountImplied => None,
541 };
542
543 let width_span = format.width_span.and_then(to_span);
544 let width = match format.width {
545 parse::CountIs(n) => Some(FormatCount::Literal(n)),
546 parse::CountIsName(name, name_span) => Some(FormatCount::Argument(lookup_arg(
547 Name(name, to_span(name_span)),
548 width_span,
549 Width,
550 FormatArgPositionKind::Named,
551 ))),
552 parse::CountIsParam(i) => Some(FormatCount::Argument(lookup_arg(
553 Index(i),
554 width_span,
555 Width,
556 FormatArgPositionKind::Number,
557 ))),
558 parse::CountIsStar(_) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
559 parse::CountImplied => None,
560 };
561
562 template.push(FormatArgsPiece::Placeholder(FormatPlaceholder {
563 argument,
564 span,
565 format_trait,
566 format_options: FormatOptions {
567 fill: format.fill,
568 alignment,
569 sign: format.sign.map(|s| match s {
570 parse::Sign::Plus => FormatSign::Plus,
571 parse::Sign::Minus => FormatSign::Minus,
572 }),
573 alternate: format.alternate,
574 zero_pad: format.zero_pad,
575 debug_hex: format.debug_hex.map(|s| match s {
576 parse::DebugHex::Lower => FormatDebugHex::Lower,
577 parse::DebugHex::Upper => FormatDebugHex::Upper,
578 }),
579 precision,
580 width,
581 },
582 }));
583 }
584 }
585 }
586
587 if !unfinished_literal.is_empty() {
588 template.push(FormatArgsPiece::Literal(Symbol::intern(&unfinished_literal)));
589 }
590
591 if !invalid_refs.is_empty() {
592 report_invalid_references(ecx, &invalid_refs, &template, fmt_span, &args, parser);
593 }
594
595 let unused = used
596 .iter()
597 .enumerate()
598 .filter(|&(_, used)| !used)
599 .map(|(i, _)| {
600 let named = #[allow(non_exhaustive_omitted_patterns)] match args.explicit_args()[i].kind {
FormatArgumentKind::Named(_) => true,
_ => false,
}matches!(args.explicit_args()[i].kind, FormatArgumentKind::Named(_));
601 (args.explicit_args()[i].expr.span, named)
602 })
603 .collect::<Vec<_>>();
604
605 let has_unused = !unused.is_empty();
606 if has_unused {
607 let detect_foreign_fmt = unused.len() > args.explicit_args().len() / 2;
610 report_missing_placeholders(
611 ecx,
612 unused,
613 &used,
614 &args,
615 &pieces,
616 &invalid_refs,
617 detect_foreign_fmt,
618 str_style,
619 fmt_str,
620 uncooked_fmt_str.1.as_str(),
621 fmt_span,
622 );
623 }
624
625 if invalid_refs.is_empty() && !has_unused && !unnamed_arg_after_named_arg {
628 for &(index, span, used_as) in &numeric_references_to_named_arg {
629 let (position_sp_to_replace, position_sp_for_msg) = match used_as {
630 Placeholder(pspan) => (span, pspan),
631 Precision => {
632 let span = span.map(|span| span.with_lo(span.lo() + BytePos(1)));
634 (span, span)
635 }
636 Width => (span, span),
637 };
638 let arg_name = args.explicit_args()[index].kind.ident().unwrap();
639 ecx.buffered_early_lint.push(BufferedEarlyLint {
640 span: Some(arg_name.span.into()),
641 node_id: rustc_ast::CRATE_NODE_ID,
642 lint_id: LintId::of(NAMED_ARGUMENTS_USED_POSITIONALLY),
643 diagnostic: DecorateDiagCompat(Box::new(move |dcx, level, sess| {
644 let (suggestion, name) =
645 if let Some(positional_arg_to_replace) = position_sp_to_replace {
646 let mut name = arg_name.name.to_string();
647 let is_formatting_arg = #[allow(non_exhaustive_omitted_patterns)] match used_as {
Width | Precision => true,
_ => false,
}matches!(used_as, Width | Precision);
648 if is_formatting_arg {
649 name.push('$')
650 };
651 let span_to_replace = if let Ok(positional_arg_content) = sess
652 .downcast_ref::<rustc_session::Session>()
653 .expect("expected a `Session`")
654 .source_map()
655 .span_to_snippet(positional_arg_to_replace)
656 && positional_arg_content.starts_with(':')
657 {
658 positional_arg_to_replace.shrink_to_lo()
659 } else {
660 positional_arg_to_replace
661 };
662 (Some(span_to_replace), name)
663 } else {
664 (None, String::new())
665 };
666
667 diagnostics::NamedArgumentUsedPositionally {
668 named_arg_sp: arg_name.span,
669 position_label_sp: position_sp_for_msg,
670 suggestion,
671 name,
672 named_arg_name: arg_name.name.to_string(),
673 }
674 .into_diag(dcx, level)
675 })),
676 });
677 }
678 }
679
680 ExpandResult::Ready(Ok(FormatArgs {
681 span: fmt_span,
682 template,
683 arguments: args,
684 uncooked_fmt_str,
685 is_source_literal,
686 }))
687}
688
689fn invalid_placeholder_type_error(
690 ecx: &ExtCtxt<'_>,
691 ty: &str,
692 ty_span: Option<Range<usize>>,
693 fmt_span: Span,
694) {
695 let sp = ty_span.map(|sp| fmt_span.from_inner(InnerSpan::new(sp.start, sp.end)));
696 let suggs = if let Some(sp) = sp {
697 [
698 ("", "Display"),
699 ("?", "Debug"),
700 ("e", "LowerExp"),
701 ("E", "UpperExp"),
702 ("o", "Octal"),
703 ("p", "Pointer"),
704 ("b", "Binary"),
705 ("x", "LowerHex"),
706 ("X", "UpperHex"),
707 ]
708 .into_iter()
709 .map(|(fmt, trait_name)| diagnostics::FormatUnknownTraitSugg { span: sp, fmt, trait_name })
710 .collect()
711 } else {
712 ::alloc::vec::Vec::new()vec![]
713 };
714 ecx.dcx().emit_err(diagnostics::FormatUnknownTrait { span: sp.unwrap_or(fmt_span), ty, suggs });
715}
716
717fn report_missing_placeholders(
718 ecx: &ExtCtxt<'_>,
719 unused: Vec<(Span, bool)>,
720 used: &[bool],
721 args: &FormatArguments,
722 pieces: &[parse::Piece<'_>],
723 invalid_refs: &[(usize, Option<Span>, PositionUsedAs, FormatArgPositionKind)],
724 detect_foreign_fmt: bool,
725 str_style: Option<usize>,
726 fmt_str: &str,
727 uncooked_fmt_str: &str,
728 fmt_span: Span,
729) {
730 let mut diag = if let &[(span, named)] = &unused[..] {
731 ecx.dcx().create_err(diagnostics::FormatUnusedArg { span, named })
732 } else {
733 let unused_labels = unused
734 .iter()
735 .map(|&(span, named)| diagnostics::FormatUnusedArg { span, named })
736 .collect();
737 let unused_spans = unused.iter().map(|&(span, _)| span).collect();
738 ecx.dcx().create_err(diagnostics::FormatUnusedArgs {
739 fmt: fmt_span,
740 unused: unused_spans,
741 unused_labels,
742 })
743 };
744
745 let placeholders = pieces
746 .iter()
747 .filter_map(|piece| {
748 if let parse::Piece::NextArgument(argument) = piece
749 && let ArgumentNamed(binding) = argument.position
750 {
751 let span = fmt_span.from_inner(InnerSpan::new(
752 argument.position_span.start,
753 argument.position_span.end,
754 ));
755 Some((span, binding))
756 } else {
757 None
758 }
759 })
760 .collect::<Vec<_>>();
761
762 if !placeholders.is_empty() {
763 if let Some(new_diag) = report_redundant_format_arguments(ecx, args, used, placeholders) {
764 diag.cancel();
765 new_diag.emit();
766 return;
767 }
768 }
769
770 let mut found_foreign = false;
772
773 if detect_foreign_fmt {
775 use super::format_foreign as foreign;
776
777 let mut explained = FxHashSet::default();
780
781 macro_rules! check_foreign {
782 ($kind:ident) => {{
783 let mut show_doc_note = false;
784
785 let mut suggestions = vec![];
786 let padding = str_style.map(|i| i + 2).unwrap_or(1);
788 for sub in foreign::$kind::iter_subs(fmt_str, padding) {
789 let (trn, success) = match sub.translate() {
790 Ok(trn) => (trn, true),
791 Err(Some(msg)) => (msg, false),
792
793 _ => continue,
795 };
796
797 let pos = sub.position();
798 if !explained.insert(sub.to_string()) {
799 continue;
800 }
801
802 if !found_foreign {
803 found_foreign = true;
804 show_doc_note = true;
805 }
806
807 let sp = fmt_span.from_inner(pos);
808
809 if success {
810 suggestions.push((sp, trn));
811 } else {
812 diag.span_note(
813 sp,
814 format!("format specifiers use curly braces, and {}", trn),
815 );
816 }
817 }
818
819 if show_doc_note {
820 diag.note(concat!(
821 stringify!($kind),
822 " formatting is not supported; see the documentation for `std::fmt`",
823 ));
824 }
825 if suggestions.len() > 0 {
826 diag.multipart_suggestion(
827 "format specifiers use curly braces",
828 suggestions,
829 Applicability::MachineApplicable,
830 );
831 }
832 }};
833 }
834
835 {
let mut show_doc_note = false;
let mut suggestions = ::alloc::vec::Vec::new();
let padding = str_style.map(|i| i + 2).unwrap_or(1);
for sub in foreign::printf::iter_subs(fmt_str, padding) {
let (trn, success) =
match sub.translate() {
Ok(trn) => (trn, true),
Err(Some(msg)) => (msg, false),
_ => continue,
};
let pos = sub.position();
if !explained.insert(sub.to_string()) { continue; }
if !found_foreign { found_foreign = true; show_doc_note = true; }
let sp = fmt_span.from_inner(pos);
if success {
suggestions.push((sp, trn));
} else {
diag.span_note(sp,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("format specifiers use curly braces, and {0}",
trn))
}));
}
}
if show_doc_note {
diag.note("printf formatting is not supported; see the documentation for `std::fmt`");
}
if suggestions.len() > 0 {
diag.multipart_suggestion("format specifiers use curly braces",
suggestions, Applicability::MachineApplicable);
}
};check_foreign!(printf);
836 if !found_foreign {
837 {
let mut show_doc_note = false;
let mut suggestions = ::alloc::vec::Vec::new();
let padding = str_style.map(|i| i + 2).unwrap_or(1);
for sub in foreign::shell::iter_subs(fmt_str, padding) {
let (trn, success) =
match sub.translate() {
Ok(trn) => (trn, true),
Err(Some(msg)) => (msg, false),
_ => continue,
};
let pos = sub.position();
if !explained.insert(sub.to_string()) { continue; }
if !found_foreign { found_foreign = true; show_doc_note = true; }
let sp = fmt_span.from_inner(pos);
if success {
suggestions.push((sp, trn));
} else {
diag.span_note(sp,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("format specifiers use curly braces, and {0}",
trn))
}));
}
}
if show_doc_note {
diag.note("shell formatting is not supported; see the documentation for `std::fmt`");
}
if suggestions.len() > 0 {
diag.multipart_suggestion("format specifiers use curly braces",
suggestions, Applicability::MachineApplicable);
}
};check_foreign!(shell);
838 }
839 }
840 if !found_foreign && unused.len() == 1 {
841 diag.span_label(fmt_span, "formatting specifier missing");
842 }
843
844 if !found_foreign && invalid_refs.is_empty() {
845 let show_example = !used.contains(&true);
847
848 if !show_example {
849 if unused.len() > 1 {
850 diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider adding {0} format specifiers",
unused.len()))
})format!("consider adding {} format specifiers", unused.len()));
851 }
852 } else {
853 let msg = if unused.len() == 1 {
854 "a format specifier".to_string()
855 } else {
856 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} format specifiers",
unused.len()))
})format!("{} format specifiers", unused.len())
857 };
858
859 let sugg = match str_style {
860 None => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\"{0}{1}\"", uncooked_fmt_str,
"{}".repeat(unused.len())))
})format!("\"{}{}\"", uncooked_fmt_str, "{}".repeat(unused.len())),
861 Some(n_hashes) => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("r{0}\"{2}{1}\"{0}",
"#".repeat(n_hashes), "{}".repeat(unused.len()),
uncooked_fmt_str))
})format!(
862 "r{hashes}\"{uncooked_fmt_str}{fmt_specifiers}\"{hashes}",
863 hashes = "#".repeat(n_hashes),
864 fmt_specifiers = "{}".repeat(unused.len())
865 ),
866 };
867 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("format specifiers use curly braces, consider adding {0}",
msg))
})format!("format specifiers use curly braces, consider adding {msg}");
868
869 diag.span_suggestion_verbose(fmt_span, msg, sugg, Applicability::MaybeIncorrect);
870 }
871 }
872
873 diag.emit();
874}
875
876fn report_redundant_format_arguments<'a>(
879 ecx: &ExtCtxt<'a>,
880 args: &FormatArguments,
881 used: &[bool],
882 placeholders: Vec<(Span, &str)>,
883) -> Option<Diag<'a>> {
884 let mut fmt_arg_indices = ::alloc::vec::Vec::new()vec![];
885 let mut args_spans = ::alloc::vec::Vec::new()vec![];
886 let mut fmt_spans = ::alloc::vec::Vec::new()vec![];
887
888 for (i, unnamed_arg) in args.unnamed_args().iter().enumerate().rev() {
889 let Some(ty) = unnamed_arg.expr.to_ty() else { continue };
890 let Some(argument_binding) = ty.kind.is_simple_path() else { continue };
891 let argument_binding = argument_binding.as_str();
892
893 if used[i] {
894 continue;
895 }
896
897 let matching_placeholders = placeholders
898 .iter()
899 .filter(|(_, inline_binding)| argument_binding == *inline_binding)
900 .map(|(span, _)| span)
901 .collect::<Vec<_>>();
902
903 if !matching_placeholders.is_empty() {
904 fmt_arg_indices.push(i);
905 args_spans.push(unnamed_arg.expr.span);
906 for span in &matching_placeholders {
907 if fmt_spans.contains(*span) {
908 continue;
909 }
910 fmt_spans.push(**span);
911 }
912 }
913 }
914
915 if !args_spans.is_empty() {
916 let multispan = MultiSpan::from(fmt_spans);
917 let mut suggestion_spans = ::alloc::vec::Vec::new()vec![];
918
919 for (arg_span, fmt_arg_idx) in args_spans.iter().zip(fmt_arg_indices.iter()) {
920 let span = if fmt_arg_idx + 1 == args.explicit_args().len() {
921 *arg_span
922 } else {
923 arg_span.until(args.explicit_args()[*fmt_arg_idx + 1].expr.span)
924 };
925
926 suggestion_spans.push(span);
927 }
928
929 let sugg = if args.named_args().len() == 0 {
930 Some(diagnostics::FormatRedundantArgsSugg { spans: suggestion_spans })
931 } else {
932 None
933 };
934
935 return Some(ecx.dcx().create_err(diagnostics::FormatRedundantArgs {
936 n: args_spans.len(),
937 span: MultiSpan::from(args_spans),
938 note: multispan,
939 sugg,
940 }));
941 }
942
943 None
944}
945
946fn report_invalid_references(
951 ecx: &ExtCtxt<'_>,
952 invalid_refs: &[(usize, Option<Span>, PositionUsedAs, FormatArgPositionKind)],
953 template: &[FormatArgsPiece],
954 fmt_span: Span,
955 args: &FormatArguments,
956 parser: parse::Parser<'_>,
957) {
958 let num_args_desc = match args.explicit_args().len() {
959 0 => "no arguments were given".to_string(),
960 1 => "there is 1 argument".to_string(),
961 n => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("there are {0} arguments", n))
})format!("there are {n} arguments"),
962 };
963
964 let mut e;
965
966 if template.iter().all(|piece| match piece {
967 FormatArgsPiece::Placeholder(FormatPlaceholder {
968 argument: FormatArgPosition { kind: FormatArgPositionKind::Number, .. },
969 ..
970 }) => false,
971 FormatArgsPiece::Placeholder(FormatPlaceholder {
972 format_options:
973 FormatOptions {
974 precision:
975 Some(FormatCount::Argument(FormatArgPosition {
976 kind: FormatArgPositionKind::Number,
977 ..
978 })),
979 ..
980 }
981 | FormatOptions {
982 width:
983 Some(FormatCount::Argument(FormatArgPosition {
984 kind: FormatArgPositionKind::Number,
985 ..
986 })),
987 ..
988 },
989 ..
990 }) => false,
991 _ => true,
992 }) {
993 let mut spans = Vec::new();
996 let mut num_placeholders = 0;
997 let mut has_white_space_only_missing_arg = false;
998 for piece in template {
999 let mut placeholder = None;
1000 if let FormatArgsPiece::Placeholder(FormatPlaceholder {
1002 format_options:
1003 FormatOptions {
1004 precision:
1005 Some(FormatCount::Argument(FormatArgPosition {
1006 span,
1007 kind: FormatArgPositionKind::Implicit,
1008 ..
1009 })),
1010 ..
1011 },
1012 ..
1013 }) = piece
1014 {
1015 placeholder = *span;
1016 num_placeholders += 1;
1017 }
1018 if let FormatArgsPiece::Placeholder(FormatPlaceholder {
1020 argument: FormatArgPosition { kind: FormatArgPositionKind::Implicit, index, .. },
1021 span,
1022 ..
1023 }) = piece
1024 {
1025 placeholder = *span;
1026 num_placeholders += 1;
1027 if index.is_err()
1029 && let Some(span) = span
1030 && let Ok(snippet) = ecx.source_map().span_to_snippet(*span)
1031 && snippet.chars().any(|c| c.is_whitespace() && c != ' ')
1032 {
1033 has_white_space_only_missing_arg = true;
1034 }
1035 }
1036 spans.extend(placeholder);
1038 }
1039 let span = if spans.is_empty() {
1040 MultiSpan::from_span(fmt_span)
1041 } else {
1042 MultiSpan::from_spans(spans)
1043 };
1044 e = ecx.dcx().create_err(diagnostics::FormatPositionalMismatch {
1045 span,
1046 n: num_placeholders,
1047 desc: num_args_desc,
1048 highlight: SingleLabelManySpans {
1049 spans: args.explicit_args().iter().map(|arg| arg.expr.span).collect(),
1050 label: "",
1051 },
1052 });
1053 let mut has_precision_star = false;
1055 for piece in template {
1056 if let FormatArgsPiece::Placeholder(FormatPlaceholder {
1057 format_options:
1058 FormatOptions {
1059 precision:
1060 Some(FormatCount::Argument(FormatArgPosition {
1061 index,
1062 span: Some(span),
1063 kind: FormatArgPositionKind::Implicit,
1064 ..
1065 })),
1066 ..
1067 },
1068 ..
1069 }) = piece
1070 {
1071 let (Ok(index) | Err(index)) = index;
1072 has_precision_star = true;
1073 e.span_label(
1074 *span,
1075 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this precision flag adds an extra required argument at position {0}, which is why there {1} expected",
index,
if num_placeholders == 1 {
"is 1 argument".to_string()
} else {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("are {0} arguments",
num_placeholders))
})
}))
})format!(
1076 "this precision flag adds an extra required argument at position {}, which is why there {} expected",
1077 index,
1078 if num_placeholders == 1 {
1079 "is 1 argument".to_string()
1080 } else {
1081 format!("are {num_placeholders} arguments")
1082 },
1083 ),
1084 );
1085 }
1086 }
1087 if has_precision_star {
1088 e.note("positional arguments are zero-based");
1089 }
1090 if has_white_space_only_missing_arg {
1091 e.note("if you intended to print `{`, you can escape it with `{{`");
1092 }
1093 } else {
1094 let mut indexes: Vec<_> = invalid_refs.iter().map(|&(index, _, _, _)| index).collect();
1095 indexes.sort();
1098 indexes.dedup();
1099 let span: MultiSpan = if !parser.is_source_literal || parser.arg_places.is_empty() {
1100 MultiSpan::from_span(fmt_span)
1101 } else {
1102 MultiSpan::from_spans(invalid_refs.iter().filter_map(|&(_, span, _, _)| span).collect())
1103 };
1104 let arg_list = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("argument{0} {1}",
if indexes.len() == 1 { "" } else { "s" },
listify(&indexes,
|i: &usize| i.to_string()).unwrap_or_default()))
})format!(
1105 "argument{} {}",
1106 pluralize!(indexes.len()),
1107 listify(&indexes, |i: &usize| i.to_string()).unwrap_or_default()
1108 );
1109 e = ecx.dcx().struct_span_err(
1110 span,
1111 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid reference to positional {0} ({1})",
arg_list, num_args_desc))
})format!("invalid reference to positional {arg_list} ({num_args_desc})"),
1112 );
1113 e.note("positional arguments are zero-based");
1114 }
1115
1116 if template.iter().any(|piece| match piece {
1117 FormatArgsPiece::Placeholder(FormatPlaceholder { format_options: f, .. }) => {
1118 *f != FormatOptions::default()
1119 }
1120 _ => false,
1121 }) {
1122 e.note("for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html");
1123 }
1124
1125 e.emit();
1126}
1127
1128fn expand_format_args_impl<'cx>(
1129 ecx: &'cx mut ExtCtxt<'_>,
1130 mut sp: Span,
1131 tts: TokenStream,
1132 nl: bool,
1133) -> MacroExpanderResult<'cx> {
1134 sp = ecx.with_def_site_ctxt(sp);
1135 ExpandResult::Ready(match parse_args(ecx, sp, tts) {
1136 Ok(input) => {
1137 let ExpandResult::Ready(mac) = make_format_args(ecx, input, nl, sp) else {
1138 return ExpandResult::Retry(());
1139 };
1140 match mac {
1141 Ok(format_args) => {
1142 MacEager::expr(ecx.expr(sp, ExprKind::FormatArgs(Box::new(format_args))))
1143 }
1144 Err(guar) => MacEager::expr(DummyResult::raw_expr(sp, Some(guar))),
1145 }
1146 }
1147 Err(err) => {
1148 let guar = err.emit();
1149 DummyResult::any(sp, guar)
1150 }
1151 })
1152}
1153
1154pub(crate) fn expand_format_args<'cx>(
1155 ecx: &'cx mut ExtCtxt<'_>,
1156 sp: Span,
1157 tts: TokenStream,
1158) -> MacroExpanderResult<'cx> {
1159 expand_format_args_impl(ecx, sp, tts, false)
1160}
1161
1162pub(crate) fn expand_format_args_nl<'cx>(
1163 ecx: &'cx mut ExtCtxt<'_>,
1164 sp: Span,
1165 tts: TokenStream,
1166) -> MacroExpanderResult<'cx> {
1167 expand_format_args_impl(ecx, sp, tts, true)
1168}