@@ -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! {
117115impl_lint_pass ! ( FormatArgs => [ FORMAT_IN_FORMAT_ARGS , NEEDLESS_FORMAT_ARGS , TO_STRING_IN_FORMAT_ARGS ] ) ;
118116
119117pub struct FormatArgs {
120- support_arg_capture : bool ,
118+ format_args_capture : bool ,
121119}
122120
123121impl 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 {
0 commit comments