Skip to content

Commit 680d97c

Browse files
committed
review feedback
1 parent 9de1f4b commit 680d97c

File tree

6 files changed

+138
-87
lines changed

6 files changed

+138
-87
lines changed

clippy_lints/src/format_args.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ declare_clippy_lint! {
105105
/// Format string uses an indexed argument that cannot be inlined.
106106
/// Supporting this case requires re-indexing of the format string.
107107
///
108-
/// Until implemnted, `print!("{0}={1}", var, 1+2)` should be changed to `print!("{var}={0}", 1+2)` by hand.
109-
///
110-
/// changelog: [`needless-format-args`]: A new lint to inline format arguments, i.e. `print!("{}", var)` into `print!("{var}")`
108+
/// Until implemented, `print!("{0}={1}", var, 1+2)` should be changed to `print!("{var}={0}", 1+2)` by hand.
111109
#[clippy::version = "1.64.0"]
112110
pub NEEDLESS_FORMAT_ARGS,
113111
nursery,
@@ -117,14 +115,14 @@ declare_clippy_lint! {
117115
impl_lint_pass!(FormatArgs => [FORMAT_IN_FORMAT_ARGS, NEEDLESS_FORMAT_ARGS, TO_STRING_IN_FORMAT_ARGS]);
118116

119117
pub struct FormatArgs {
120-
support_arg_capture: bool,
118+
format_args_capture: bool,
121119
}
122120

123121
impl FormatArgs {
124122
#[must_use]
125123
pub fn new(msrv: Option<RustcVersion>) -> Self {
126124
Self {
127-
support_arg_capture: meets_msrv(msrv, msrvs::FORMAT_ARGS_CAPTURE),
125+
format_args_capture: meets_msrv(msrv, msrvs::FORMAT_ARGS_CAPTURE),
128126
}
129127
}
130128
}
@@ -150,14 +148,15 @@ impl<'tcx> LateLintPass<'tcx> for FormatArgs {
150148
check_to_string_in_format_args(cx, name, arg.param.value);
151149
}
152150

153-
// if at least some of the arguments/format/precision are referenced by an index,
154-
// e.g. format!("{} {1}", foo, bar) or format!("{:1$}", foo, 2)
155-
// we cannot remove an argument from a list until we support renumbering.
156-
// We are OK if we inline all numbered arguments.
157-
if !self.support_arg_capture {
151+
if !self.format_args_capture {
158152
return;
159153
}
160154
let mut inline_spans = Vec::new();
155+
// If any of the arguments are referenced by an index number,
156+
// and that argument is not a simple variable and cannot be inlined,
157+
// we cannot remove any other arguments in the format string,
158+
// because the index numbers might be wrong after inlining.
159+
// Example of an un-inlinable format: print!("{}{1}", foo, 2)
161160
let do_inlining = format_args.params().all(|p| check_inline(cx, &p, &mut inline_spans));
162161
if do_inlining && !inline_spans.is_empty()
163162
{

clippy_utils/src/macros.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ fn span_from_inner(base: SpanData, inner: rpf::InnerSpan) -> Span {
545545
)
546546
}
547547

548+
/// How a format parameter is used in the format string
548549
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
549550
pub enum FormatParamKind {
550551
/// An implicit parameter , such as `{}` or `{:?}`.
@@ -559,10 +560,14 @@ pub enum FormatParamKind {
559560
NamedInline(Symbol),
560561
}
561562

563+
/// Where a format parameter is being used in the format string
562564
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
563565
pub enum FormatParamUsage {
566+
/// Appears as an argument, e.g. `format!("{}", foo)`
564567
Argument,
568+
/// Appears as a width, e.g. `format!("{:width$}", foo, width = 1)`
565569
Width,
570+
/// Appears as a precision, e.g. `format!("{:.precision$}", foo, precision = 1)`
566571
Precision,
567572
}
568573

@@ -577,13 +582,12 @@ pub enum FormatParamUsage {
577582
/// and a [`FormatParamKind::NamedInline("precision")`] `.kind` with a `.value` of `2`
578583
#[derive(Debug, Copy, Clone)]
579584
pub struct FormatParam<'tcx> {
580-
/// How this format param is being used - argument/width/precision
581-
pub usage: FormatParamUsage,
582-
583585
/// The expression this parameter refers to.
584586
pub value: &'tcx Expr<'tcx>,
585587
/// How this parameter refers to its `value`.
586588
pub kind: FormatParamKind,
589+
/// Where this format param is being used - argument/width/precision
590+
pub usage: FormatParamUsage,
587591
/// Span of the parameter, may be zero width. Includes the whitespace of implicit parameters.
588592
///
589593
/// ```text

src/docs/needless_format_args.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,4 @@ format!("{var:.prec$}"); // precision and asterisk support
3030
Format string uses an indexed argument that cannot be inlined.
3131
Supporting this case requires re-indexing of the format string.
3232

33-
Until implemnted, `print!("{0}={1}", var, 1+2)` should be changed to `print!("{var}={0}", 1+2)` by hand.
34-
35-
changelog: [`needless-format-args`]: A new lint to inline format arguments, i.e. `print!("{}", var)` into `print!("{var}")`
33+
Until implemented, `print!("{0}={1}", var, 1+2)` should be changed to `print!("{var}={0}", 1+2)` by hand.

tests/ui/needless_format_args.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![allow(named_arguments_used_positionally)]
77
#![allow(unused_variables)]
88
#![warn(clippy::needless_format_args)]
9+
#![feature(custom_inner_attributes)]
910

1011
fn tester(fn_arg: i32) {
1112
let local_i32 = 1;
@@ -120,3 +121,15 @@ fn tester(fn_arg: i32) {
120121
fn main() {
121122
tester(42);
122123
}
124+
125+
fn _under_msrv() {
126+
#![clippy::msrv = "1.57"]
127+
let local_i32 = 1;
128+
println!("don't expand='{local_i32}'");
129+
}
130+
131+
fn _meets_msrv() {
132+
#![clippy::msrv = "1.58"]
133+
let local_i32 = 1;
134+
println!("expand='{local_i32}'");
135+
}

tests/ui/needless_format_args.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![allow(named_arguments_used_positionally)]
77
#![allow(unused_variables)]
88
#![warn(clippy::needless_format_args)]
9+
#![feature(custom_inner_attributes)]
910

1011
fn tester(fn_arg: i32) {
1112
let local_i32 = 1;
@@ -123,3 +124,15 @@ fn tester(fn_arg: i32) {
123124
fn main() {
124125
tester(42);
125126
}
127+
128+
fn _under_msrv() {
129+
#![clippy::msrv = "1.57"]
130+
let local_i32 = 1;
131+
println!("don't expand='{}'", local_i32);
132+
}
133+
134+
fn _meets_msrv() {
135+
#![clippy::msrv = "1.58"]
136+
let local_i32 = 1;
137+
println!("expand='{}'", local_i32);
138+
}

0 commit comments

Comments
 (0)