Skip to content
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

Don't warn CA1802 when type's name matches enum's name #6682

Merged
merged 7 commits into from
Jun 30, 2023

Conversation

CollinAlpert
Copy link
Contributor

Currently the analyzer suggests to change a static readonly field declaration to a const if possible. This results in a compilation error if the field has the same name as the type.

Fixes #6681.

@CollinAlpert CollinAlpert requested a review from a team as a code owner June 9, 2023 15:06
@codecov
Copy link

codecov bot commented Jun 9, 2023

Codecov Report

Merging #6682 (4d4f961) into main (c6b6e10) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 4d4f961 differs from pull request most recent head 85c163c. Consider uploading reports for the commit 85c163c to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6682   +/-   ##
=======================================
  Coverage   96.36%   96.36%           
=======================================
  Files        1395     1395           
  Lines      324940   325005   +65     
  Branches    10665    10666    +1     
=======================================
+ Hits       313135   313202   +67     
+ Misses       9241     9238    -3     
- Partials     2564     2565    +1     

@@ -70,6 +70,7 @@ public override void Initialize(AnalysisContext context)
lastField == null ||
lastField.IsConst ||
!lastField.IsReadOnly ||
lastField.Type.Name == lastField.Name ||
Copy link
Member

Choose a reason for hiding this comment

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

I don't know what accurate we want to handle this case, but the following is valid:

private const BindingFlags BindingFlags = (BindingFlags)5;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, true. What do you think about extending the condition to the following:

lastField.Type.TypeKind == TypeKind.Enum && lastField.Type.Name == lastField.Name && fieldInitializerValue is IBinaryOperation or IFieldReferenceOperation

Copy link
Member

@mavasani mavasani Jun 19, 2023

Choose a reason for hiding this comment

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

private const BindingFlags BindingFlags = (BindingFlags)(BindingFlags.Public | BindingFlags.NonPublic)

This still produces the error, and the right side is an IConversionOperation. I am fine with the simple check you have in the PR. If you want to make it stricter, the correct check would be to verify that the IOperation tree rooted at fieldInitializer does not contain any IFieldReferenceOperation whose field symbol's containing type is same as lastField.Type. Either way works for me - please do add both the test cases mentioned in this comment thread to the new unit test added in the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. I opted for the more extensive check.

@mavasani mavasani enabled auto-merge June 30, 2023 06:47
@mavasani mavasani merged commit eae093b into dotnet:main Jun 30, 2023
10 of 11 checks passed
@CollinAlpert CollinAlpert deleted the issue_6681 branch June 30, 2023 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False Positive for CA1802 when using readonly with enum Flags
3 participants