Skip to content

Commit 3259b48

Browse files
committedMar 28, 2023
Migrate format_args.rs to rustc_ast::FormatArgs
No longer lints empty precisions `{:.}` as the spans aren't available
1 parent 70db226 commit 3259b48

11 files changed

+264
-239
lines changed
 

‎clippy_lints/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ keywords = ["clippy", "lint", "plugin"]
99
edition = "2021"
1010

1111
[dependencies]
12+
arrayvec = { version = "0.7", default-features = false }
1213
cargo_metadata = "0.15.3"
1314
clippy_utils = { path = "../clippy_utils" }
1415
declare_clippy_lint = { path = "../declare_clippy_lint" }

‎clippy_lints/src/format_args.rs

Lines changed: 124 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1+
use arrayvec::ArrayVec;
12
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
23
use clippy_utils::is_diag_trait_item;
3-
use clippy_utils::macros::FormatParamKind::{Implicit, Named, NamedInline, Numbered, Starred};
44
use clippy_utils::macros::{
5-
is_assert_macro, is_format_macro, is_panic, root_macro_call, Count, FormatArg, FormatArgsExpn, FormatParam,
6-
FormatParamUsage,
5+
find_format_arg_expr, find_format_args, format_arg_removal_span, format_placeholder_format_span, is_assert_macro,
6+
is_format_macro, is_panic, root_macro_call, root_macro_call_first_node, FormatParamUsage,
77
};
88
use clippy_utils::msrvs::{self, Msrv};
99
use clippy_utils::source::snippet_opt;
1010
use clippy_utils::ty::{implements_trait, is_type_lang_item};
1111
use if_chain::if_chain;
1212
use itertools::Itertools;
13+
use rustc_ast::{
14+
FormatArgPosition, FormatArgPositionKind, FormatArgsPiece, FormatArgumentKind, FormatCount, FormatOptions,
15+
FormatPlaceholder, FormatTrait,
16+
};
1317
use rustc_errors::{
1418
Applicability,
1519
SuggestionStyle::{CompletelyHidden, ShowCode},
1620
};
17-
use rustc_hir::{Expr, ExprKind, HirId, LangItem, QPath};
21+
use rustc_hir::{Expr, ExprKind, LangItem};
1822
use rustc_lint::{LateContext, LateLintPass, LintContext};
1923
use rustc_middle::ty::adjustment::{Adjust, Adjustment};
2024
use rustc_middle::ty::Ty;
2125
use rustc_session::{declare_tool_lint, impl_lint_pass};
2226
use rustc_span::def_id::DefId;
2327
use rustc_span::edition::Edition::Edition2021;
24-
use rustc_span::{sym, ExpnData, ExpnKind, Span, Symbol};
28+
use rustc_span::{sym, Span, Symbol};
2529

2630
declare_clippy_lint! {
2731
/// ### What it does
@@ -184,111 +188,120 @@ impl FormatArgs {
184188

185189
impl<'tcx> LateLintPass<'tcx> for FormatArgs {
186190
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
187-
if let Some(format_args) = FormatArgsExpn::parse(cx, expr)
188-
&& let expr_expn_data = expr.span.ctxt().outer_expn_data()
189-
&& let outermost_expn_data = outermost_expn_data(expr_expn_data)
190-
&& let Some(macro_def_id) = outermost_expn_data.macro_def_id
191-
&& is_format_macro(cx, macro_def_id)
192-
&& let ExpnKind::Macro(_, name) = outermost_expn_data.kind
193-
{
194-
for arg in &format_args.args {
195-
check_unused_format_specifier(cx, arg);
196-
if !arg.format.is_default() {
197-
continue;
198-
}
199-
if is_aliased(&format_args, arg.param.value.hir_id) {
200-
continue;
191+
let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return };
192+
if !is_format_macro(cx, macro_call.def_id) {
193+
return;
194+
}
195+
let name = cx.tcx.item_name(macro_call.def_id);
196+
197+
find_format_args(cx, expr, macro_call.expn, |format_args| {
198+
for piece in &format_args.template {
199+
if let FormatArgsPiece::Placeholder(placeholder) = piece
200+
&& let Ok(index) = placeholder.argument.index
201+
&& let Some(arg) = format_args.arguments.all_args().get(index)
202+
{
203+
let arg_expr = find_format_arg_expr(expr, arg);
204+
205+
check_unused_format_specifier(cx, placeholder, arg_expr);
206+
207+
if placeholder.format_trait != FormatTrait::Display
208+
|| placeholder.format_options != FormatOptions::default()
209+
|| is_aliased(format_args, index)
210+
{
211+
continue;
212+
}
213+
214+
if let Ok(arg_hir_expr) = arg_expr {
215+
check_format_in_format_args(cx, macro_call.span, name, arg_hir_expr);
216+
check_to_string_in_format_args(cx, name, arg_hir_expr);
217+
}
201218
}
202-
check_format_in_format_args(cx, outermost_expn_data.call_site, name, arg.param.value);
203-
check_to_string_in_format_args(cx, name, arg.param.value);
204219
}
220+
205221
if self.msrv.meets(msrvs::FORMAT_ARGS_CAPTURE) {
206-
check_uninlined_args(cx, &format_args, outermost_expn_data.call_site, macro_def_id, self.ignore_mixed);
222+
check_uninlined_args(cx, format_args, macro_call.span, macro_call.def_id, self.ignore_mixed);
207223
}
208-
}
224+
});
209225
}
210226

211227
extract_msrv_attr!(LateContext);
212228
}
213229

214-
fn check_unused_format_specifier(cx: &LateContext<'_>, arg: &FormatArg<'_>) {
215-
let param_ty = cx.typeck_results().expr_ty(arg.param.value).peel_refs();
230+
fn check_unused_format_specifier(
231+
cx: &LateContext<'_>,
232+
placeholder: &FormatPlaceholder,
233+
arg_expr: Result<&Expr<'_>, &rustc_ast::Expr>,
234+
) {
235+
let ty_or_ast_expr = arg_expr.map(|expr| cx.typeck_results().expr_ty(expr).peel_refs());
216236

217-
if let Count::Implied(Some(mut span)) = arg.format.precision
218-
&& !span.is_empty()
219-
{
220-
span_lint_and_then(
221-
cx,
222-
UNUSED_FORMAT_SPECS,
223-
span,
224-
"empty precision specifier has no effect",
225-
|diag| {
226-
if param_ty.is_floating_point() {
227-
diag.note("a precision specifier is not required to format floats");
228-
}
237+
let is_format_args = match ty_or_ast_expr {
238+
Ok(ty) => is_type_lang_item(cx, ty, LangItem::FormatArguments),
239+
Err(expr) => matches!(expr.peel_parens_and_refs().kind, rustc_ast::ExprKind::FormatArgs(_)),
240+
};
229241

230-
if arg.format.is_default() {
231-
// If there's no other specifiers remove the `:` too
232-
span = arg.format_span();
233-
}
242+
let options = &placeholder.format_options;
234243

235-
diag.span_suggestion_verbose(span, "remove the `.`", "", Applicability::MachineApplicable);
236-
},
237-
);
238-
}
244+
let arg_span = match arg_expr {
245+
Ok(expr) => expr.span,
246+
Err(expr) => expr.span,
247+
};
239248

240-
if is_type_lang_item(cx, param_ty, LangItem::FormatArguments) && !arg.format.is_default_for_trait() {
249+
if let Some(placeholder_span) = placeholder.span
250+
&& is_format_args
251+
&& *options != FormatOptions::default()
252+
{
241253
span_lint_and_then(
242254
cx,
243255
UNUSED_FORMAT_SPECS,
244-
arg.span,
256+
placeholder_span,
245257
"format specifiers have no effect on `format_args!()`",
246258
|diag| {
247-
let mut suggest_format = |spec, span| {
259+
let mut suggest_format = |spec| {
248260
let message = format!("for the {spec} to apply consider using `format!()`");
249261

250-
if let Some(mac_call) = root_macro_call(arg.param.value.span)
262+
if let Some(mac_call) = root_macro_call(arg_span)
251263
&& cx.tcx.is_diagnostic_item(sym::format_args_macro, mac_call.def_id)
252-
&& arg.span.eq_ctxt(mac_call.span)
253264
{
254265
diag.span_suggestion(
255266
cx.sess().source_map().span_until_char(mac_call.span, '!'),
256267
message,
257268
"format",
258269
Applicability::MaybeIncorrect,
259270
);
260-
} else if let Some(span) = span {
261-
diag.span_help(span, message);
271+
} else {
272+
diag.help(message);
262273
}
263274
};
264275

265-
if !arg.format.width.is_implied() {
266-
suggest_format("width", arg.format.width.span());
276+
if options.width.is_some() {
277+
suggest_format("width");
267278
}
268279

269-
if !arg.format.precision.is_implied() {
270-
suggest_format("precision", arg.format.precision.span());
280+
if options.precision.is_some() {
281+
suggest_format("precision");
271282
}
272283

273-
diag.span_suggestion_verbose(
274-
arg.format_span(),
275-
"if the current behavior is intentional, remove the format specifiers",
276-
"",
277-
Applicability::MaybeIncorrect,
278-
);
284+
if let Some(format_span) = format_placeholder_format_span(placeholder) {
285+
diag.span_suggestion_verbose(
286+
format_span,
287+
"if the current behavior is intentional, remove the format specifiers",
288+
"",
289+
Applicability::MaybeIncorrect,
290+
);
291+
}
279292
},
280293
);
281294
}
282295
}
283296

284297
fn check_uninlined_args(
285298
cx: &LateContext<'_>,
286-
args: &FormatArgsExpn<'_>,
299+
args: &rustc_ast::FormatArgs,
287300
call_site: Span,
288301
def_id: DefId,
289302
ignore_mixed: bool,
290303
) {
291-
if args.format_string.span.from_expansion() {
304+
if args.span.from_expansion() {
292305
return;
293306
}
294307
if call_site.edition() < Edition2021 && (is_panic(cx, def_id) || is_assert_macro(cx, def_id)) {
@@ -303,7 +316,13 @@ fn check_uninlined_args(
303316
// we cannot remove any other arguments in the format string,
304317
// because the index numbers might be wrong after inlining.
305318
// Example of an un-inlinable format: print!("{}{1}", foo, 2)
306-
if !args.params().all(|p| check_one_arg(args, &p, &mut fixes, ignore_mixed)) || fixes.is_empty() {
319+
for (pos, usage) in format_arg_positions(args) {
320+
if !check_one_arg(args, pos, usage, &mut fixes, ignore_mixed) {
321+
return;
322+
}
323+
}
324+
325+
if fixes.is_empty() {
307326
return;
308327
}
309328

@@ -332,38 +351,36 @@ fn check_uninlined_args(
332351
}
333352

334353
fn check_one_arg(
335-
args: &FormatArgsExpn<'_>,
336-
param: &FormatParam<'_>,
354+
args: &rustc_ast::FormatArgs,
355+
pos: &FormatArgPosition,
356+
usage: FormatParamUsage,
337357
fixes: &mut Vec<(Span, String)>,
338358
ignore_mixed: bool,
339359
) -> bool {
340-
if matches!(param.kind, Implicit | Starred | Named(_) | Numbered)
341-
&& let ExprKind::Path(QPath::Resolved(None, path)) = param.value.kind
342-
&& let [segment] = path.segments
360+
let index = pos.index.unwrap();
361+
let arg = &args.arguments.all_args()[index];
362+
363+
if !matches!(arg.kind, FormatArgumentKind::Captured(_))
364+
&& let rustc_ast::ExprKind::Path(None, path) = &arg.expr.kind
365+
&& let [segment] = path.segments.as_slice()
343366
&& segment.args.is_none()
344-
&& let Some(arg_span) = args.value_with_prev_comma_span(param.value.hir_id)
367+
&& let Some(arg_span) = format_arg_removal_span(args, index)
368+
&& let Some(pos_span) = pos.span
345369
{
346-
let replacement = match param.usage {
370+
let replacement = match usage {
347371
FormatParamUsage::Argument => segment.ident.name.to_string(),
348372
FormatParamUsage::Width => format!("{}$", segment.ident.name),
349373
FormatParamUsage::Precision => format!(".{}$", segment.ident.name),
350374
};
351-
fixes.push((param.span, replacement));
375+
fixes.push((pos_span, replacement));
352376
fixes.push((arg_span, String::new()));
353377
true // successful inlining, continue checking
354378
} else {
355379
// Do not continue inlining (return false) in case
356380
// * if we can't inline a numbered argument, e.g. `print!("{0} ...", foo.bar, ...)`
357381
// * if allow_mixed_uninlined_format_args is false and this arg hasn't been inlined already
358-
param.kind != Numbered && (!ignore_mixed || matches!(param.kind, NamedInline(_)))
359-
}
360-
}
361-
362-
fn outermost_expn_data(expn_data: ExpnData) -> ExpnData {
363-
if expn_data.call_site.from_expansion() {
364-
outermost_expn_data(expn_data.call_site.ctxt().outer_expn_data())
365-
} else {
366-
expn_data
382+
pos.kind != FormatArgPositionKind::Number
383+
&& (!ignore_mixed || matches!(arg.kind, FormatArgumentKind::Captured(_)))
367384
}
368385
}
369386

@@ -438,10 +455,31 @@ fn check_to_string_in_format_args(cx: &LateContext<'_>, name: Symbol, value: &Ex
438455
}
439456
}
440457

441-
/// Returns true if `hir_id` is referred to by multiple format params
442-
fn is_aliased(args: &FormatArgsExpn<'_>, hir_id: HirId) -> bool {
443-
args.params()
444-
.filter(|param| param.value.hir_id == hir_id)
458+
fn format_arg_positions(
459+
format_args: &rustc_ast::FormatArgs,
460+
) -> impl Iterator<Item = (&FormatArgPosition, FormatParamUsage)> {
461+
format_args.template.iter().flat_map(|piece| match piece {
462+
FormatArgsPiece::Placeholder(placeholder) => {
463+
let mut positions = ArrayVec::<_, 3>::new();
464+
465+
positions.push((&placeholder.argument, FormatParamUsage::Argument));
466+
if let Some(FormatCount::Argument(position)) = &placeholder.format_options.width {
467+
positions.push((position, FormatParamUsage::Width));
468+
}
469+
if let Some(FormatCount::Argument(position)) = &placeholder.format_options.precision {
470+
positions.push((position, FormatParamUsage::Precision));
471+
}
472+
473+
positions
474+
},
475+
FormatArgsPiece::Literal(_) => ArrayVec::new(),
476+
})
477+
}
478+
479+
/// Returns true if the format argument at `index` is referred to by multiple format params
480+
fn is_aliased(format_args: &rustc_ast::FormatArgs, index: usize) -> bool {
481+
format_arg_positions(format_args)
482+
.filter(|(position, _)| position.index == Ok(index))
445483
.at_most_one()
446484
.is_err()
447485
}

‎clippy_lints/src/utils/format_args_collector.rs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
use clippy_utils::macros::collect_ast_format_args;
2-
use rustc_ast::{Expr, ExprKind};
2+
use clippy_utils::source::snippet_opt;
3+
use itertools::Itertools;
4+
use rustc_ast::{Expr, ExprKind, FormatArgs};
5+
use rustc_lexer::{tokenize, TokenKind};
36
use rustc_lint::{EarlyContext, EarlyLintPass};
47
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
use rustc_span::hygiene;
9+
use std::iter::once;
510

611
declare_clippy_lint! {
712
/// ### What it does
@@ -15,9 +20,79 @@ declare_clippy_lint! {
1520
declare_lint_pass!(FormatArgsCollector => [FORMAT_ARGS_COLLECTOR]);
1621

1722
impl EarlyLintPass for FormatArgsCollector {
18-
fn check_expr(&mut self, _: &EarlyContext<'_>, expr: &Expr) {
23+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
1924
if let ExprKind::FormatArgs(args) = &expr.kind {
25+
if has_span_from_proc_macro(cx, args) {
26+
return;
27+
}
28+
2029
collect_ast_format_args(expr.span, args);
2130
}
2231
}
2332
}
33+
34+
/// Detects if the format string or an argument has its span set by a proc macro to something inside
35+
/// a macro callsite, e.g.
36+
///
37+
/// ```ignore
38+
/// println!(some_proc_macro!("input {}"), a);
39+
/// ```
40+
///
41+
/// Where `some_proc_macro` expands to
42+
///
43+
/// ```ignore
44+
/// println!("output {}", a);
45+
/// ```
46+
///
47+
/// But with the span of `"output {}"` set to the macro input
48+
///
49+
/// ```ignore
50+
/// println!(some_proc_macro!("input {}"), a);
51+
/// // ^^^^^^^^^^
52+
/// ```
53+
fn has_span_from_proc_macro(cx: &EarlyContext<'_>, args: &FormatArgs) -> bool {
54+
let ctxt = args.span.ctxt();
55+
56+
// `format!("{} {} {c}", "one", "two", c = "three")`
57+
// ^^^^^ ^^^^^ ^^^^^^^
58+
let argument_span = args
59+
.arguments
60+
.explicit_args()
61+
.iter()
62+
.map(|argument| hygiene::walk_chain(argument.expr.span, ctxt));
63+
64+
// `format!("{} {} {c}", "one", "two", c = "three")`
65+
// ^^ ^^ ^^^^^^
66+
let between_spans = once(args.span)
67+
.chain(argument_span)
68+
.tuple_windows()
69+
.map(|(start, end)| start.between(end));
70+
71+
for between_span in between_spans {
72+
let mut seen_comma = false;
73+
74+
let Some(snippet) = snippet_opt(cx, between_span) else { return true };
75+
for token in tokenize(&snippet) {
76+
match token.kind {
77+
TokenKind::LineComment { .. } | TokenKind::BlockComment { .. } | TokenKind::Whitespace => {},
78+
TokenKind::Comma if !seen_comma => seen_comma = true,
79+
// named arguments, `start_val, name = end_val`
80+
// ^^^^^^^^^ between_span
81+
TokenKind::Ident | TokenKind::Eq if seen_comma => {},
82+
// An unexpected token usually indicates that we crossed a macro boundary
83+
//
84+
// `println!(some_proc_macro!("input {}"), a)`
85+
// ^^^ between_span
86+
// `println!("{}", val!(x))`
87+
// ^^^^^^^ between_span
88+
_ => return true,
89+
}
90+
}
91+
92+
if !seen_comma {
93+
return true;
94+
}
95+
}
96+
97+
false
98+
}

‎clippy_utils/src/macros.rs

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::visitors::{for_each_expr, Descend};
66
use arrayvec::ArrayVec;
77
use itertools::{izip, Either, Itertools};
88
use rustc_ast::ast::LitKind;
9-
use rustc_ast::FormatArgs;
9+
use rustc_ast::{FormatArgs, FormatArgument, FormatPlaceholder};
1010
use rustc_data_structures::fx::FxHashMap;
1111
use rustc_hir::intravisit::{walk_expr, Visitor};
1212
use rustc_hir::{self as hir, Expr, ExprField, ExprKind, HirId, LangItem, Node, QPath, TyKind};
@@ -391,30 +391,67 @@ pub fn collect_ast_format_args(span: Span, format_args: &FormatArgs) {
391391
});
392392
}
393393

394-
/// Calls `callback` with an AST [`FormatArgs`] node if one is found
394+
/// Calls `callback` with an AST [`FormatArgs`] node if a `format_args` expansion is found as a
395+
/// descendant of `expn_id`
395396
pub fn find_format_args(cx: &LateContext<'_>, start: &Expr<'_>, expn_id: ExpnId, callback: impl FnOnce(&FormatArgs)) {
396397
let format_args_expr = for_each_expr(start, |expr| {
397398
let ctxt = expr.span.ctxt();
398-
if ctxt == start.span.ctxt() {
399-
ControlFlow::Continue(Descend::Yes)
400-
} else if ctxt.outer_expn().is_descendant_of(expn_id)
401-
&& macro_backtrace(expr.span)
399+
if ctxt.outer_expn().is_descendant_of(expn_id) {
400+
if macro_backtrace(expr.span)
402401
.map(|macro_call| cx.tcx.item_name(macro_call.def_id))
403402
.any(|name| matches!(name, sym::const_format_args | sym::format_args | sym::format_args_nl))
404-
{
405-
ControlFlow::Break(expr)
403+
{
404+
ControlFlow::Break(expr)
405+
} else {
406+
ControlFlow::Continue(Descend::Yes)
407+
}
406408
} else {
407409
ControlFlow::Continue(Descend::No)
408410
}
409411
});
410412

411-
if let Some(format_args_expr) = format_args_expr {
413+
if let Some(expr) = format_args_expr {
412414
AST_FORMAT_ARGS.with(|ast_format_args| {
413-
ast_format_args.borrow().get(&format_args_expr.span).map(callback);
415+
ast_format_args.borrow().get(&expr.span).map(callback);
414416
});
415417
}
416418
}
417419

420+
/// Attempt to find the [`rustc_hir::Expr`] that corresponds to the [`FormatArgument`]'s value, if
421+
/// it cannot be found it will return the [`rustc_ast::Expr`].
422+
pub fn find_format_arg_expr<'hir, 'ast>(
423+
start: &'hir Expr<'hir>,
424+
target: &'ast FormatArgument,
425+
) -> Result<&'hir rustc_hir::Expr<'hir>, &'ast rustc_ast::Expr> {
426+
for_each_expr(start, |expr| {
427+
if expr.span == target.expr.span {
428+
ControlFlow::Break(expr)
429+
} else {
430+
ControlFlow::Continue(())
431+
}
432+
})
433+
.ok_or(&target.expr)
434+
}
435+
436+
/// Span of the `:` and format specifiers
437+
///
438+
/// ```ignore
439+
/// format!("{:.}"), format!("{foo:.}")
440+
/// ^^ ^^
441+
/// ```
442+
pub fn format_placeholder_format_span(placeholder: &FormatPlaceholder) -> Option<Span> {
443+
let base = placeholder.span?.data();
444+
445+
// `base.hi` is `{...}|`, subtract 1 byte (the length of '}') so that it points before the closing
446+
// brace `{...|}`
447+
Some(Span::new(
448+
placeholder.argument.span?.hi(),
449+
base.hi - BytePos(1),
450+
base.ctxt,
451+
base.parent,
452+
))
453+
}
454+
418455
/// Returns the [`Span`] of the value at `index` extended to the previous comma, e.g. for the value
419456
/// `10`
420457
///
@@ -897,22 +934,6 @@ pub struct FormatArg<'tcx> {
897934
pub span: Span,
898935
}
899936

900-
impl<'tcx> FormatArg<'tcx> {
901-
/// Span of the `:` and format specifiers
902-
///
903-
/// ```ignore
904-
/// format!("{:.}"), format!("{foo:.}")
905-
/// ^^ ^^
906-
/// ```
907-
pub fn format_span(&self) -> Span {
908-
let base = self.span.data();
909-
910-
// `base.hi` is `{...}|`, subtract 1 byte (the length of '}') so that it points before the closing
911-
// brace `{...|}`
912-
Span::new(self.param.span.hi(), base.hi - BytePos(1), base.ctxt, base.parent)
913-
}
914-
}
915-
916937
/// A parsed `format_args!` expansion.
917938
#[derive(Debug)]
918939
pub struct FormatArgsExpn<'tcx> {

‎tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@ LL - println!("val='{}'", local_i32);
1111
LL + println!("val='{local_i32}'");
1212
|
1313

14-
error: literal with an empty format string
15-
--> $DIR/uninlined_format_args.rs:10:35
14+
error: variables can be used directly in the `format!` string
15+
--> $DIR/uninlined_format_args.rs:10:5
1616
|
1717
LL | println!("Hello {} is {:.*}", "x", local_i32, local_f64);
18-
| ^^^
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1919
|
20-
= note: `-D clippy::print-literal` implied by `-D warnings`
21-
help: try this
20+
help: change this to
2221
|
2322
LL - println!("Hello {} is {:.*}", "x", local_i32, local_f64);
24-
LL + println!("Hello x is {:.*}", local_i32, local_f64);
23+
LL + println!("Hello {} is {local_f64:.local_i32$}", "x");
2524
|
2625

27-
error: variables can be used directly in the `format!` string
28-
--> $DIR/uninlined_format_args.rs:10:5
26+
error: literal with an empty format string
27+
--> $DIR/uninlined_format_args.rs:10:35
2928
|
3029
LL | println!("Hello {} is {:.*}", "x", local_i32, local_f64);
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
| ^^^
3231
|
33-
help: change this to
32+
= note: `-D clippy::print-literal` implied by `-D warnings`
33+
help: try this
3434
|
3535
LL - println!("Hello {} is {:.*}", "x", local_i32, local_f64);
36-
LL + println!("Hello {} is {local_f64:.local_i32$}", "x");
36+
LL + println!("Hello x is {:.*}", local_i32, local_f64);
3737
|
3838

3939
error: variables can be used directly in the `format!` string

‎tests/ui/uninlined_format_args.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ fn tester(fn_arg: i32) {
119119
println!("Width = {local_i32}, value with width = {local_f64:local_i32$}");
120120
println!("{local_i32:width$.prec$}");
121121
println!("{width:width$.prec$}");
122-
println!("{}", format!("{local_i32}"));
122+
println!("{}", format!("{}", local_i32));
123123
my_println!("{}", local_i32);
124124
my_println_args!("{}", local_i32);
125125

‎tests/ui/uninlined_format_args.stderr

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -774,18 +774,6 @@ LL - println!("{:w$.p$}", w = width, p = prec);
774774
LL + println!("{width:width$.prec$}");
775775
|
776776

777-
error: variables can be used directly in the `format!` string
778-
--> $DIR/uninlined_format_args.rs:125:20
779-
|
780-
LL | println!("{}", format!("{}", local_i32));
781-
| ^^^^^^^^^^^^^^^^^^^^^^^^
782-
|
783-
help: change this to
784-
|
785-
LL - println!("{}", format!("{}", local_i32));
786-
LL + println!("{}", format!("{local_i32}"));
787-
|
788-
789777
error: variables can be used directly in the `format!` string
790778
--> $DIR/uninlined_format_args.rs:143:5
791779
|
@@ -856,5 +844,5 @@ LL - println!("expand='{}'", local_i32);
856844
LL + println!("expand='{local_i32}'");
857845
|
858846

859-
error: aborting due to 72 previous errors
847+
error: aborting due to 71 previous errors
860848

‎tests/ui/unused_format_specs.fixed

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎tests/ui/unused_format_specs.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎tests/ui/unused_format_specs.stderr

Lines changed: 0 additions & 54 deletions
This file was deleted.

‎tests/ui/unused_format_specs_unfixable.stderr

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ error: format specifiers have no effect on `format_args!()`
3737
LL | println!("{:5}.", format_args_from_macro!());
3838
| ^^^^
3939
|
40-
help: for the width to apply consider using `format!()`
41-
--> $DIR/unused_format_specs_unfixable.rs:16:17
42-
|
43-
LL | println!("{:5}.", format_args_from_macro!());
44-
| ^
40+
= help: for the width to apply consider using `format!()`
4541
help: if the current behavior is intentional, remove the format specifiers
4642
|
4743
LL - println!("{:5}.", format_args_from_macro!());
@@ -54,11 +50,7 @@ error: format specifiers have no effect on `format_args!()`
5450
LL | println!("{args:5}");
5551
| ^^^^^^^^
5652
|
57-
help: for the width to apply consider using `format!()`
58-
--> $DIR/unused_format_specs_unfixable.rs:19:21
59-
|
60-
LL | println!("{args:5}");
61-
| ^
53+
= help: for the width to apply consider using `format!()`
6254
help: if the current behavior is intentional, remove the format specifiers
6355
|
6456
LL - println!("{args:5}");

0 commit comments

Comments
 (0)
Please sign in to comment.