Skip to content

Commit

Permalink
Added check for conversion operation with top-down approach
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-kolbay-sonarsource committed Mar 29, 2023
1 parent 2ae8193 commit ede37b4
Showing 1 changed file with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ public class PublicMethodArgumentsShouldBeCheckedForNull : SymbolicRuleCheck
private const string DiagnosticId = "S3900";
private const string MessageFormat = "{0}";

private static readonly OperationKind[] DereferenceOperations = new[]
{
OperationKindEx.Invocation,
OperationKindEx.FieldReference,
OperationKindEx.PropertyReference,
OperationKindEx.EventReference,
OperationKindEx.Await,
OperationKindEx.ArrayElementReference
};

internal static readonly DiagnosticDescriptor S3900 = DescriptorFactory.Create(DiagnosticId, MessageFormat);

protected override DiagnosticDescriptor Rule => S3900;
Expand Down Expand Up @@ -101,8 +91,9 @@ bool NullableStateIsNotKnownForParameter(IParameterSymbol symbol) =>
context.State[symbol] is null || !context.State[symbol].HasConstraint<ObjectConstraint>();
}

private static IOperation NullDereferenceCandidate(IOperation operation) =>
operation.Kind switch
private static IOperation NullDereferenceCandidate(IOperation operation)
{
var candidate = operation.Kind switch
{
OperationKindEx.Invocation => operation.ToInvocation().Instance,
OperationKindEx.FieldReference => operation.ToFieldReference().Instance,
Expand All @@ -113,6 +104,16 @@ private static IOperation NullDereferenceCandidate(IOperation operation) =>
_ => null,
};

return candidate?.Kind == OperationKindEx.Conversion
? ConversionOperand(candidate)
: candidate;
}

private static IOperation ConversionOperand(IOperation operation) =>
operation.Kind == OperationKindEx.Conversion
? ConversionOperand(operation.ToConversion().Operand)
: operation;

private sealed class ArgumentDereferenceWalker : SafeCSharpSyntaxWalker
{
private readonly ISet<string> argumentNames;
Expand Down

0 comments on commit ede37b4

Please sign in to comment.