Skip to content

Conversation

@repnop
Copy link
Contributor

@repnop repnop commented Oct 16, 2020

Fixes #6241.

@repnop repnop force-pushed the enum-struct-extract-check-differing-name branch from 6054b25 to d6378ed Compare October 16, 2020 20:12
Comment on lines 40 to 51
// Grab the tuple field inner type as a string (if it exists and is alone)
// to check against structs that may have a differing name from the enum
// variant itself
let variant_inner = {
let mut fields = field_list.fields();
let ty = fields.next().and_then(|field| field.ty()).map(|ty| ty.to_string());

match fields.next() {
Some(_) => None,
None => ty,
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, why do we need this check? I'd expect something much simpler, like "if number of fields == 1, do nothing"

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'm not sure if I follow -- I was under the assumption that the fix for the issue would be to check that, for single-element tuple variants, there needs to be an additional check that no types in the parent scope that also have that name, which avoids the false positive shown in the issue. The assist would be valid for extracting out >1 element tuples regardless, therefore it checks to see if there is more than one element in the tuple.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, for

enum NameClass {
  ConstReference(Definition)
}

The assist should not be applicable regardnless of whether Definition exists in the scope or not. This can be implemented as a purely syntactic check, I don't think we need to look at tys here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, so it's simply not applying the assist to 1-tuple variants. I'll go ahead and rewrite the PR to do that instead :) thanks for the clarification

@repnop repnop force-pushed the enum-struct-extract-check-differing-name branch from d6378ed to b9790a2 Compare October 26, 2020 21:38
@repnop repnop changed the title Don't suggest extracting out inner types as structs when they already exist Don't suggest extracting out 1-tuple enum variants Oct 26, 2020
@matklad
Copy link
Contributor

matklad commented Oct 26, 2020

bors r+

Thanks!

@bors
Copy link
Contributor

bors bot commented Oct 26, 2020

@bors bors bot merged commit 7f346f9 into rust-lang:master Oct 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

extract struct from enum variant false positive

4 participants