|
1 | 1 | use rustc_ast::ptr::P;
|
2 |
| -use rustc_ast::token; |
3 | 2 | use rustc_ast::tokenstream::TokenStream;
|
| 3 | +use rustc_ast::{token, StmtKind}; |
4 | 4 | use rustc_ast::{
|
5 | 5 | Expr, ExprKind, FormatAlignment, FormatArgPosition, FormatArgPositionKind, FormatArgs,
|
6 | 6 | FormatArgsPiece, FormatArgument, FormatArgumentKind, FormatArguments, FormatCount,
|
@@ -163,25 +163,41 @@ fn make_format_args(
|
163 | 163 |
|
164 | 164 | let MacroInput { fmtstr: efmt, mut args, is_direct_literal } = input;
|
165 | 165 |
|
166 |
| - let (fmt_str, fmt_style, fmt_span) = match expr_to_spanned_string(ecx, efmt, msg) { |
| 166 | + let (fmt_str, fmt_style, fmt_span) = match expr_to_spanned_string(ecx, efmt.clone(), msg) { |
167 | 167 | Ok(mut fmt) if append_newline => {
|
168 | 168 | fmt.0 = Symbol::intern(&format!("{}\n", fmt.0));
|
169 | 169 | fmt
|
170 | 170 | }
|
171 | 171 | Ok(fmt) => fmt,
|
172 | 172 | Err(err) => {
|
173 | 173 | if let Some((mut err, suggested)) = err {
|
174 |
| - let sugg_fmt = match args.explicit_args().len() { |
175 |
| - 0 => "{}".to_string(), |
176 |
| - _ => format!("{}{{}}", "{} ".repeat(args.explicit_args().len())), |
177 |
| - }; |
178 | 174 | if !suggested {
|
179 |
| - err.span_suggestion( |
180 |
| - unexpanded_fmt_span.shrink_to_lo(), |
181 |
| - "you might be missing a string literal to format with", |
182 |
| - format!("\"{sugg_fmt}\", "), |
183 |
| - Applicability::MaybeIncorrect, |
184 |
| - ); |
| 175 | + if let ExprKind::Block(block, None) = &efmt.kind |
| 176 | + && block.stmts.len() == 1 |
| 177 | + && let StmtKind::Expr(expr) = &block.stmts[0].kind |
| 178 | + && let ExprKind::Path(None, path) = &expr.kind |
| 179 | + && path.is_potential_trivial_const_arg() |
| 180 | + { |
| 181 | + err.multipart_suggestion( |
| 182 | + "quote your inlined format argument to use as string literal", |
| 183 | + vec![ |
| 184 | + (unexpanded_fmt_span.shrink_to_hi(), "\"".to_string()), |
| 185 | + (unexpanded_fmt_span.shrink_to_lo(), "\"".to_string()), |
| 186 | + ], |
| 187 | + Applicability::MaybeIncorrect, |
| 188 | + ); |
| 189 | + } else { |
| 190 | + let sugg_fmt = match args.explicit_args().len() { |
| 191 | + 0 => "{}".to_string(), |
| 192 | + _ => format!("{}{{}}", "{} ".repeat(args.explicit_args().len())), |
| 193 | + }; |
| 194 | + err.span_suggestion( |
| 195 | + unexpanded_fmt_span.shrink_to_lo(), |
| 196 | + "you might be missing a string literal to format with", |
| 197 | + format!("\"{sugg_fmt}\", "), |
| 198 | + Applicability::MaybeIncorrect, |
| 199 | + ); |
| 200 | + } |
185 | 201 | }
|
186 | 202 | err.emit();
|
187 | 203 | }
|
|
0 commit comments