-
Notifications
You must be signed in to change notification settings - Fork 231
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
Improve Rule S2223: cleanup and performance #6761
Conversation
d39f5ee
to
73f5639
Compare
This reverts commit 016aebf.
1bffdec
to
f6cf3ad
Compare
I suggest to test the performance impact (before and after) - see this page and/or the msbuild To get the AnalyzerRunner, you need to build this project locally. |
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.
Minor comments.
The only main aspect is the private protected
visibility, which would possibly (but not necessarily) be a false negative, since this visibility is almost private
as private
itself.
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/StaticFieldVisible.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/StaticFieldVisible.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/StaticFieldVisible.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/StaticFieldVisible.cs
Outdated
Show resolved
Hide resolved
node.Modifiers.Count > 1 | ||
&& node.Modifiers.Any(SyntaxKind.StaticKeyword) | ||
&& (!node.Modifiers.Any(SyntaxKind.PrivateKeyword) || (node.Modifiers.Any(SyntaxKind.PrivateKeyword) && node.Modifiers.Any(SyntaxKind.ProtectedKeyword))) | ||
&& !node.Modifiers.Any(SyntaxKind.ReadOnlyKeyword); |
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.
A volatile
field should also be irrelevant as volatile
indicates some advanced multi-threading voodoo is going on here.
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.
maybe good to exclude - per MSDN docs
The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time.
On a multiprocessor system, a volatile read operation does not guarantee to obtain the latest value written to that memory location by any processor.
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.
Ok, while I see a public static
volatile non-readonly field as dangerous as a public static
non-volatile non-readonly one, and it seems that there are almost always ways to have the field private
(at least for the typical scenarios of sentinel loopers or counters), let's assume that the person using it has a sound multi-threading model and a solid understanding of it.
So I am OK with disabling the check when volatile
is encountered.
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.
@martin-strecker-sonarsource to resolve this one
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.
@martin-strecker-sonarsource and I had an offiline convertation on the matter and agreed to the current solution regarding volatile
modifier.
@antonioaversa I added also the volatile keyword as an exception as from the msdn docs my understanding is that when a field is declared volatile it is expected that the values might change in an unexpected way and might be an FP?
|
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.
LGTM! Just minor things, mostly aesthetics.
protected static double Pi4 = 3.14; // Noncompliant | ||
internal static double Pi5 = 3.14; // Noncompliant | ||
internal static double Pi6 = 3.14; // Noncompliant | ||
protected internal static double Pi7 = 3.14; // Noncompliant |
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.
It would be good to align vertically all these // Noncompliant
.
node.Modifiers.Count > 1 | ||
&& node.Modifiers.Any(SyntaxKind.StaticKeyword) | ||
&& (!node.Modifiers.Any(SyntaxKind.PrivateKeyword) || (node.Modifiers.Any(SyntaxKind.PrivateKeyword) && node.Modifiers.Any(SyntaxKind.ProtectedKeyword))) | ||
&& !node.Modifiers.Any(SyntaxKind.ReadOnlyKeyword); |
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.
Ok, while I see a public static
volatile non-readonly field as dangerous as a public static
non-volatile non-readonly one, and it seems that there are almost always ways to have the field private
(at least for the typical scenarios of sentinel loopers or counters), let's assume that the person using it has a sound multi-threading model and a solid understanding of it.
So I am OK with disabling the check when volatile
is encountered.
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
RSPEC PR: SonarSource/rspec#1583
Performance comparison:
pascalabcnet
andservuo
were the two projects with most issues for rule S2223 on peach.