Skip to content

Commit

Permalink
Static properties and fields named Value
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioaversa committed Mar 30, 2023
1 parent 9480d62 commit 008e632
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ protected override ProgramState PreProcessSimple(SymbolicContext context)
if (operationInstance.Kind == OperationKindEx.PropertyReference
&& operationInstance.ToPropertyReference() is var reference
&& reference.Property.Name == nameof(Nullable<int>.Value)
&& reference.Instance.Type.IsNullableValueType()
&& context.HasConstraint(reference.Instance, ObjectConstraint.Null))
&& reference.Instance is { } referenceInstance
&& referenceInstance.Type.IsNullableValueType()
&& context.HasConstraint(referenceInstance, ObjectConstraint.Null))
{
ReportIssue(reference.Instance, reference.Instance.Syntax.ToString());
ReportIssue(referenceInstance, referenceInstance.Syntax.ToString());
}
else if (operationInstance.Kind == OperationKindEx.Conversion
&& operationInstance.ToConversion() is var conversion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ void Basics(MaybeInt i)
}
}

namespace TypeWithValueProperty
namespace TypeWithValueInstanceProperty
{
class Test
{
Expand Down Expand Up @@ -990,3 +990,56 @@ struct StructWithValuePropertyAndCastOperators
}
}

namespace TypeWithValueStaticProperty
{
class Test
{
void Basics()
{
_ = AClassWithStaticValueProperty.Value; // Compliant, not on nullable value type
_ = AClassWithStaticValueProperty.Value.Value; // Compliant
_ = AClassWithStaticValueProperty.Value.Value.InstanceProperty; // Compliant
_ = AClassWithStaticValueProperty.Value.Value.InstanceProperty.Value; // Compliant
_ = new AClassWithInstanceValueProperty().Value; // Compliant
}
}

class AClassWithStaticValueProperty
{
public AClassWithInstanceValueProperty InstanceProperty => new AClassWithInstanceValueProperty();

public static AClassWithInstanceValueProperty Value => new AClassWithInstanceValueProperty();
}

class AClassWithInstanceValueProperty
{
public AClassWithStaticValueProperty Value => new AClassWithStaticValueProperty();
}
}

namespace TypeWithValueStaticField
{
class Test
{
void Basics()
{
_ = AClassWithStaticValueField.Value; // Compliant, not on nullable value type
_ = AClassWithStaticValueField.Value.Value; // Compliant
_ = AClassWithStaticValueField.Value.Value.InstanceField; // Compliant
_ = AClassWithStaticValueField.Value.Value.InstanceField.Value; // Compliant
_ = new AClassWithInstanceValueField().Value; // Compliant
}
}

class AClassWithStaticValueField
{
public AClassWithInstanceValueField InstanceField = new AClassWithInstanceValueField();

public static AClassWithInstanceValueField Value = new AClassWithInstanceValueField();
}

class AClassWithInstanceValueField
{
public AClassWithStaticValueField Value = new AClassWithStaticValueField();
}
}

0 comments on commit 008e632

Please sign in to comment.