rust-lang / rust-lang/rust-clippy

False Positive: `needless_raw_string_hashes` when combined with `const_format::formatcp`

Open
#11,748 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

When using formatcp! from const_format,
Some FPs happen, following (to me) strange rules.
The same does not happen using the same string in e.g. println!.

Lint Name

needless_raw_string_hashes

Reproducer

Using const_format = "0.2" in Cargo.toml.

I tried this code:

#![deny(clippy::needless_raw_string_hashes)]

use const_format::formatcp;

const C: &str = "c";

fn main() {
    println!(
        "{}",
        formatcp!(
            // NOTE warning -> false positive!
            r#"a "b" {C} d"#
        )
    );

    println!(
        "{}",
        formatcp!(
            // no warning -> correct
            r#"a "b" {C}"#
        )
    );
}

I saw this happen:

error: unnecessary hashes around raw string literal
  --> clippy-raw-string-lit-test/src/main.rs:15:13
   |
15 |             r#"a "b" {C} d"#
   |             ^^^^^^^^^^^^^^^^

I expected to see this happen:

Version
rustc 1.75.0-nightly (9d83ac217 2023-10-31)
binary: rustc
commit-hash: 9d83ac217957eece2189eccf4a7232caec7232ee
commit-date: 2023-10-31
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.3
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the false positive from the issue in clippy-raw-string-lit-test/src/main.rs, using the provided const_format::formatcp examples. Compare the two raw string cases and trace the needless_raw_string_hashes lint handling. Done means the first case no longer produces a false positive while the lint continues to report genuinely unnecessary hashes.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.