Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

literal_string_with_formatting_args is trigger by Actix Web's wildcard route definitions #13928

Open
Thomasdezeeuw opened this issue Jan 2, 2025 · 8 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@Thomasdezeeuw
Copy link
Contributor

Summary

Actix Web (https://crates.io/crates/actix-web) supports wildcard in their HTTP route definition macro. It looks something like #[post("/path/{variable:.*}")]. This triggers literal_string_with_formatting_args incorrectly.

As a work around we're adding #![allow(clippy::literal_string_with_formatting_args)] to all our files.

Lint Name

literal_string_with_formatting_args

Reproducer

I tried this code:

use actix_web::{post, HttpRequest, HttpResponse};

#[post("/path/{variable:.*}")]
pub async fn my_route(req: HttpRequest) -> HttpResponse {
    Ok(HttpResponse::Ok())
}

Creates (roughly) the following error:

error: this looks like a formatting argument but it is not part of a formatting macro
  --> path.rs:1
   |
1 | #[post("/path/{variable:.*}")]
   |              ^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#literal_string_with_formatting_args
   = note: `-D clippy::literal-string-with-formatting-args` implied by `-D warnings`
   = help: to override `-D warnings` add `#[allow(clippy::literal_string_with_formatting_args)]`

Version

rustc 1.85.0-nightly (45d11e51b 2025-01-01)
binary: rustc
commit-hash: 45d11e51bb66c2deb63a006fe3953c4b6fbc50c2
commit-date: 2025-01-01
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6

Additional Labels

No response

@Thomasdezeeuw Thomasdezeeuw added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 2, 2025
@Thomasdezeeuw Thomasdezeeuw changed the title literal_string_with_formatting_args is trigger by Actix Web's wildcare route definitions literal_string_with_formatting_args is trigger by Actix Web's wildcard route definitions Jan 2, 2025
@mladedav
Copy link
Contributor

mladedav commented Jan 8, 2025

axum also triggers it since 0.8. Though it seems that this happens only if a variable with the same name is in the same scope.

@Arvamer
Copy link

Arvamer commented Jan 9, 2025

ClickHouse's query parameters use {name:Type}, which is also caught by this lint. Perhaps the lint should trigger only when a string literal is used as an argument of functions like expect? Otherwise this is bound to trigger a lot of false positives in the general case.

@conradludgate
Copy link

I have another weird case, which I think is slightly different:

warning: this looks like a formatting argument but it is not part of a formatting macro
 --> /Users/conrad/Documents/neon/neon/clippy.toml:2:6
  |
2 |     "tokio::task::block_in_place",
  |      ^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#literal_string_with_formatting_args
  = note: `#[warn(clippy::literal_string_with_formatting_args)]` on by default

I wonder why clippy is linting our clippy.toml 🤔

@y21
Copy link
Member

y21 commented Jan 9, 2025

I have another weird case, which I think is slightly different:

warning: this looks like a formatting argument but it is not part of a formatting macro
 --> /Users/conrad/Documents/neon/neon/clippy.toml:2:6
  |
2 |     "tokio::task::block_in_place",
  |      ^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#literal_string_with_formatting_args
  = note: `#[warn(clippy::literal_string_with_formatting_args)]` on by default

I wonder why clippy is linting our clippy.toml 🤔

That's most likely #13885 (it's emitting a warning on a dummy span at position 0 which happens to be where clippy.toml is located in the source map)

@y21
Copy link
Member

y21 commented Jan 10, 2025

cc @GuillaumeGomez (lint author). It seems unfortunate that users of several popular crates that happen to share syntax with that of format strings can run into false positives with this and have to add #![allow]s (and it's also a shame because I could see the lint be very useful possibly if it was less eager and that it could catch real errors).

If there's no trivial or obvious fix, we might just want to (temporarily) move the lint to nursery, just to have more time on this before it makes it to stable because the lint is already on beta. We'll also probably want to backport a fix, which a category change would likely be the easiest.

@GuillaumeGomez
Copy link
Member

There is a fix waiting for review/approval open at #13953. It's ready and fixes all issues listed in this is issue and in #13885.

@y21
Copy link
Member

y21 commented Jan 10, 2025

Ah... I think I misunderstood this issue. I thought the issue here would be different from #13885 in that the lint thinks that the #[post("/path/{variable:.*}")] route string (which is in user-written code, so unrelated to dummy spans) looks like a format string and lints that string, and I thought it would affect most users of actix_web that use these macros.

Looking closer at the repro in the OP though (specifically the span of the warning and what it points at; it doesn't cover the whole {} part of the string) I guess the dummy span of a different expression just happens to be there coincidentally, so it probably is another instance of #13885. (Or is that just a manually constructed warning since the OP says "roughly" and it actually does cover the whole {variable:.*}?)

I also couldn't really repro the warning for actix_web in the OP locally (after having fixed some of the type errors in the snippet).

There are two other comments here where I'm not quite sure if they're the same or the issue that I described though (and it's a bit hard to tell because they don't have a reproducer). So yeah, if that fixes all issues, that should be fine, then. Bit of a confusing situation though ^^

yukiiiteru added a commit to yukiiiteru/volo that referenced this issue Jan 20, 2025
This commit fixes clippy warning `useless_conversion`.

In addition, a false positive rule `literal_string_with_formatting_args`
was introduced in the nightly toolchain, which we will ignore for now.

Ref: rust-lang/rust-clippy#13928

Signed-off-by: Yu Li <[email protected]>
yukiiiteru added a commit to yukiiiteru/volo that referenced this issue Jan 20, 2025
This commit fixes clippy warning `useless_conversion`.

Also, a false positive rule `literal_string_with_formatting_args` is
introduced in the nightly toolchain, we cannot `allow` it because it
is a new rule that does not exist in the stable toolchain, so we
should ignore it manually.

Ref: rust-lang/rust-clippy#13928

Signed-off-by: Yu Li <[email protected]>
yukiiiteru added a commit to cloudwego/volo that referenced this issue Jan 20, 2025
* chore(volo): bump volo to 0.10.4

* chore: fix some clippy warnings

This commit fixes clippy warning `useless_conversion`.

Also, a false positive rule `literal_string_with_formatting_args` is
introduced in the nightly toolchain, we cannot `allow` it because it
is a new rule that does not exist in the stable toolchain, so we
should ignore it manually.

Ref: rust-lang/rust-clippy#13928

---------

Signed-off-by: Yu Li <[email protected]>
@jcdyer
Copy link

jcdyer commented Jan 22, 2025

This is a little more obscure, perhaps, but seed 0.9.2's log! macro also runs afoul of this lint.

Would it make sense to be able to configure a list of known_formatting_macros?

Edit: I just updated my rustc, and see this on the following version:

$ cargo --version
cargo 1.85.0-beta.5 (d73d2caf9 2024-12-31)

Specifically, I get:

$ cargo clippy
warning: this looks like a formatting argument but it is not part of a formatting macro
   --> datagen-client/src/update/project_reading.rs:304:56
    |
304 |             ::seed::log!("Error getting media devices: {:?}", sopt);
    |                                                        ^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#literal_string_with_formatting_args
    = note: `#[warn(clippy::literal_string_with_formatting_args)]` on by default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

7 participants