-
Notifications
You must be signed in to change notification settings - Fork 899
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
Deduplicate imports #5410
Comments
Thanks for reaching out. If you're able to use a nightly version of rustfmt you can set the imports_granularity option to anything other than the default of Let me know if that helps. @calebcartwright when set to We recently merged #5380, which focused on deduplicating imports for the Linking the tracking issue #4991 |
looking into this a little bit more, I don't think we can take the exact same approach as #5380, however I think a good place to perform the deduplication would be Lines 213 to 223 in c4416f2
|
Although I have a proof of concept for how we could go from rustfmt/tests/target/multiple.rs Lines 19 to 23 in c4416f2
Because of that I'm not sure if we should try to change this for the |
What is the rationale behind keeping duplicates? (as it's not valid rust code it seems?) |
Great question. Just want to say up front that I'm just enumerating some thoughts on why we might consider leaving this alone. I'm not trying to suggest that this is a problem that shouldn't be solved. From the standpoint of the rustfmt project we need to ensure that we don't introduce breaking formatting changes. Given that this is the default behavior that's been around since the start of the project this would be a breaking change. That being said, we have options to introduce breaking changes. We can version gate them using the version config. We can also add new unstable options or variants to existing options. Recently there's been more discussion about stabilizing the imports_granularity config, so I don't think adding a new unstable option is possible at the moment #4991 (comment) From the perspective of the implementation, we'd need to weigh the added complexity of the fix on the codebase against other options the developer has to fix the problem. In this case one could use a different Lastly, the case you posed is simple enough, but the problem of removing duplicate imports gets complicated quickly. Are we only going to consider the simple case
|
Thanks a lot for the insights 🙏 From afar apart the comment one which seems a bit unavoidable and pathological, the others can be deduplicated with the normalization/flattening part I think I saw in the code? and then recompose them with the proper import granularity |
Thank you for sharing @IceTDrinker and @ytmimi for providing context and details. There's been some prior discussions on this topic which I can't find at the moment so will attempt to recap. The historical, and thus the continued, behavior of rustfmt was to only attempt to deduplicate with the user gave rustfmt the permission to merge/modify imports, originally via the As such my inclination is that we should close this. The only thing we could do would be to introduce yet another imports related config option that allowed for dedupes but banned any form of granularity normalization, and while I acknowledge there's probably a non-zero set of users that would want that, I don't think there's anywhere near enough volume to justify the added complexity of the config surface/mental model around import formatting and associated code maintenance. |
At the moment I agree with leaving |
Hello!
I searched the issues and could not find a similar issue, if there is already one on that topic I missed feel free to close this one.
I find myself solving some merge conflicts lately that happen for use statements, sometimes ending up with more than one import of the same name e.g.:
And running rustfmt on that does not deduplicate the import.
In practice the use statements causing issues are much bigger and it can be a bit of pain to deduplicate imports manually. In addition, the duplicate import prevents compilation because of the name Vec being redefined
error[E0252]: the name
Vec
is defined multiple timesRust playground:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0cd8ef07ca6cde1a2892c9b8e1697ff4
It feels like it should be possible as I've already seen rustfmt merging multiple imports from the same module into one use statement, so there is a mechanism to group names together and sort them, I'm guessing that switching the underlying container for the grouped names to some Set-like object should do the trick.
I haven't had time to check the source for that particular case, but I thought I might as well open the issue here if someone knew where the code for this is and how it could be changed to manage that case 🙂
Cheers!
The text was updated successfully, but these errors were encountered: