-
Notifications
You must be signed in to change notification settings - Fork 222
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
NullPointerDereference.SyntaxKindWalker reduce allocations and evaluations in the hot path #8385
NullPointerDereference.SyntaxKindWalker reduce allocations and evaluations in the hot path #8385
Conversation
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, I left some polishing comments
|
||
public override void VisitMemberAccessExpression(MemberAccessExpressionSyntax node) | ||
{ | ||
if (node.Kind() is SyntaxKind.SimpleMemberAccessExpression) |
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's not really a pattern to match
if (node.Kind() is SyntaxKind.SimpleMemberAccessExpression) | |
if (node.Kind() == SyntaxKind.SimpleMemberAccessExpression) |
{ | ||
public bool Result { get; private set; } | ||
|
||
public override void Visit(SyntaxNode node) | ||
{ | ||
Result |= node.Kind() is SyntaxKindEx.ForEachVariableStatement; |
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's simple int comparison
Result |= node.Kind() is SyntaxKindEx.ForEachVariableStatement; | |
Result |= node.Kind() == SyntaxKindEx.ForEachVariableStatement; |
{ | ||
public bool Result { get; private set; } | ||
|
||
public override void Visit(SyntaxNode node) | ||
{ | ||
Result |= node.Kind() is SyntaxKindEx.ForEachVariableStatement; |
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.
That's so annoying that it's a different syntax kind :/ We're likely missing it all over the place
analyzers/src/SonarAnalyzer.VisualBasic/SymbolicExecution/Roslyn/NullPointerDereference.cs
Show resolved
Hide resolved
...onarAnalyzer.UnitTest/Rules/SymbolicExecution/NullPointerDereferenceSyntaxKindWalkerTests.cs
Outdated
Show resolved
Hide resolved
{ | ||
public unsafe void M(int i) | ||
{ | ||
int* iPtr = &i; |
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.
You can use int* iPtr
argument directly, to avoid this scaffolding.
...onarAnalyzer.UnitTest/Rules/SymbolicExecution/NullPointerDereferenceSyntaxKindWalkerTests.cs
Show resolved
Hide resolved
...onarAnalyzer.UnitTest/Rules/SymbolicExecution/NullPointerDereferenceSyntaxKindWalkerTests.cs
Outdated
Show resolved
Hide resolved
} | ||
|
||
[DataTestMethod] | ||
[DataRow("""Dim i As System.Int32""")] |
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.
This doesn't need to be a DataTestMethod
. Or you can add this, to make it similar to the C# version
[DataRow("""Dim S As String = (ToString()?.Length)?.ToString()""")]
public class NullPointerDereferenceSyntaxKindWalkerTests | ||
{ | ||
[DataTestMethod] | ||
[DataRow("""await Task.Yield();""")] |
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.
None of the DataRow
needs """
. Simple string is enough in all cases
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
See also #8103
This PR fixes the params array allocation caused by IsAnyKind and reactors the SyntaxWalker to do the SyntaxKind checks in the dedicated Visit... methods. That way the number of comparisons is reduced.