Repository: rust-lang/rust
File: compiler/rustc_builtin_macros/src/format.rs (Rust)

Code before:
                        span: span.shrink_to_hi(),
                        arg,
                    });
                }
            }
            parse::Suggestion::RemoveRawIdent(span) => {
                if is_source_literal {
                    let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
                    e.sugg_ = Some(errors::InvalidFormatStringSuggestion::RemoveRawIdent { span })
                }
            }
            parse::Suggestion::ReorderFormatParameter(span, replacement) => {
                let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
                e.sugg_ = Some(errors::InvalidFormatStringSuggestion::ReorderFormatParameter {
                    span,
                    replacement,
                });
            }
            parse::Suggestion::AddMissingColon(span) => {
                let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
                e.sugg_ = Some(errors::InvalidFormatStringSuggestion::AddMissingColon { span });
            }
            parse::Suggestion::UseRustDebugPrintingMacro() => {
                // there should only be suggestion when we have only one argument
                if args.all_args().len() == 1 {
                    let arg = &args.all_args()[0];
                    match &arg.kind {
                        // println!("{x=}")
                        FormatArgumentKind::Captured(ident) => {
                            let ident_name = ident.name.to_ident_string();

                            let replacement = format!("!({}{})", "dbg", ident_name);

                            e.sugg_ = Some(
                                errors::InvalidFormatStringSuggestion::UseRustDebugPrintingMacro {
                                    macro_span,
                                    replacement,
                                },
                            );
                        }

                        // println!("{=}", x) or println!("{0=}", x)
                        FormatArgumentKind::Normal => {
                            let expr_span = arg.expr.span;

                            if let Ok(expr_snippet) = ecx.source_map().span_to_snippet(expr_span) {
                                let replacement = format!("{}!({})", "dbg", expr_snippet);

                                e.sugg_ = Some(
                                    errors::InvalidFormatStringSuggestion::UseRustDebugPrintingMacro {
                                        macro_span,

Review comment (suggestion):
This can be
```suggestion
                if [arg] = args.all_args() {
```

Code after:
                        span: span.shrink_to_hi(),
                        arg,
                    });
                }
            }
            parse::Suggestion::RemoveRawIdent(span) => {
                if is_source_literal {
                    let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
                    e.sugg_ = Some(errors::InvalidFormatStringSuggestion::RemoveRawIdent { span })
                }
            }
            parse::Suggestion::ReorderFormatParameter(span, replacement) => {
                let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
                e.sugg_ = Some(errors::InvalidFormatStringSuggestion::ReorderFormatParameter {
                    span,
                    replacement,
                });
            }
            parse::Suggestion::AddMissingColon(span) => {
                let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
                e.sugg_ = Some(errors::InvalidFormatStringSuggestion::AddMissingColon { span });
            }
            parse::Suggestion::UseRustDebugPrintingMacro => {
                // This targets `println!("{=}", x);` and `println!("{0=}", x);`
                if let [arg] = args.all_args() {
                    let expr_span = arg.expr.span;
                    if let Ok(expr_snippet) = ecx.source_map().span_to_snippet(expr_span) {
                        let replacement = format!("{}!({})", "dbg", expr_snippet);

                        let call_span = macro_span.source_callsite();
                        e.sugg_ = Some(
                            errors::InvalidFormatStringSuggestion::UseRustDebugPrintingMacro {
                                macro_span: call_span,
                                replacement,
                            },
                        );
                    }
                }
            }
        }
        let guar = ecx.dcx().emit_err(e);
        return ExpandResult::Ready(Err(guar));
    }

    let to_span = |inner_span: Range<usize>| {
        is_source_literal.then(|| {
            fmt_span.from_inner(InnerSpan { start: inner_span.start, end: inner_span.end })
        })
    };

    let mut used = vec![false; args.explicit_args().len()];