Skip to content

Update CA1812 to not report on vbnet static-like holder type #3882

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 2 commits into from
Aug 10, 2020

Conversation

Evangelink
Copy link
Member

Fix #1878

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@Evangelink Evangelink requested review from sharwell and mavasani July 20, 2020 10:43
@Evangelink Evangelink requested a review from a team as a code owner July 20, 2020 10:43
@@ -73,13 +73,18 @@ public override void Initialize(AnalysisContext context)
private static void AnalyzeSymbol(SymbolAnalysisContext context)
{
var symbol = (INamedTypeSymbol)context.Symbol;

if (!symbol.IsStatic &&
Copy link
Member Author

Choose a reason for hiding this comment

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

I have reordered the checks to postpone more expensive checks to later in the code. I am not sure if IsStaticHolderType is more expensive than MatchesConfiguredVisibility or not so feel free to tell me if I shall swap them.

if (!symbol.IsStatic &&
symbol.MatchesConfiguredVisibility(context.Options, Rule, context.Compilation, context.CancellationToken) &&
!symbol.IsAbstract &&
!IsSealedAndVisualBasic(symbol) &&
Copy link
Member Author

Choose a reason for hiding this comment

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

In vb.net the rule recommends to mark the class as "sealed" (NotInheritable) because static doesn't exist, so if the class is already marked as such, bail-out before doing more complex process. Note that the change is required because before the IsStaticHolderType was returning false for a vbnet static-like type holder causing CA1812 to report FPs.

Public Shared Event ThresholdReached As EventHandler
End Class

Friend Class C4
Copy link
Member Author

Choose a reason for hiding this comment

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

The static-like type holder considers this type as correct (although it will report CA1052 to suggest adding NotInheritable) so we don't report CA1812.


Friend NotInheritable Class C5

Public Sub New()
Copy link
Member Author

Choose a reason for hiding this comment

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

The helper is not considering the default constructor visibility and there was no note on if there was a technical motivation to do so, hence I have decided not to change the behavior. We could revisit if we decide we want to have CA1812 on this type.

Copy link
Member Author

Choose a reason for hiding this comment

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

@sharwell You wrote the helper, was there any technical motivation?

@Evangelink Evangelink closed this Jul 21, 2020
@Evangelink Evangelink reopened this Jul 21, 2020
@Evangelink
Copy link
Member Author

Ping @mavasani

// Sealed objects are presumed to be non-static holder types for C#.
// In VB.NET the type cannot be static and guidelines favor having a sealed (NotInheritable) type
// to act as static holder type.
if (symbol.IsSealed && symbol.Language == LanguageNames.CSharp)
Copy link
Contributor

@mavasani mavasani Aug 6, 2020

Choose a reason for hiding this comment

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

Do you need to do changes to both this extension method and the analyzer? Can we not just do one of them?

Copy link
Member Author

Choose a reason for hiding this comment

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

I need both of them because the helper is a little misleading, it's actually IsStaticHolderLikeType (or something like this). The helper checks that the member looks like a static holder but disregard the fact it is static to allow for the rule to report.

So in the end, the change here is just to prevent judging the type as not static-holder like when it's sealed. And the change in StaticHolderTypes.cs is made to ensure that if the class is sealed and it's vbnet we don't suggest to mark the class as static.

@codecov
Copy link

codecov bot commented Aug 7, 2020

Codecov Report

Merging #3882 into master will decrease coverage by 0.00%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #3882      +/-   ##
==========================================
- Coverage   95.63%   95.63%   -0.01%     
==========================================
  Files        1155     1155              
  Lines      254876   254953      +77     
  Branches    15269    15272       +3     
==========================================
+ Hits       243744   243813      +69     
  Misses       9189     9189              
- Partials     1943     1951       +8     

@Evangelink Evangelink requested a review from mavasani August 7, 2020 13:53
@mavasani mavasani merged commit 0a95f9e into dotnet:master Aug 10, 2020
@Evangelink Evangelink deleted the CA1812-vbnet branch August 10, 2020 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants