-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add new lint [positional_named_format_parameters
]
#9040
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dswij (or someone else) soon. Please see the contribution instructions for more information. |
Thanks for the PR and welcome! Sorry, haven't had the time to take a look at this. I will get to it asap this week when I have the time |
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.
Thanks for the patience! The changes in the PR look great to me and is certainly a nice addition! Just some comments
Hmmm it looks like the exact same lint was added directly to rustc for release 1.64.0 (rust-lang/rust#98580). What were the chances of that? |
@miam-miam100 it might still be worth it to have a clippy lint against named params ever being referred to positionally, which would be noisier/stricter than what this PR and rustc do. That way rustc avoids false positives but there's still a mechanism to detect "iffy" cases. |
Hmmm so if I've understood correctly we should have a lint that prevents you from writing this? println!("hello {world} {}", world = "world"); |
Exactly. |
unused_named_parameter
]positional_named_format_parameters
]
I don't know what rustc version clippy is using but it looks like my two errors are due to these two PRs not being merged in yet: rust-lang/rust#99263 and rust-lang/rust#99480. @dswij Do you know what I can do? Do I just have to wait? |
I believe The job failed when trying to apply the fix for the lint 😕. |
The changes do not currently pass on my local machine as the toolchain version clippy uses does not include the changes I need for the lint to work correctly. However you are correct in saying that the rust fix file was incorrectly written and has now been fixed. |
That makes sense. We can wait for the weekly sync then. The next scheduled one is pretty soon (by the next 24 hours). We can come back and rebase the PR afterward. |
I have been hacking on a var inlining lint available since 1.58 - it is based on another possibly conflicting pr #9233 I could really use the code 1)to check if an argument is an alias to another argument or width or precision, 2) get the spans of all three possible usages so I can inline vars and 3) possibly share the same code between rustc and clippy (keep all buggy parsing code in one place 😀). I have written some test cases you might want to use, eg with some unexpected whitespaces. Awesome to see so much format-related work! |
That's awesome, that does not sound all too easy so I wish you the best of luck on that! I added the test cases to my code which look like they should all pass once we update the nightly version. |
Nice! Successfully rebased, @dswij feel free to review me whenever you are free. |
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.
@miam-miam100 I made a tiny suggestion, but the bigger question is how this PR should be merged with the @Alexendoo 's #8518 -- it seems both make a number of significant changes to the clippy_lints/src/write.rs
, nearly rewriting it. Would it make sense to perhaps base your PR on top of @Alexendoo 's, so that all of them use late context?
Hmmm I'm not sure that is a great idea since we don't know when that PR will land and this PR does not really benefit from the late pass in any way. |
@miam-miam100 my interest is getting the #9233 in, which I was basing on #8518. I am not yet certain I need to base my lint on #8518, or if I can do it all with the early context. Perhaps your PR could be refactored a bit so that we can have shared functions to parse params and get back the spans? My main requirement:
Do you think your functions could produce this result? If so, I could base my work on top of your PR that doesn't try to change the overall structure of the format code. Thx! |
Ah well fortunately for you, all those things are directly exposed by rustc. All the arg, width and precision spans are given, as you can see here (https://github.com/miam-miam100/rust-clippy/blob/631525a21fa3b4bd0c495da11c3b1c0eec84c5ff/clippy_lints/src/write.rs#L543). As for checking if the corresponding argument is a local var (checking if it is an ident) you can do that here https://github.com/miam-miam100/rust-clippy/blob/631525a21fa3b4bd0c495da11c3b1c0eec84c5ff/clippy_lints/src/write.rs#L667. From what you've told me you should just be able to check that there and get the whole lint working without any extra work. |
To explain the situation a bit, rust-clippy/clippy_lints/src/lib.rs Lines 419 to 428 in 53a09d4
The plan is to move away from the pre-expansion pass as it has a few issues, as an example for this lint: // lib.rs
#![allow(clippy::positional_named_format_parameters)]
mod foo; fn x() {
// this would still trigger the lint, ignoring the crate wide allow
println!("hello {world} {}", world = "world");
} Since it would be pretty unlikely someone will want to For #9233 I could see people wanting to |
@Alexendoo thank you for the detailed answer! What are you thoughts about integrating the new rustc format string parsing capabilities like |
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.
The lint itself looks great, just the lint description that can be improved, IMO.
Can you help squash the commits, and after that, I think it's good to merge.
@nyurik Yeah, just waiting for the next sync that includes rust-lang/rust#99987, I'll ping you in the PR once it's ready |
Right that's great thanks, I'm currently on holiday so I'll do that when I get back. (a week's time) |
@dswij All squashed! |
Huge thanks for this @miam-miam100! This is a nice lint to have for sure. @bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Refactor `FormatArgsExpn` It now for each format argument `{..}` has: - The `Expr` it points to, and how it does so (named/named inline/numbered/implicit) - The parsed `FormatSpec` (format trait/fill/align/etc., the precision/width and any value they point to) - Many spans The caller no longer needs to pair up arguments to their value, or separately interpret the `specs` `Expr`s when it isn't `None` The gist is that it combines the result of [`rustc_parse_format::Parser`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse_format/struct.Parser.html) with the macro expansion itself This unfortunately makes the code a bit longer, however we need to use both as neither have all the information we're after. `rustc_parse_format` doesn't have the information to resolve named arguments to their values. The macro expansion doesn't contain whether the positions are implicit/numbered/named, or the spans for format arguments Wanted by #9233 and #8518 to be able to port the changes from #9040 Also fixes #8643, previously the format args seem to have been paired up with the wrong values somehow changelog: [`format_in_format_args`]: Fix false positive due to misattributed arguments r? `@flip1995` cc `@nyurik`
Please write a short comment explaining your change (or "none" for internal only changes)
changelog: Add new lint [
positional_named_format_parameters
] to warn when named parameters in format strings are used as positional arguments.