Skip to content
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 S2259 FP: ValidatedNotNullAttribute in extension method #7049

Merged
merged 8 commits into from
Apr 13, 2023

Conversation

pavel-mikula-sonarsource
Copy link
Contributor

Fixes #7048

This will also fix the same problem on S3900

Copy link
Contributor

Choose a reason for hiding this comment

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

HasNotNullAttribute and IsNotNullAttribute should not be public in the Argument processor.

Comment on lines 57 to 58
public static bool IsNotNullAttribute(AttributeData attribute) =>
attribute.HasAnyName("ValidatedNotNullAttribute", "NotNullAttribute");

Choose a reason for hiding this comment

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

Why are these public now? HasNotNullAttribute and IsNotNullAttribute should be made extension methods if we want to reuse them. We will also likely add support for more of these attributes in the future. Can we add these to KnownTypes and add an overload to AttributeSyntaxExtensions.IsKnownType instead? (This only works if we can ignore the namespace, though).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The IsNotNullAttribute as not intentional. I'll make the other one extension.

if (invocation.TargetMethod.IsStatic)
if (invocation.TargetMethod.IsExtensionMethod
&& invocation.TargetMethod.ReducedFrom is { } reducedFrom // VB reduces method symbol to 'instance.Extension()' without annotated ArgumentOperation
&& Argument.HasNotNullAttribute(reducedFrom.Parameters.First())

Choose a reason for hiding this comment

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

With #7036 we also get IMethodSymbol.ReceiverNullableAnnotation which would improve the precision in nullable context.

""";
var validator = new SETestContext(code, AnalyzerLanguage.VisualBasic, Array.Empty<SymbolicCheck>()).Validator;
validator.ValidateTag("AfterGuard_o1", x => x.Should().HaveOnlyConstraint(ObjectConstraint.NotNull));
validator.ValidateTag("AfterGuard_o2", x => x.Should().HaveNoConstraints()); // Extension method invoked on Object type is DynamicInvocationOperation that we don't support

Choose a reason for hiding this comment

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

The comment could also go into "because" as there isn't much we can do about it and it will be permanent behavior.

Suggested change
validator.ValidateTag("AfterGuard_o2", x => x.Should().HaveNoConstraints()); // Extension method invoked on Object type is DynamicInvocationOperation that we don't support
validator.ValidateTag("AfterGuard_o2", x => x.Should().HaveNoConstraints("extension method invoked on Object type is DynamicInvocationOperation that we don't support"));

validator.ValidateTag("AfterGuard_o3", x => x.Should().HaveOnlyConstraint(ObjectConstraint.NotNull));
validator.ValidateTag("AfterGuard_o4", x => x.Should().HaveOnlyConstraint(ObjectConstraint.NotNull));
validator.ValidateTag("AfterGuard_o5", x => x.Should().HaveOnlyConstraint(ObjectConstraint.NotNull));
validator.ValidateTag("AfterGuard_o6", x => x.Should().HaveNoConstraints()); // parameter is not annotated

Choose a reason for hiding this comment

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

Suggested change
validator.ValidateTag("AfterGuard_o6", x => x.Should().HaveNoConstraints()); // parameter is not annotated
validator.ValidateTag("AfterGuard_o6", x => x.Should().HaveNoConstraints("parameter is not annotated"));

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM. You may want to change the parameter type for the extension method.


public static class IParameterSymbolExtensions
{
public static bool HasNotNullAttribute(this IParameterSymbol parameter) =>

Choose a reason for hiding this comment

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

NIT:
NotNull has AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue) (see). So it can be applied to other symbols as well. You may consider changing the type to ISymbol instead.

@pavel-mikula-sonarsource pavel-mikula-sonarsource marked this pull request as draft April 13, 2023 09:09
@pavel-mikula-sonarsource
Copy link
Contributor Author

Converted to draft for now, I will merge S3900 for VB.NET first #7045 and this will fix some FPs there (this is easier to rebase)

@pavel-mikula-sonarsource pavel-mikula-sonarsource marked this pull request as ready for review April 13, 2023 10:43
@sonarcloud
Copy link

sonarcloud bot commented Apr 13, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarcloud
Copy link

sonarcloud bot commented Apr 13, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@pavel-mikula-sonarsource pavel-mikula-sonarsource merged commit 82304b5 into master Apr 13, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource deleted the Pavel/SE/NotNullExtensions-VB branch April 13, 2023 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix S2259 FP: ValidatedNotNullAttribute in extension method
2 participants