Skip to content

Commit

Permalink
don't raise for reference types in nullable context
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-georgiou-sonarsource committed May 22, 2024
1 parent 9423b57 commit 4e24132
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private static bool CanBeUsedInModelBinding(INamedTypeSymbol type) =>

private static bool CanBeNull(ITypeSymbol type) =>
type is ITypeParameterSymbol { HasValueTypeConstraint: false }
|| (type.IsReferenceType && type.NullableAnnotation() != NullableAnnotation.NotAnnotated)
|| type.IsReferenceType
|| type.IsNullableValueType();

private static void GetAllDeclaredProperties(ITypeSymbol type, ConcurrentDictionary<ITypeSymbol, bool> examinedTypes, List<IPropertySymbol> declaredProperties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ public class ModelUsedInController
// Otherwise, strings behave always as nullable reference types.
#nullable enable
public string NonNullableReferenceProperty { get; set; }
public object AnotherNonNullableReferenceProperty { get; set; }
[Required] public string RequiredNonNullableReferenceProperty { get; set; }
public string? NullableReferenceProperty { get; set; }
public int ValueProperty { get; set; } // Noncompliant
public int? NullableValueProperty { get; set; }
#nullable disable
public string ReferenceProperty { get; set; }
public object AnotherReferenceProperty { get; set; }
public int? AnotherNullableValueProperty { get; set; }
}

public class DerivedFromController : Controller
Expand All @@ -25,24 +30,3 @@ public IActionResult Create(ModelUsedInController model)
}
}
}

namespace CustomGenerics
{
public class GenericType<TNoContstraint, TClass, TStruct, TNotNull>
where TClass : class
where TStruct : struct
where TNotNull : notnull
{
public TNoContstraint NoConstraintProperty { get; set; }
public TClass ClassProperty { get; set; }
public TStruct StructProperty { get; set; } // Noncompliant
[Required] public TStruct RequiredStructProperty { get; set; }
public TStruct? NullableStructProperty { get; set; }
public TNotNull NotNullProperty { get; set; } // Noncompliant
}

public class ControllerClass : Controller
{
[HttpPost] public IActionResult Create(GenericType<object, string, int, DateTime> model) => View(model);
}
}

0 comments on commit 4e24132

Please sign in to comment.