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

False positive literal_string_with_formatting_args for non-static templete string expansion with replace. #13989

Open
anatawa12 opened this issue Jan 12, 2025 · 3 comments · May be fixed by #14014
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

@anatawa12
Copy link

Summary

When I write code expanding dynamically changed template string, I encountered literal_string_with_formatting_args.

I think calling replace("{var}", &var) is likely to be intended to not treat as format string in rust.

Lint Name

literal_string_with_formatting_args

Reproducer

I tried this code:

fn lint_test(templete: &str) -> String {
    let version = env!("CARGO_PKG_VERSION");
    templete.replace("{version}", version)
}

I saw this happen:

warning: this looks like a formatting argument but it is not part of a formatting macro
 --> vrc-get-gui/src/main.rs:3:23
  |
3 |     templete.replace("{version}", version)
  |                       ^^^^^^^^^
  |
  = 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 expected to see this happen:
No lint for this code.

Version

rustc 1.85.0-beta.1 (e30eefff4 2025-01-08)
binary: rustc
commit-hash: e30eefff41038ceea427009023627d6d66b36715
commit-date: 2025-01-08
host: aarch64-apple-darwin
release: 1.85.0-beta.1
LLVM version: 19.1.6

Additional Labels

the commit message and description of PR adds literal_string_with_formatting_args says literal_string_with_formatting_args is pedantic so it might be it's a bug that literal_string_with_formatting_args is categorized as suspicious;

According to the edit history of the PR description, it's originally categorized as suspicious and changed to pedantic later, but code has not changed (or rebase miss in some point).

When I searched on github, I can see the thousands of similar false positive cases are exists.

@anatawa12 anatawa12 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 12, 2025
@Xuanwo
Copy link

Xuanwo commented Jan 17, 2025

Thank you @anatawa12 for the report. We at opendal also hit this case, where we are performing:

templete.replace("{region}", region)

I believe we should change it to pedantic or fix its behavior to avoid triggering for places that don't access a format_args, such as str::replace.

@Xuanwo
Copy link

Xuanwo commented Jan 17, 2025

Oops, during my in-depth research, I found that this issue is a duplicate of #13989. It is also likely to be resolved in #13953.

I believe it's safe to close this issue now.

@anatawa12
Copy link
Author

I already saw #13953 but I think this is not fixed by #13953 since #13953 is about proc macro or something like this, but this is about real, hand-written code.

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

Successfully merging a pull request may close this issue.

2 participants