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

Emit warning when named arguments are used positionally in format #98580

Merged
merged 1 commit into from
Jul 14, 2022

Conversation

PrestonFrom
Copy link
Contributor

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

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 27, 2022
@rust-highfive
Copy link
Collaborator

r? @cjgillot

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 27, 2022
@PrestonFrom
Copy link
Contributor Author

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.

@rust-highfive rust-highfive assigned estebank and unassigned cjgillot Jun 27, 2022
@estebank
Copy link
Contributor

@bors try

@bors
Copy link
Contributor

bors commented Jun 27, 2022

⌛ Trying commit aa875b67b4e6be07f55065129a3b4426d720de28 with merge 768af4cba83356e997782d009ecca0564739e9fc...

@PrestonFrom
Copy link
Contributor Author

Uh-oh! I will look into this! I must have missed something when compiling locally.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Jun 27, 2022

💔 Test failed - checks-actions

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 27, 2022
@rust-log-analyzer

This comment has been minimized.

@estebank
Copy link
Contributor

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.

@PrestonFrom
Copy link
Contributor Author

@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!

@Mark-Simulacrum
Copy link
Member

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)).

@PrestonFrom
Copy link
Contributor Author

@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?

@Mark-Simulacrum
Copy link
Member

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.

@PrestonFrom
Copy link
Contributor Author

Oh, I see -- this should not be done in format.rs at all, but in rustc_lint then. (Or should it be in clippy?)

@rust-log-analyzer

This comment has been minimized.

@estebank
Copy link
Contributor

The new error shows that the "liveness" check needs to account for the formatting position (when used after the :, instead of before it).

Comment on lines 986 to 993
match a.format {
FormatSpec { width: Count::CountIsName(s, _), .. } => {
used_argument_names.insert(s);
}
_ => {}
Copy link
Contributor

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? 🤔

Copy link
Contributor Author

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!

@estebank
Copy link
Contributor

Oh, I see -- this should not be done in format.rs at all, but in rustc_lint then. (Or should it be in clippy?)

It shouldn't need to be in clippy, I think this warrants a rustc lint. To add a lint you have to declare it and you can (and will have to in this case) create it from a place other than rustc_lint. I think you can take a look at lint_unicode_text_flow for one example of a "non-standard" builtin lint that requires state to be kept. You would have to add a variant to BuiltinLintDiagnostics, and that would keep around enough info (spans and symbols) to be able to provide an appropriate suggestion (likely naming it in the format string would suffice).

@PrestonFrom
Copy link
Contributor Author

@estebank Thank you! I will take a look at lint_unicode_text_flow!

Comment on lines 1 to 5
error: named argument is not used by name
--> $DIR/issue98466.rs:4:26
|
LL | println!("_x is {}", _x = 5);
| ^^
Copy link
Contributor

@estebank estebank Jul 1, 2022

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);
   |                      ++

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

@rust-log-analyzer

This comment has been minimized.

@PrestonFrom PrestonFrom force-pushed the issue_98466 branch 2 times, most recently from f20ee9f to abf6741 Compare July 8, 2022 04:39
@PrestonFrom
Copy link
Contributor Author

@estebank Thank you for the review and suggestions! I believe I have addressed them all and squashed (after running x.py fmt).

@PrestonFrom PrestonFrom requested a review from estebank July 8, 2022 04:43
Comment on lines 1159 to 1161
.iter()
.map(|piece| {
let mut piece = piece.clone();
Copy link
Contributor

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

Suggested change
.iter()
.map(|piece| {
let mut piece = piece.clone();
.iter()
.cloned()
.map(|mut piece| {

Copy link
Contributor Author

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.)

Copy link
Contributor

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 :)

Copy link
Contributor Author

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!

@estebank
Copy link
Contributor

estebank commented Jul 8, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Jul 8, 2022

📌 Commit abf6741408fb31701168c6150c077e98cdc82b4b has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 8, 2022
@PrestonFrom PrestonFrom changed the title Emit error when named arguments are used positionally in format Emit warning when named arguments are used positionally in format Jul 11, 2022
@PrestonFrom
Copy link
Contributor Author

@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?

@bors
Copy link
Contributor

bors commented Jul 13, 2022

☔ The latest upstream changes (presumably #99203) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 13, 2022
@PrestonFrom
Copy link
Contributor Author

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
@PrestonFrom
Copy link
Contributor Author

I think this is good now!

@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Jul 13, 2022

📌 Commit 1219f72 has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 13, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 14, 2022
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
@bors bors merged commit 8c5c983 into rust-lang:master Jul 14, 2022
@rustbot rustbot added this to the 1.64.0 milestone Jul 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Built in format macro should emit a warning when named arguments are used positionally
8 participants