-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Support generating explicit Equals/GetHashCode for records #81124
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1166,8 +1166,8 @@ public override bool Equals(object obj) | |
| } | ||
| } | ||
| """, | ||
| CodeActionEquivalenceKey = FeaturesResources.Generate_Equals_object, | ||
| CodeActionVerifier = (codeAction, verifier) => verifier.Equal(FeaturesResources.Generate_Equals_object, codeAction.Title), | ||
| CodeActionEquivalenceKey = FeaturesResources.Generate_Equals, | ||
| CodeActionVerifier = (codeAction, verifier) => verifier.Equal(FeaturesResources.Generate_Equals, codeAction.Title), | ||
| }.RunAsync(); | ||
|
|
||
| [Fact] | ||
|
|
@@ -3891,4 +3891,271 @@ class C(int a) | |
| """, | ||
| LanguageVersion = LanguageVersion.CSharp12, | ||
| }.RunAsync(); | ||
|
|
||
| [Fact] | ||
| public Task TestRecord_Equals1() | ||
| => new VerifyCS.Test | ||
| { | ||
| TestCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| record Program | ||
| { | ||
| [|int a;|] | ||
| } | ||
| """, | ||
| FixedCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| record Program | ||
| { | ||
| int a; | ||
|
|
||
| public virtual bool Equals(Program program) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question - should this be generated as
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup :) |
||
| { | ||
| return program is not null && | ||
| a == program.a; | ||
| } | ||
| } | ||
| """, | ||
| LanguageVersion = LanguageVersion.Preview, | ||
| }.RunAsync(); | ||
|
|
||
| [Fact] | ||
| public Task TestRecord_EqualsAndGetHashCode1() | ||
| => new VerifyCS.Test | ||
| { | ||
| TestCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| record Program | ||
| { | ||
| [|int a;|] | ||
| } | ||
| """, | ||
| FixedCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| record Program | ||
| { | ||
| int a; | ||
|
|
||
| public virtual bool Equals(Program program) | ||
| { | ||
| return program is not null && | ||
| a == program.a; | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| return -1757793268 + a.GetHashCode(); | ||
| } | ||
| } | ||
| """, | ||
| LanguageVersion = LanguageVersion.Preview, | ||
| CodeActionIndex = 1, | ||
| }.RunAsync(); | ||
|
|
||
| [Fact] | ||
| public Task TestSealedRecord_Equals1() | ||
| => new VerifyCS.Test | ||
| { | ||
| TestCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| sealed record Program | ||
| { | ||
| [|int a;|] | ||
| } | ||
| """, | ||
| FixedCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| sealed record Program | ||
| { | ||
| int a; | ||
|
|
||
| public bool Equals(Program program) | ||
| { | ||
| return program is not null && | ||
| a == program.a; | ||
| } | ||
| } | ||
| """, | ||
| LanguageVersion = LanguageVersion.Preview, | ||
| }.RunAsync(); | ||
|
|
||
| [Fact] | ||
| public Task TestSealedRecord_EqualsAndGetHashCode1() | ||
| => new VerifyCS.Test | ||
| { | ||
| TestCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| sealed record Program | ||
| { | ||
| [|int a;|] | ||
| } | ||
| """, | ||
| FixedCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| sealed record Program | ||
| { | ||
| int a; | ||
|
|
||
| public bool Equals(Program program) | ||
| { | ||
| return program is not null && | ||
| a == program.a; | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| return -1757793268 + a.GetHashCode(); | ||
| } | ||
| } | ||
| """, | ||
| LanguageVersion = LanguageVersion.Preview, | ||
| CodeActionIndex = 1, | ||
| }.RunAsync(); | ||
|
|
||
| [Fact] | ||
| public Task TestRecordStruct_Equals1() | ||
| => new VerifyCS.Test | ||
| { | ||
| TestCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| record struct Program | ||
| { | ||
| [|int a;|] | ||
| } | ||
| """, | ||
| FixedCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| record struct Program | ||
| { | ||
| int a; | ||
|
|
||
| public bool Equals(Program program) | ||
| { | ||
| return a == program.a; | ||
| } | ||
| } | ||
| """, | ||
| LanguageVersion = LanguageVersion.Preview, | ||
| }.RunAsync(); | ||
|
|
||
| [Fact] | ||
| public Task TestRecordStruct_EqualsAndGetHashCode1() | ||
| => new VerifyCS.Test | ||
| { | ||
| TestCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| record struct Program | ||
| { | ||
| [|int a;|] | ||
| } | ||
| """, | ||
| FixedCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| record struct Program | ||
| { | ||
| int a; | ||
|
|
||
| public bool Equals(Program program) | ||
| { | ||
| return a == program.a; | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| return -1757793268 + a.GetHashCode(); | ||
| } | ||
| } | ||
| """, | ||
| LanguageVersion = LanguageVersion.Preview, | ||
| CodeActionIndex = 1, | ||
| }.RunAsync(); | ||
|
|
||
| [Fact] | ||
| public Task TestDerived_Equals1() | ||
| => new VerifyCS.Test | ||
| { | ||
| TestCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| record Base { } | ||
|
|
||
| record Program : Base | ||
| { | ||
| [|int a;|] | ||
| } | ||
| """, | ||
| FixedCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| record Base { } | ||
|
|
||
| record Program : Base | ||
| { | ||
| int a; | ||
|
|
||
| public virtual bool Equals(Program program) | ||
| { | ||
| return program is not null && | ||
| base.Equals(program) && | ||
| a == program.a; | ||
| } | ||
| } | ||
| """, | ||
| LanguageVersion = LanguageVersion.Preview, | ||
| }.RunAsync(); | ||
|
|
||
| [Fact] | ||
| public Task TestDerivedRecord_EqualsAndGetHashCode1() | ||
| => new VerifyCS.Test | ||
| { | ||
| TestCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| record Base { } | ||
|
|
||
| record Program : Base | ||
| { | ||
| [|int a;|] | ||
| } | ||
| """, | ||
| FixedCode = """ | ||
| using System.Collections.Generic; | ||
|
|
||
| record Base { } | ||
|
|
||
| record Program : Base | ||
| { | ||
| int a; | ||
|
|
||
| public virtual bool Equals(Program program) | ||
| { | ||
| return program is not null && | ||
| base.Equals(program) && | ||
| a == program.a; | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| int hashCode = 155057090; | ||
| hashCode = hashCode * -1521134295 + base.GetHashCode(); | ||
| hashCode = hashCode * -1521134295 + a.GetHashCode(); | ||
| return hashCode; | ||
| } | ||
| } | ||
| """, | ||
| LanguageVersion = LanguageVersion.Preview, | ||
| CodeActionIndex = 1, | ||
| }.RunAsync(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
View with whitespace off.