From 91826ed2a991d336b10c3b0e53bf575bf665decf Mon Sep 17 00:00:00 2001 From: Mary Georgiou <89914005+mary-georgiou-sonarsource@users.noreply.github.com> Date: Wed, 22 May 2024 14:50:20 +0200 Subject: [PATCH] Fix S6964 FP: Should not raise for reference properties in nullable context (#9308) --- .../SonarAnalyzer.CSharp/Rules/AspNet/AvoidUnderPosting.cs | 2 +- .../TestCases/AspNet/AvoidUnderPosting.CSharp8.cs | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/analyzers/src/SonarAnalyzer.CSharp/Rules/AspNet/AvoidUnderPosting.cs b/analyzers/src/SonarAnalyzer.CSharp/Rules/AspNet/AvoidUnderPosting.cs index 9f836873228..b029ef202bf 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Rules/AspNet/AvoidUnderPosting.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Rules/AspNet/AvoidUnderPosting.cs @@ -103,7 +103,7 @@ private static void CheckInvalidProperties(INamedTypeSymbol parameterType, Sonar 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 examinedTypes, List declaredProperties) diff --git a/analyzers/tests/SonarAnalyzer.Test/TestCases/AspNet/AvoidUnderPosting.CSharp8.cs b/analyzers/tests/SonarAnalyzer.Test/TestCases/AspNet/AvoidUnderPosting.CSharp8.cs index 99f6b8e962f..acdab62faa6 100644 --- a/analyzers/tests/SonarAnalyzer.Test/TestCases/AspNet/AvoidUnderPosting.CSharp8.cs +++ b/analyzers/tests/SonarAnalyzer.Test/TestCases/AspNet/AvoidUnderPosting.CSharp8.cs @@ -7,11 +7,16 @@ namespace NullableReferences public class ModelUsedInController { #nullable enable - public string NonNullableReferenceProperty { get; set; } // Noncompliant + public string NonNullableReferenceProperty { get; set; } // Compliant - ASP.NET Core treats non-nullable reference types as if they were decorated with the [Required] attribute + 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