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

Detect println!("{x}", x) #105225

Closed
estebank opened this issue Dec 3, 2022 · 4 comments · Fixed by #115324
Closed

Detect println!("{x}", x) #105225

estebank opened this issue Dec 3, 2022 · 4 comments · Fixed by #115324
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Dec 3, 2022

When encountering println!("{x}", x) the user either meant println!("{x}") or println!("{x}", x=x). The later is only likely in <=2018 editions, while the former is what they actually likely wanted. We should suggest it.

@estebank estebank added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. A-diagnostics Area: Messages for errors, warnings, and lints P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Dec 3, 2022
@atamakahere-git
Copy link

Hello, I would like to work on this issue with some guidance.
It would be great if someone can give me a lead to start.
@rustbot claim

@estebank
Copy link
Contributor Author

estebank commented Dec 6, 2022

You'll want to start looking at

diag.span_label(fmt_span, "formatting specifier missing");
and work your way backwards from there.

You should look at the unused list

and check if the name corresponds to one of the pieces. If it does, then point at it while mentioning that it is being captured by the macro already.

@atamakahere-git
Copy link

Hello @estebank ,
I was trying to do the same as you told but I'm not able to check if the unused arg is present in the piece,
I can check the currently unused arg in the closure here

Current unused arg is:-

args.explicit_args()[i].expr.to_ty().unwrap().kind.is_simple_path().unwrap()

Gives out "x" for println!("{x}",x);
But I'm not able to compare it to pieces as it contains either String() literal or NextArgument() and NextArgument() doesn't contain the literal it refers to.

Please correct me If I misunderstood something.

@Solo-steven
Copy link

@rustbot claim

bors added a commit to rust-lang-ci/rust that referenced this issue Oct 18, 2023
Suggest removing redundant arguments in format!()

Closes rust-lang#105225. This is also a follow-up to rust-lang#105635, which seems to have become stale.

r? `@estebank`
@bors bors closed this as completed in 111adde Oct 23, 2023
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
3 participants