-
Notifications
You must be signed in to change notification settings - Fork 470
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
Conversation
Codecov Report
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 || |
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.
I don't know what accurate we want to handle this case, but the following is valid:
private const BindingFlags BindingFlags = (BindingFlags)5;
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.
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
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.
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.
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.
Done. I opted for the more extensive check.
Currently the analyzer suggests to change a
static readonly
field declaration to aconst
if possible. This results in a compilation error if the field has the same name as the type.Fixes #6681.