-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Emit warning when named arguments are used positionally in format #98580
Conversation
r? @cjgillot (rust-highfive has picked a reviewer for you, use r? to override) |
r? @estebank I'm not sure if I should override who rustbot picked to review this, but I thought you might have a little more context. |
@bors try |
⌛ Trying commit aa875b67b4e6be07f55065129a3b4426d720de28 with merge 768af4cba83356e997782d009ecca0564739e9fc... |
Uh-oh! I will look into this! I must have missed something when compiling locally. |
This comment has been minimized.
This comment has been minimized.
💔 Test failed - checks-actions |
This comment has been minimized.
This comment has been minimized.
The fact that we have failures in our own codebase makes me think that this error is way too common for an outright error. We'll have to turn it into a warn by default lint very likely. Still interested in gauging what the real incidence is. |
@estebank Looking into this, it turns out I made a bad assumption in how I was checking for unused named arguments, and the error should not have been emitted! I'll let you know when I figure out how to do this correctly. Sorry for the delay! |
FWIW, this has been discussed by T-lang in the past and at the time there was positive sentiment towards a lint on this (#45256 (comment)). |
@Mark-Simulacrum thank you for sharing this! Would I be correct in thinking that this means a warning should be emitted instead of an error? |
I would expect so, it should be a lint though -- maybe even we can reuse something like unused variable for it, but @estebank can say more on the naming front. |
Oh, I see -- this should not be done in |
This comment has been minimized.
This comment has been minimized.
The new error shows that the "liveness" check needs to account for the formatting position (when used after the |
match a.format { | ||
FormatSpec { width: Count::CountIsName(s, _), .. } => { | ||
used_argument_names.insert(s); | ||
} | ||
_ => {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, it seems like you were doing that already? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out I missed the same case but for precision
!
It shouldn't need to be in clippy, I think this warrants a |
@estebank Thank you! I will take a look at |
src/test/ui/macros/issue98466.stderr
Outdated
error: named argument is not used by name | ||
--> $DIR/issue98466.rs:4:26 | ||
| | ||
LL | println!("_x is {}", _x = 5); | ||
| ^^ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally the end result would look something like this:
error: named string formatting argument is not used by name
--> $DIR/issue98466.rs:4:26
|
LL | println!("_x is {}", _x = 5);
| -- ^^ this named argument is only referred to by position in the formatting string
| |
| this formatting argument uses named argument `_x` by position
|
= note: lint `#[deny(named_formatting_arg_by_pos)]` enabled by default
help: use the named argument by name to avoid ambiguity
|
LL | println!("_x is {_x}", _x = 5);
| ++
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems reasonable to me! Still working on getting the lint to actually be emitted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I ended up with looks a little different -- since I used span_suggestion
the help part got rolled in with the suggestion. Should I be using a different method?
Also, the lint is currently Warn
instead of Deny
, since it seems like it's been valid to use named arguments positionally for some years. Would Deny
be better?
This comment has been minimized.
This comment has been minimized.
f20ee9f
to
abf6741
Compare
@estebank Thank you for the review and suggestions! I believe I have addressed them all and squashed (after running |
.iter() | ||
.map(|piece| { | ||
let mut piece = piece.clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can also be written as
.iter() | |
.map(|piece| { | |
let mut piece = piece.clone(); | |
.iter() | |
.cloned() | |
.map(|mut piece| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@estebank Thanks for the suggestion! Did you want me to fix this? (I wasn't sure since it was in the queue.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need right now if you can't get to it, but keep it in mind for the future. If you do fix it, I can reapprove :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I force pushed the fix -- I hope that was what was expected!
@bors r+ |
📌 Commit abf6741408fb31701168c6150c077e98cdc82b4b has been approved by It is now in the queue for this repository. |
@estebank I hope I didn’t break anything by doing this, but I updated the name of the PR since it’s a warning instead of an error now. I also pushed the change to use cloned. Do I need another approval from you or do I just wait for bors now? |
☔ The latest upstream changes (presumably #99203) made this pull request unmergeable. Please resolve the merge conflicts. |
Oh I forgot the no merging rule. I'll rebase and force push. |
Addresses Issue 98466 by emitting a warning if a named argument is used like a position argument (i.e. the name is not used in the string to be formatted). Fixes rust-lang#98466
I think this is good now! |
@bors r+ |
Rollup of 6 pull requests Successful merges: - rust-lang#98072 (Add provider API to error trait) - rust-lang#98580 (Emit warning when named arguments are used positionally in format) - rust-lang#99000 (Move abstract const to middle) - rust-lang#99192 (Fix spans for asm diagnostics) - rust-lang#99222 (Better error message for generic_const_exprs inference failure) - rust-lang#99236 (solaris: unbreak build on native platform) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Addresses Issue 98466 by emitting an error if a named argument
is used like a position argument (i.e. the name is not used in
the string to be formatted).
Fixes #98466