rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

feature: add the "Recursively add derive" assist

Open IvarWithoutBones opened this issue 1 year ago • 6 comments

This introduces a new assist that adds the derive attributes from a struct or enum to the types of its fields, recursively: Peek 2024-09-16 00-24

The derive attributes will be copied over if the field type meets the following criteria:

  • It resides within the same crate as the "top-level" type, to avoid editing the crate registry.
  • It does not already have the same derive attribute.
  • It does not have a manual implementation of the trait likely implemented by the derive.

The last criteria is a bit of a guess since it isn't possible to know what trait implementation(s) a derive macro generates. If a trait has the same name as the derive macro and the top-level type (from which we're copying the derive attribute) implements it, it'll be used to check for manual impls. This seems to work well in practice :)

Fixes #12330

IvarWithoutBones avatar Sep 15 '24 22:09 IvarWithoutBones

Thanks for the reviews, I've fixed most comments. Supporting non-ADTs and using SyntaxEditor will take a bit more investigation so I'll push that once it's ready.

IvarWithoutBones avatar Sep 17 '24 12:09 IvarWithoutBones

@bors delegate=ChayimFriedman2

Veykril avatar Sep 24 '24 10:09 Veykril

:v: @ChayimFriedman2, you can now approve this pull request!

If @Veykril told you to "r=me" after making some further change, please make that change, then do @bors r=@Veykril

bors avatar Sep 24 '24 10:09 bors

:umbrella: The latest upstream changes (presumably #18415) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Oct 26 '24 06:10 bors

Hello @IvarWithoutBones, would you happen to have time to finish this PR? We're planning to deprecate some of the SyntaxEditor APIs soon, so it would be ideal if this could be merged before that. If you're busy, would it be okay if I added a few more commits to your current work and completed the PR myself?

ShoyuVanilla avatar Jun 27 '25 07:06 ShoyuVanilla

Hi @IvarWithoutBones, do you still want to pursue this PR?

If not, we'll merge the derivative work of @joriskleiber (https://github.com/rust-lang/rust-analyzer/pull/20474).

ChayimFriedman2 avatar Aug 17 '25 02:08 ChayimFriedman2