Skip to contentSkip to conversation

kbuild: rust: allow `clippy::uninlined_format_args`

PATCH Applied Miguel Ojeda 5 participants 16 messages Mar 31 – Apr 9

Subsystems (4)
lore
Share & tools
lore

Miguel Ojeda

author maintainer
#0 lore raw

PATCH 1/2 kbuild: rust: allow `clippy::uninlined_format_args`

Clippy in Rust 1.88.0 (only) reports [1]:

    warning: variables can be used directly in the `format!` string
       --> rust/macros/module.rs:112:23
        |
    112 |         let content = format!("{param}:{content}", param = param, content = content);
        |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
        = note: `-W clippy::uninlined-format-args` implied by `-W clippy::all`
        = help: to override `-W clippy::all` add `#[allow(clippy::uninlined_format_args)]`
    help: change this to
        |
    112 -         let content = format!("{param}:{content}", param = param, content = content);
    112 +         let content = format!("{param}:{content}");

    warning: variables can be used directly in the `format!` string
       --> rust/macros/module.rs:198:14
        |
    198 |         t => panic!("Unsupported parameter type {}", t),
        |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
        = note: `-W clippy::uninlined-format-args` implied by `-W clippy::all`
        = help: to override `-W clippy::all` add `#[allow(clippy::uninlined_format_args)]`
    help: change this to
        |
    198 -         t => panic!("Unsupported parameter type {}", t),
    198 +         t => panic!("Unsupported parameter type {t}"),
        |

The reason it only triggers in that version is that the lint was moved from pedantic to style in Rust 1.88.0 and then back to pedantic in Rust 1.89.0 [2][3].

In the first case, the suggestion is fair and a pure simplification, thus we will clean it up separately.

To keep the behavior the same across all versions, and since the lint does not work for all macros (e.g. custom ones like pr_info!), disable it globally.

---
 Makefile | 1 +
 1 file changed, 1 insertion(+)
Makefile +1 -0
index 1a219bf1c771..a63684c36d60 100644
@@ -494,6 +494,7 @@ export rust_common_flags := --edition=2021 \
 			    -Wclippy::ptr_cast_constness \
 			    -Wclippy::ref_as_ptr \
 			    -Wclippy::undocumented_unsafe_blocks \
+			    -Aclippy::uninlined_format_args \
 			    -Wclippy::unnecessary_safety_comment \
 			    -Wclippy::unnecessary_safety_doc \
 			    -Wrustdoc::missing_crate_level_docs \
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 2/2 rust: macros: simplify `format!` arguments

Clippy in Rust 1.88.0 (only) reported [1] up to the previous commit:

    warning: variables can be used directly in the `format!` string
       --> rust/macros/module.rs:112:23
        |
    112 |         let content = format!("{param}:{content}", param = param, content = content);
        |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
        = note: `-W clippy::uninlined-format-args` implied by `-W clippy::all`
        = help: to override `-W clippy::all` add `#[allow(clippy::uninlined_format_args)]`
    help: change this to
        |
    112 -         let content = format!("{param}:{content}", param = param, content = content);
    112 +         let content = format!("{param}:{content}");

The reason it only triggers in that version is that the lint was moved from pedantic to style in Rust 1.88.0 and then back to pedantic in Rust 1.89.0 [2][3].

In this case, the suggestion is fair and a pure simplification, thus just apply it.

In addition, do the same for another place in the file that Clippy does not report because it is multi-line.

---
 rust/macros/module.rs | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
rust/macros/module.rs +2 -7
index e16298e520c7..06c18e207508 100644
@@ -52,12 +52,7 @@ fn new(module: &'a str) -> Self {
     fn emit_base(&mut self, field: &str, content: &str, builtin: bool, param: bool) {
         let string = if builtin {
             // Built-in modules prefix their modinfo strings by `module.`.
-            format!(
-                "{module}.{field}={content}\0",
-                module = self.module,
-                field = field,
-                content = content
-            )
+            format!("{module}.{field}={content}\0", module = self.module)
         } else {
             // Loadable modules' modinfo strings go as-is.
             format!("{field}={content}\0")
@@ -109,7 +104,7 @@ fn emit_internal(&mut self, field: &str, content: &str, param: bool) {
     }
 
     fn emit_param(&mut self, field: &str, param: &str, content: &str) {
-        let content = format!("{param}:{content}", param = param, content = content);
+        let content = format!("{param}:{content}");
         self.emit_internal(field, &content, true);
     }
 
Signature · 1 line
2.53.0
reviewer
#6 lore raw
In reply to Miguel Ojeda
Quoted 35 lines · Miguel Ojeda

Clippy in Rust 1.88.0 (only) reports [1]:

    warning: variables can be used directly in the `format!` string
       --> rust/macros/module.rs:112:23
        |
    112 |         let content = format!("{param}:{content}", param = param, content = content);
        |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
        = note: `-W clippy::uninlined-format-args` implied by `-W clippy::all`
        = help: to override `-W clippy::all` add `#[allow(clippy::uninlined_format_args)]`
    help: change this to
        |
    112 -         let content = format!("{param}:{content}", param = param, content = content);
    112 +         let content = format!("{param}:{content}");

    warning: variables can be used directly in the `format!` string
       --> rust/macros/module.rs:198:14
        |
    198 |         t => panic!("Unsupported parameter type {}", t),
        |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
        = note: `-W clippy::uninlined-format-args` implied by `-W clippy::all`
        = help: to override `-W clippy::all` add `#[allow(clippy::uninlined_format_args)]`
    help: change this to
        |
    198 -         t => panic!("Unsupported parameter type {}", t),
    198 +         t => panic!("Unsupported parameter type {t}"),
        |

The reason it only triggers in that version is that the lint was moved from pedantic to style in Rust 1.88.0 and then back to pedantic in Rust 1.89.0 [2][3].

Quoted 6 lines

In the first case, the suggestion is fair and a pure simplification, thus we will clean it up separately.

To keep the behavior the same across all versions, and since the lint does not work for all macros (e.g. custom ones like pr_info!), disable it globally.

Does it produce a false positive, or it's a false negative? If it's the latter, I think we don't need to disable the lint.

Best,
Gary

Quoted 22 lines
---
 Makefile | 1 +
 1 file changed, 1 insertion(+)
Makefile +1 -0
index 1a219bf1c771..a63684c36d60 100644
@@ -494,6 +494,7 @@ export rust_common_flags := --edition=2021 \
 			    -Wclippy::ptr_cast_constness \
 			    -Wclippy::ref_as_ptr \
 			    -Wclippy::undocumented_unsafe_blocks \
+			    -Aclippy::uninlined_format_args \
 			    -Wclippy::unnecessary_safety_comment \
 			    -Wclippy::unnecessary_safety_doc \
 			    -Wrustdoc::missing_crate_level_docs \
reviewer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] rust: macros: simplify `format!` arguments

In reply to Miguel Ojeda
Quoted 23 lines · Miguel Ojeda

Clippy in Rust 1.88.0 (only) reported [1] up to the previous commit:

    warning: variables can be used directly in the `format!` string
       --> rust/macros/module.rs:112:23
        |
    112 |         let content = format!("{param}:{content}", param = param, content = content);
        |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
        = note: `-W clippy::uninlined-format-args` implied by `-W clippy::all`
        = help: to override `-W clippy::all` add `#[allow(clippy::uninlined_format_args)]`
    help: change this to
        |
    112 -         let content = format!("{param}:{content}", param = param, content = content);
    112 +         let content = format!("{param}:{content}");

The reason it only triggers in that version is that the lint was moved from pedantic to style in Rust 1.88.0 and then back to pedantic in Rust 1.89.0 [2][3].

In this case, the suggestion is fair and a pure simplification, thus just apply it.

Quoted 7 lines

In addition, do the same for another place in the file that Clippy does not report because it is multi-line.

Quoted 3 lines
---
 rust/macros/module.rs | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
#7 lore raw
In reply to Gary Guo
Quoted 3 lines · Gary Guo

Does it produce a false positive, or it's a false negative? If it's the latter, I think we don't need to disable the lint.

If you mean for custom macros, then I think it is just that it doesn't take those into account at all. So I guess you could say false negative if you consider that it should.

In any case, originally I just cleaned it, because it wasn't a big deal either way, but then I thought it would be best to keep the behavior the same. So either we disable or enable everywhere.

Cheers,
Miguel

reviewer
#8 lore raw
Quoted 9 lines · Miguel Ojeda
Quoted 3 lines · Gary Guo

Does it produce a false positive, or it's a false negative? If it's the latter, I think we don't need to disable the lint.

If you mean for custom macros, then I think it is just that it doesn't take those into account at all. So I guess you could say false negative if you consider that it should.

Quoted 6 lines

In any case, originally I just cleaned it, because it wasn't a big deal either way, but then I thought it would be best to keep the behavior the same. So either we disable or enable everywhere.

Cheers,
Miguel

I mean the lint is kinda useful, and I don't want to disable it just because it doesn't exist in older versions of rustc.

Best,
Gary

#9 lore raw
Quoted 3 lines · Gary Guo

I mean the lint is kinda useful, and I don't want to disable it just because it doesn't exist in older versions of rustc.

Yeah, personally I like the inlined way, i.e. I use it myself, so I don't mind enabling it everywhere if people is happy that it only applies to some cases.

Another consideration is that the issue linked mentions that they don't want to mix inline and not (for field access cases), so that could be annoying for some, which is why they moved it back.

Either way sounds fine for me.

Anyway, if we enable it, I should apply the other suggestion too, put the Cc: stable@ on them, and switch to -W here and remove the Cc:
stable@.

Thanks for taking a look!

Cheers,
Miguel

reviewer
#10 lore raw
Quoted 5 lines · Miguel Ojeda
Quoted 3 lines · Gary Guo

I mean the lint is kinda useful, and I don't want to disable it just because it doesn't exist in older versions of rustc.

Quoted 7 lines

Yeah, personally I like the inlined way, i.e. I use it myself, so I don't mind enabling it everywhere if people is happy that it only applies to some cases.

Another consideration is that the issue linked mentions that they don't want to mix inline and not (for field access cases), so that could be annoying for some, which is why they moved it back.

For mixed cases I suppose it's really up to personal taste. In that case we can say it does have "false positive" and allow the lint.

Best,
Gary

Quoted 11 lines

Either way sounds fine for me.

Anyway, if we enable it, I should apply the other suggestion too, put the Cc: stable@ on them, and switch to -W here and remove the Cc:
stable@.

Thanks for taking a look!

Cheers,
Miguel

#3 lore raw

Re: [PATCH 2/2] rust: macros: simplify `format!` arguments

In reply to Miguel Ojeda
Quoted 24 lines · Miguel Ojeda

Clippy in Rust 1.88.0 (only) reported [1] up to the previous commit:

    warning: variables can be used directly in the `format!` string
       --> rust/macros/module.rs:112:23
        |
    112 |         let content = format!("{param}:{content}", param = param, content = content);
        |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
        = note: `-W clippy::uninlined-format-args` implied by `-W clippy::all`
        = help: to override `-W clippy::all` add `#[allow(clippy::uninlined_format_args)]`
    help: change this to
        |
    112 -         let content = format!("{param}:{content}", param = param, content = content);
    112 +         let content = format!("{param}:{content}");

The reason it only triggers in that version is that the lint was moved from pedantic to style in Rust 1.88.0 and then back to pedantic in Rust 1.89.0 [2][3].

In this case, the suggestion is fair and a pure simplification, thus just apply it.

Quoted 7 lines

In addition, do the same for another place in the file that Clippy does not report because it is multi-line.

I will pick this one up together with the other one, but if someone prefers that I don't, please shout (e.g. if modules wants to pick it themselves).

An Acked-by is also appreciated, thanks!

Cheers,
Miguel

Applied
#11 lore raw
In reply to Miguel Ojeda
Quoted 43 lines · Miguel Ojeda

Clippy in Rust 1.88.0 (only) reports [1]:

    warning: variables can be used directly in the `format!` string
       --> rust/macros/module.rs:112:23
        |
    112 |         let content = format!("{param}:{content}", param = param, content = content);
        |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
        = note: `-W clippy::uninlined-format-args` implied by `-W clippy::all`
        = help: to override `-W clippy::all` add `#[allow(clippy::uninlined_format_args)]`
    help: change this to
        |
    112 -         let content = format!("{param}:{content}", param = param, content = content);
    112 +         let content = format!("{param}:{content}");

    warning: variables can be used directly in the `format!` string
       --> rust/macros/module.rs:198:14
        |
    198 |         t => panic!("Unsupported parameter type {}", t),
        |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
        = note: `-W clippy::uninlined-format-args` implied by `-W clippy::all`
        = help: to override `-W clippy::all` add `#[allow(clippy::uninlined_format_args)]`
    help: change this to
        |
    198 -         t => panic!("Unsupported parameter type {}", t),
    198 +         t => panic!("Unsupported parameter type {t}"),
        |

The reason it only triggers in that version is that the lint was moved from pedantic to style in Rust 1.88.0 and then back to pedantic in Rust 1.89.0 [2][3].

In the first case, the suggestion is fair and a pure simplification, thus we will clean it up separately.

To keep the behavior the same across all versions, and since the lint does not work for all macros (e.g. custom ones like pr_info!), disable it globally.

Quoted 5 lines

Applied series to rust-next -- thanks everyone!

(If wanted by modules, I can drop the top commit.)

Cheers,
Miguel

#12 lore raw
In reply to Miguel Ojeda
Quoted 36 lines · Miguel Ojeda

Clippy in Rust 1.88.0 (only) reports [1]:

    warning: variables can be used directly in the `format!` string
       --> rust/macros/module.rs:112:23
        |
    112 |         let content = format!("{param}:{content}", param = param, content = content);
        |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
        = note: `-W clippy::uninlined-format-args` implied by `-W clippy::all`
        = help: to override `-W clippy::all` add `#[allow(clippy::uninlined_format_args)]`
    help: change this to
        |
    112 -         let content = format!("{param}:{content}", param = param, content = content);
    112 +         let content = format!("{param}:{content}");

    warning: variables can be used directly in the `format!` string
       --> rust/macros/module.rs:198:14
        |
    198 |         t => panic!("Unsupported parameter type {}", t),
        |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
        = note: `-W clippy::uninlined-format-args` implied by `-W clippy::all`
        = help: to override `-W clippy::all` add `#[allow(clippy::uninlined_format_args)]`
    help: change this to
        |
    198 -         t => panic!("Unsupported parameter type {}", t),
    198 +         t => panic!("Unsupported parameter type {t}"),
        |

The reason it only triggers in that version is that the lint was moved from pedantic to style in Rust 1.88.0 and then back to pedantic in Rust 1.89.0 [2][3].

Quoted 6 lines

In the first case, the suggestion is fair and a pure simplification, thus we will clean it up separately.

To keep the behavior the same across all versions, and since the lint does not work for all macros (e.g. custom ones like pr_info!), disable it globally.

Seeing this patch a bit late but in clippy 1.85.0 there is #[clippy::format_args] which would permit us to make the lint work with our custom macros.

https://doc.rust-lang.org/nightly/clippy/attribs.html#clippyformat_args

#13 lore raw
Quoted 4 lines · Tamir Duberstein

Seeing this patch a bit late but in clippy 1.85.0 there is #[clippy::format_args] which would permit us to make the lint work with our custom macros.

+1, that may be good to consider, especially with the bump -- added and backlinked in:

https://github.com/Rust-for-Linux/linux/issues/349

Maybe an issue would be good to create too.

It is good to see Clippy adding more attributes, because I requested a similar one for other lints involving macros in that list, e.g.

https://github.com/rust-lang/rust-clippy/issues/11303

So hopefully we will eventually get those too.

Thanks!

Cheers,
Miguel

maintainer Acked-by
#4 lore raw

Re: [PATCH 2/2] rust: macros: simplify `format!` arguments

In reply to Miguel Ojeda

Hi Miguel,

Quoted 29 lines · Miguel Ojeda
Quoted 27 lines · Miguel Ojeda

Clippy in Rust 1.88.0 (only) reported [1] up to the previous commit:

    warning: variables can be used directly in the `format!` string
       --> rust/macros/module.rs:112:23
        |
    112 |         let content = format!("{param}:{content}", param = param, content = content);
        |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
        = note: `-W clippy::uninlined-format-args` implied by `-W clippy::all`
        = help: to override `-W clippy::all` add `#[allow(clippy::uninlined_format_args)]`
    help: change this to
        |
    112 -         let content = format!("{param}:{content}", param = param, content = content);
    112 +         let content = format!("{param}:{content}");

The reason it only triggers in that version is that the lint was moved from pedantic to style in Rust 1.88.0 and then back to pedantic in Rust 1.89.0 [2][3].

In this case, the suggestion is fair and a pure simplification, thus just apply it.

In addition, do the same for another place in the file that Clippy does not report because it is multi-line.

Quoted 8 lines
Quoted 4 lines

I will pick this one up together with the other one, but if someone prefers that I don't, please shout (e.g. if modules wants to pick it themselves).

Sounds good to me, thanks.

Quoted 1 line

An Acked-by is also appreciated, thanks!

Sami

#14 lore raw
In reply to Miguel Ojeda
Quoted 7 lines · Miguel Ojeda
Quoted 4 lines · Tamir Duberstein

Seeing this patch a bit late but in clippy 1.85.0 there is #[clippy::format_args] which would permit us to make the lint work with our custom macros.

Quoted 6 lines

+1, that may be good to consider, especially with the bump -- added and backlinked in:

https://github.com/Rust-for-Linux/linux/issues/349

Maybe an issue would be good to create too.

Turns out #[clippy::format_args] doesn't work for us due to the fmt! proc-macro.

It seems the handling of #[clippy::format_args] is more sophisticated than (at least I) expected: it doesn't blindly check the inputs to annotated macros, but rather looks for the place where fmt::Arguments are created.

In our case something like pr_info!("{}", i) ends up expanding to core::format_args!("{}", Adapter(&(i))), which does not trigger uninlined_format_args.

We also cannot fix that just by having fmt! assign Adapter(&(i)) to a local variable and then return fmt::Arguments, since core::format_args! borrows its arguments. The local would not live long enough.

I filed this upstream as https://github.com/rust-lang/rust-clippy/issues/16833.

reviewer
#15 lore raw
Quoted 28 lines · Tamir Duberstein
Quoted 7 lines · Miguel Ojeda
Quoted 4 lines · Tamir Duberstein

Seeing this patch a bit late but in clippy 1.85.0 there is #[clippy::format_args] which would permit us to make the lint work with our custom macros.

Quoted 6 lines

+1, that may be good to consider, especially with the bump -- added and backlinked in:

https://github.com/Rust-for-Linux/linux/issues/349

Maybe an issue would be good to create too.

Turns out #[clippy::format_args] doesn't work for us due to the fmt! proc-macro.

It seems the handling of #[clippy::format_args] is more sophisticated than (at least I) expected: it doesn't blindly check the inputs to annotated macros, but rather looks for the place where fmt::Arguments are created.

In our case something like pr_info!("{}", i) ends up expanding to core::format_args!("{}", Adapter(&(i))), which does not trigger uninlined_format_args.

Quoted 6 lines

We also cannot fix that just by having fmt! assign Adapter(&(i)) to a local variable and then return fmt::Arguments, since core::format_args! borrows its arguments. The local would not live long enough.

I filed this upstream as https://github.com/rust-lang/rust-clippy/issues/16833.

The issue is that by the time Clippy lints run, it only have post-expansion results. The pre-expansion AST is no longer available.

But if you run your lints pre-expansion, the name resolver and query engine is not yet available (resolution of macro names happen during expansion).

This is generally tricky, the way Clippy works is it tries to reconstruct the macro invocation from the expanded result.

Best,
Gary