-
Notifications
You must be signed in to change notification settings - Fork 226
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
Fix S2259 FP: ValidatedNotNullAttribute in extension method #7049
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.
HasNotNullAttribute and IsNotNullAttribute should not be public in the Argument
processor.
public static bool IsNotNullAttribute(AttributeData attribute) => | ||
attribute.HasAnyName("ValidatedNotNullAttribute", "NotNullAttribute"); |
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.
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).
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 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()) |
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.
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 |
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 comment could also go into "because" as there isn't much we can do about it and it will be permanent behavior.
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 |
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.
validator.ValidateTag("AfterGuard_o6", x => x.Should().HaveNoConstraints()); // parameter is not annotated | |
validator.ValidateTag("AfterGuard_o6", x => x.Should().HaveNoConstraints("parameter is not annotated")); |
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. You may want to change the parameter type for the extension method.
|
||
public static class IParameterSymbolExtensions | ||
{ | ||
public static bool HasNotNullAttribute(this IParameterSymbol parameter) => |
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.
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.
e90326e
to
5011f28
Compare
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
Fixes #7048
This will also fix the same problem on S3900