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

S3655: Honor null forgiving operator #7015

Merged
merged 3 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,24 @@ protected override ProgramState PreProcessSimple(SymbolicContext context)
&& reference.Property.Name == nameof(Nullable<int>.Value)
&& reference.Instance is { } instance
&& instance.Type.IsNullableValueType()
&& context.HasConstraint(instance, ObjectConstraint.Null))
&& context.HasConstraint(instance, ObjectConstraint.Null)
&& FlowState(reference.Instance) != NullableFlowState.NotNull)
{
ReportIssue(instance, instance.Syntax.ToString());
}
else if (operationInstance.Kind == OperationKindEx.Conversion
&& operationInstance.ToConversion() is var conversion
&& conversion.Operand.Type.IsNullableValueType()
&& conversion.Type.IsNonNullableValueType()
&& context.HasConstraint(conversion.Operand, ObjectConstraint.Null))
&& context.HasConstraint(conversion.Operand, ObjectConstraint.Null)
&& FlowState(conversion.Operand) != NullableFlowState.NotNull)
{
ReportIssue(conversion.Operand, conversion.Operand.Syntax.ToString());
}

return context.State;

NullableFlowState FlowState(IOperation reference) =>
SemanticModel.GetTypeInfo(reference.Syntax).Nullability().FlowState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@ class NullForgivingOperator
{
void Basics(int? i)
{
_ = i!.Value; // Compliant, unknown
_ = i!.Value; // Compliant, user-asserted non-empty via bang
i = SomeMethod();
_ = i!.Value; // Compliant, unknown
_ = i!.Value; // Compliant

i = null;
_ = i!.Value; // Noncompliant, empty
_ = i!.Value; // Compliant
i = new int?();
_ = i!.Value; // Noncompliant, empty
_ = i!.Value; // Compliant
i = new Nullable<int>();
_ = i!.Value; // Noncompliant, empty
_ = i!.Value; // Compliant

i = 42;
_ = i!.Value; // Compliant, non-empty
_ = i!.Value; // Compliant
}

void CastToValueType(int? i)
{
_ = (int)i!; // Compliant, unknown
_ = (int)i!; // Compliant, user-asserted non-empty via bang
i = SomeMethod();
_ = (int)i!; // Compliant, unknown
_ = (int)i!; // Compliant

i = null;
_ = (int)i!; // Noncompliant, empty
_ = (int)i!; // Compliant
i = new int?();
_ = (int)i!; // Noncompliant, empty
_ = (int)i!; // Compliant
i = new Nullable<int>();
_ = (int)i!; // Noncompliant, empty
_ = (int)i!; // Compliant
}

void CastToNullableType(int? i)
{
_ = ((int?)i)!.Value; // Compliant, unknown
_ = (i as int?)!.Value; // Compliant, unknown
_ = ((int?)i)!.Value; // Compliant, user-asserted non-empty via bang
_ = (i as int?)!.Value; // Compliant

_ = ((int?)null)!.Value; // Noncompliant
_ = (null as int?)!.Value; // Noncompliant
_ = ((int?)null)!.Value; // Compliant
_ = (null as int?)!.Value; // Compliant
}

static int? SomeMethod() => null;
Expand Down