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

write_literal breaks raw strings #13959

Closed
ChrisJefferson opened this issue Jan 7, 2025 · 1 comment · Fixed by #13990
Closed

write_literal breaks raw strings #13959

ChrisJefferson opened this issue Jan 7, 2025 · 1 comment · Fixed by #13990
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@ChrisJefferson
Copy link

ChrisJefferson commented Jan 7, 2025

Summary

write_literal doesn't correctly handle raw strings.

Reproducer

I tried this code:

writeln!(
                pre,
                "{}",
                r#"
$#CON colcheck "Use the sum of col {{index[0]}} for {{index[1]}}"
find colcheck: matrix indexed by [int(1..width), ALLRANGE] of bool"#
                "
            )?;

I expected write_literal to rewrite this to valid Rust. Instead, this happened:

writeln!(
                pre,
                "
$#CON colcheck \\"Use the sum of col {{{{index[0]}}}} for {{{{index[1]}}}}\\"
find colcheck: matrix indexed by [int(1..width), ALLRANGE] of bool"
            )?;

My preference would be to just leave the raw string as is, I can't see why that wouldn't be the easist, and least-suprising option.

Version

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1

Additional Labels

No response

@ChrisJefferson ChrisJefferson added the C-bug Category: Clippy is not doing the correct thing label Jan 7, 2025
@t-webber
Copy link

t-webber commented Jan 8, 2025

This problem appears to happen even for non-raw strings, like in this example:

writeln!(pre, "{}", "x with braces {x}")?;

Clippy suggests replacing it with

writeln!(pre, "{}", "x with braces {{x}}")?;

github-merge-queue bot pushed a commit that referenced this issue Jan 28, 2025
…estion (#13990)

fix #13959

The current implementation of the `write_literal` and `print_literal`
lint performs escaping for the second argument of `write!` ,`writeln!`,
`print!` and `println!` of the suggestion by first replacing `"` with
`\"`, and then replacing `\` with `\\`. Performing these replacements in
this order may lead to unnecessary backslashes being added if the
original code contains `"` (e.g. `"` -> `\\"`), potentially resulting in
a suggestion that causes the code to fail to compile.
In the issue mentioned above, it’s suggested to use raw strings as raw
strings, but implementing this would require an ad-hoc change to the
current implementation, so it has been deferred. (I'll implement this in
another PR)

changelog: [`write_literal`]: fix incorrect escaping of suggestions
changelog: [`print_literal`]: fix incorrect escaping of suggestions
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants