-
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
Fix S2234 FN nullable argument value #3880
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.
Hi @H4pH, thank you for the contribution. I left some comments to address.
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/ParametersCorrectOrder.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/ParametersCorrectOrder.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/ParametersCorrectOrder.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/ParametersCorrectOrder.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/ParametersCorrectOrder.cs
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/ParametersCorrectOrder.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/ParametersCorrectOrder.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/ParametersCorrectOrder.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/ParametersCorrectOrder.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/ParametersCorrectOrder.cs
Outdated
Show resolved
Hide resolved
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.
Final polishing
analyzers/src/SonarAnalyzer.VisualBasic/Rules/ParametersCorrectOrder.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/ParametersCorrectOrder.cs
Outdated
Show resolved
Hide resolved
/azp run Sonar.Net.External |
Azure Pipelines successfully started running 1 pipeline(s). |
Hi @H4pH, CI run failed on .NET Integration tests and that's actually a good thing - new issue was found in
|
Hi @pavel-mikula-sonarsource, I tried running your script but it fails when processing the results:
|
@H4pH, Ah OK, please address the comments and I'll run it afterwards and provide a commit to deal with it. |
Hi @H4pH, will you have time to finish this PR? It was close to being merged. |
Hi, @pavel-mikula-sonarsource, |
Hi @H4pH, thanks for taking a look. I'll try to check it next week. Can you please rebase your PR meanwhile? There's a merge conflict. |
i've done it, but now github shows all commits from master as PR commits :( |
Something went wrong, it looks that you've merged
In local commit history, you should see your PR commits on top of |
ed21c18
to
4bd6a1f
Compare
Hi @pavel-mikula-sonarsource, congratulations on the first year of my pull request :) |
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.
Hi @H4pH, I'll try to prioritize this PR now.
bool IdentifierWithSameNameAndTypeExistsLater(ArgumentIdentifier argumentIdentifier, int index) => | ||
argumentIdentifiers.Skip(index + 1) | ||
.Any(ia => string.Equals(ia.IdentifierName, argumentIdentifier.IdentifierName, StringComparison.OrdinalIgnoreCase) | ||
&& ArgumentTypesAreSame(ia.ArgumentSyntax, argumentIdentifier.ArgumentSyntax)); |
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.
What was the motivation for having this method? AFAICT it prevents the issue from being raised twice when arguments are swapped. For sake of simplicity, I'd say it's fine to raise twice and we can remove this check.
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.
Without this method there is different result for same test. Example is passing one var to both params, see TestCases\ParametersCorrectOrder.cs line 97 and 101. (pass "a" and "b" to both params of m(int a, int b)
)
I think, that if argument was passed at correct place once, we shouldn't rise
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, that makes sense. And at the same time, if b
was passed to both, we would have an equal scenario and we would raise and we shouldn't, right?
So we need to know if the argument was passed on the correct location at least once (no matter if before or after). This sounds complicated to do in a clean way to me. To keep this PR focused, I would be fine with creating a new issue for that case, fix it in another PR and here just document those two cases m(a, a)
and m(b, b)
as ' Noncompliant FP
with a link to that new issue.
private static SyntaxToken? GetValueAccessIdentifier(MemberAccessExpressionSyntax expression, SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) => | ||
expression.Name.ToString() == "Value" && IsNullableValueAccess(expression, syntaxNodeAnalysisContext) | ||
? GetExpressionSyntaxIdentifier(expression.Expression, syntaxNodeAnalysisContext) | ||
: expression.Name.Identifier; | ||
|
||
private static bool IsNullableValueAccess(MemberAccessExpressionSyntax expression, SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) | ||
{ | ||
var typeInfo = syntaxNodeAnalysisContext.SemanticModel.GetTypeInfo(expression.Expression); | ||
var type = typeInfo.ConvertedType; | ||
var nullableT = syntaxNodeAnalysisContext.Compilation.GetTypeByMetadataName(typeof(Nullable<>).FullName); | ||
return type.OriginalDefinition.DerivesOrImplements(nullableT); | ||
} |
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 can be simplified a lot using the KnownType
.
You actually don't need SyntaxNodeAnalysisContext
and using SemanticModel model
everywhere is enough.
private static SyntaxToken? GetValueAccessIdentifier(MemberAccessExpressionSyntax expression, SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) => | |
expression.Name.ToString() == "Value" && IsNullableValueAccess(expression, syntaxNodeAnalysisContext) | |
? GetExpressionSyntaxIdentifier(expression.Expression, syntaxNodeAnalysisContext) | |
: expression.Name.Identifier; | |
private static bool IsNullableValueAccess(MemberAccessExpressionSyntax expression, SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) | |
{ | |
var typeInfo = syntaxNodeAnalysisContext.SemanticModel.GetTypeInfo(expression.Expression); | |
var type = typeInfo.ConvertedType; | |
var nullableT = syntaxNodeAnalysisContext.Compilation.GetTypeByMetadataName(typeof(Nullable<>).FullName); | |
return type.OriginalDefinition.DerivesOrImplements(nullableT); | |
} | |
private static SyntaxToken? GetValueAccessIdentifier(MemberAccessExpressionSyntax expression, SemanticModel model) => | |
expression.Name.ToString() == "Value" && model.GetTypeInfo(expression.Expression).ConvertedType.DerivesOrImplements(KnownType.System_Nullable_T) | |
? GetExpressionSyntaxIdentifier(expression.Expression, model) | |
: expression.Name.Identifier; |
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.
Same for VB
/azp run Sonar.Net |
Azure Pipelines successfully started running 1 pipeline(s). |
Kudos, SonarCloud Quality Gate passed! |
SonarCloud Quality Gate failed. |
IT results have changed, please rerun the PowerShell scripts again to update the JSON as described above after your changes. |
@@ -87,10 +88,32 @@ protected override void Initialize(SonarAnalysisContext context) | |||
protected override Location GetMethodDeclarationIdentifierLocation(SyntaxNode syntaxNode) => | |||
(syntaxNode as BaseMethodDeclarationSyntax)?.FindIdentifierLocation(); | |||
|
|||
protected override SyntaxToken? GetArgumentIdentifier(ArgumentSyntax argument) => | |||
(argument.Expression as IdentifierNameSyntax)?.Identifier; | |||
protected override SyntaxToken? GetArgumentIdentifier(ArgumentSyntax argument, SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) => GetExpressionSyntaxIdentifier(argument?.Expression, syntaxNodeAnalysisContext); |
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.
The body should be on the next line, it will also fix the issue about too long line
protected override SyntaxToken? GetArgumentIdentifier(ArgumentSyntax argument, SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) => GetExpressionSyntaxIdentifier(argument?.Expression, syntaxNodeAnalysisContext); | |
protected override SyntaxToken? GetArgumentIdentifier(ArgumentSyntax argument, SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) => | |
GetExpressionSyntaxIdentifier(argument?.Expression, syntaxNodeAnalysisContext); |
- VB.NET implementation - more cases and tests
- added better nullable recognising - added better same arguments check
4bd6a1f
to
7a9eb69
Compare
This Pull Request, along with the subsequent one #6748, has been decided to be closed as we are preparing to merge PR #8238. |
Fixes #3879