Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-kolbay-sonarsource committed May 29, 2024
1 parent 124a3dd commit c41fc6c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static void CheckInvalidProperties(INamedTypeSymbol parameterType, Sonar
var invalidProperties = declaredProperties
.Where(x => !CanBeNull(x.Type) && !x.HasAttribute(KnownType.System_Text_Json_Serialization_JsonRequiredAttribute) && !x.IsRequired())
.Select(x => x.GetFirstSyntaxRef())
.Where(x => !HasDefaultValue(x));
.Where(x => !IsInitialized(x));
foreach (var property in invalidProperties)
{
context.ReportIssue(Rule, property.GetIdentifier()?.GetLocation());
Expand Down Expand Up @@ -147,10 +147,6 @@ INamedTypeSymbol namedType when type.IsInSameAssembly(controllerType) => [namedT
private static bool HasValidateNeverAttribute(ISymbol symbol) =>
symbol.HasAttribute(KnownType.Microsoft_AspNetCore_Mvc_ModelBinding_Validation_ValidateNeverAttribute);

private static bool HasDefaultValue(SyntaxNode node) => node switch
{
ParameterSyntax { Default: not null } => true,
PropertyDeclarationSyntax { Initializer: not null } property => property.Parent.ParameterList() is null,
_ => false
};
private static bool IsInitialized(SyntaxNode node) =>
node is ParameterSyntax { Default: not null } or PropertyDeclarationSyntax { Initializer: not null };
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ModelWithPrimaryConstructor(int vp, int rvp, int nvp)
public class ModelWithPrimaryAndParameterlessConstructor(int vp, int rvp, int nvp)
{
public ModelWithPrimaryAndParameterlessConstructor() : this(0, 0, 0) { }
public int ValueProperty { get; set; } = vp; // Noncompliant
public int ValueProperty { get; set; } = vp; // Compliant - the property has default value
}

public class DerivedFromController : Controller
Expand Down

0 comments on commit c41fc6c

Please sign in to comment.