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

needless_raw_strings, needless_raw_string_hashes: False negative with macros #13503

Closed
musjj opened this issue Oct 5, 2024 · 4 comments · Fixed by #13504
Closed

needless_raw_strings, needless_raw_string_hashes: False negative with macros #13503

musjj opened this issue Oct 5, 2024 · 4 comments · Fixed by #13504
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@musjj
Copy link

musjj commented Oct 5, 2024

Summary

needless_raw_strings and needless_raw_string_hashes does not work when the string is inside macros.

Lint Name

needless_raw_strings, needless_raw_string_hashes

Reproducer

#![warn(clippy::needless_raw_strings, clippy::needless_raw_string_hashes)]

let _ = r"SELECT * FROM posts"; // needless-raw-strings
let _ = r#"SELECT * FROM posts"#; // needless-raw-strings
let _ = r##"SELECT * FROM "posts""##; // needless-raw-string-hashes

// None of lines below triggers any warning
println!(r"SELECT * FROM posts");
println!(r#"SELECT * FROM posts"#);
println!(r##"SELECT * FROM "posts""##);

// No warning when used with sqlx
sqlx::query!(
    r#"
    SELECT * FROM posts
    "#,
)
.fetch_all(&pool)
.await
.unwrap();

Version

rustc 1.83.0-nightly (363ae4188 2024-09-24)
binary: rustc
commit-hash: 363ae4188316b8b22cf6c1890bc73d84d05f70a4
commit-date: 2024-09-24
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

@musjj musjj added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Oct 5, 2024
@samueltardieu
Copy link
Contributor

@rustbot claim

@samueltardieu
Copy link
Contributor

The problem is not with macros, it is with format_args!() templates (and with macros using them, such as println!()).

@musjj
Copy link
Author

musjj commented Oct 26, 2024

It looks like that the issue is fixed in clippy 0.1.84 (c1db4dc242 2024-10-25) for println!. But it's still a problem for the sqlx macro:

sqlx::query!(r#"SELECT * FROM posts"#) // no warnings
    .fetch_all(&pool)
    .await?;

The macro is implemented here: /~https://github.com/launchbadge/sqlx/blob/028084bce3a741e995c3e6c559c6dbb27a62534d/src/macros/mod.rs#L314. Is there something that is confusing clippy here?

@samueltardieu
Copy link
Contributor

In general, Clippy doesn't lint the output of macros if it cannot determine that the tokens come from the source. Here, the procedural macro in sqlx seems to cut the link between the string literal and the emitted string. Clippy never sees the original string as it has been replaced by the macro expansion.

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-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants