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

does not detect {} not being in quotes when looking for a string literal, instead offers an incorrect solution #130170

Closed
EnderNon opened this issue Sep 9, 2024 · 6 comments · Fixed by #131430
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@EnderNon
Copy link

EnderNon commented Sep 9, 2024

Code

use serde_json;

let idsmap: HashMap<String, u8> = serde_json::from_reader(fs::File::open("id_keys.json").expect("error 3"))
    .expect("error 4");
println!({},idsmap.get("1stSpellCost"));

Current output

error: format argument must be a string literal
  --> src/main.rs:55:14
   |
55 |     println!({},idsmap.get("1stSpellCost"));
   |              ^^
   |
help: you might be missing a string literal to format with
   |
55 |     println!("{} {}", {},idsmap.get("1stSpellCost"));
   |              ++++++++

Desired output

error: format argument must be a string literal
  --> src/main.rs:55:14
   |
55 |     println!({},idsmap.get("1stSpellCost"));
   |              ^^
   |
help: you might be missing brackets
   |
55 |     println!("{}",idsmap.get("1stSpellCost"));
   |              +  +

Rationale and extra context

Cargo does not seem to be detecting the {} outside brackets and provides an incredibly strange fix instead.

Other cases

No response

Rust Version

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7

Anything else?

the file is from here:
https://github.com/Wynntils/Static-Storage/blob/main/Reference/id_keys.json
You will need serde_json crate.

@EnderNon EnderNon added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 9, 2024
@lolbinarycat lolbinarycat changed the title does not detect {} not being in brackets when looking for a string literal, instead offers a stupid solution does not detect {} not being in quotes when looking for a string literal, instead offers an incorrect solution Sep 9, 2024
@lolbinarycat
Copy link
Contributor

it's worth noting that {} is a valid rust expression, it evaluates to the empty tuple.

this is perfectly valid code:

fn main() {
    println!("{:?}", {});
}

minimal repro:

fn main() {
    println!({});
}

@lolbinarycat lolbinarycat added D-confusing Diagnostics: Confusing error or lint that should be reworked. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue labels Sep 9, 2024
@EnderNon
Copy link
Author

EnderNon commented Sep 9, 2024

huh, didn't know that. good to know ig

@asquared31415
Copy link
Contributor

I think it is worth special casing this in diagnostics though, if someone wanted to print a unit type (for some reason?) they probably should use the () value or just embed it directly in the format string. An empty {} expression as the first argument of a format macro is almost certainly a mistake.

@surechen
Copy link
Contributor

surechen commented Oct 8, 2024

@rustbot claim

surechen added a commit to surechen/rust that referenced this issue Oct 9, 2024
… format macro.

For example:
```rust
let s = "123";
println!({}, "sss", s);
```
Suggest:
`println!("{:?} {} {}", {}, "sss", s);`

fixes rust-lang#130170
surechen added a commit to surechen/rust that referenced this issue Oct 9, 2024
… format macro.

For example:
```rust
let s = "123";
println!({}, "sss", s);
```
Suggest:
`println!("{:?} {} {}", {}, "sss", s);`

fixes rust-lang#130170
surechen added a commit to surechen/rust that referenced this issue Oct 9, 2024
… format macro.

For example:
```rust
let s = "123";
println!({}, "sss", s);
```
Suggest:
`println!("{:?} {} {}", {}, "sss", s);`

fixes rust-lang#130170
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 14, 2024
Special treatment empty tuple when suggest adding a string literal in format macro.

For example:
```rust
let s = "123";
println!({}, "sss", s);
```
Suggest:
`println!("{:?} {} {}", {}, "sss", s);`

fixes rust-lang#130170
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 14, 2024
Special treatment empty tuple when suggest adding a string literal in format macro.

For example:
```rust
let s = "123";
println!({}, "sss", s);
```
Suggest:
`println!("{:?} {} {}", {}, "sss", s);`

fixes rust-lang#130170
@bors bors closed this as completed in ceced53 Oct 14, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 14, 2024
Rollup merge of rust-lang#131430 - surechen:fix_130495, r=jieyouxu

Special treatment empty tuple when suggest adding a string literal in format macro.

For example:
```rust
let s = "123";
println!({}, "sss", s);
```
Suggest:
`println!("{:?} {} {}", {}, "sss", s);`

fixes rust-lang#130170
@asquared31415
Copy link
Contributor

The PR that closed this doesn't seem to actually apply the wanted fix, this should be reopened.

@surechen
Copy link
Contributor

The PR that closed this doesn't seem to actually apply the wanted fix, this should be reopened.

Hello, thank you very much for the suggestion. My idea is to find a slightly more general solution instead of special treatment for rare cases. If you think it should be modified as Desired output, I completely agree with reopening the issue. ☺️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants