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

Fix S6964 FP: Properties with default values #9348

Merged
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 @@ -76,10 +76,12 @@ private static void CheckInvalidProperties(INamedTypeSymbol parameterType, Sonar
var declaredProperties = new List<IPropertySymbol>();
GetAllDeclaredProperties(parameterType, examinedTypes, declaredProperties);
var invalidProperties = declaredProperties
.Where(x => !CanBeNull(x.Type) && !x.HasAttribute(KnownType.System_Text_Json_Serialization_JsonRequiredAttribute) && !x.IsRequired());
.Where(x => !CanBeNull(x.Type) && !x.HasAttribute(KnownType.System_Text_Json_Serialization_JsonRequiredAttribute) && !x.IsRequired())
.Select(x => x.GetFirstSyntaxRef())
.Where(x => !IsInitialized(x));
foreach (var property in invalidProperties)
{
context.ReportIssue(Rule, property.GetFirstSyntaxRef().GetIdentifier()?.GetLocation());
context.ReportIssue(Rule, property.GetIdentifier()?.GetLocation());
}
}

Expand Down Expand Up @@ -144,4 +146,7 @@ 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 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace CSharp9
// https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding#constructor-binding-and-record-types
public record RecordModel(
int ValueProperty, // Noncompliant
[property: JsonRequired] int RequiredValueProperty, // without the property prefix the attribute would have been applied to the constructor parameter instead
int? NullableValueProperty);
[property: JsonRequired] int RequiredValueProperty, // without the property prefix the attribute would have been applied to the constructor parameter instead
int? NullableValueProperty,
int PropertyWithDefaultValue = 42);

public class ModelUsedInController
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ModelUsedInController
internal int InternalProperty { get; set; }
protected internal int ProtectedInternalProperty { get; set; }
private int PrivateProperty { get; set; }
public int PropertyWithDefaultValue { get; set; } = 42;
public int ReadOnlyProperty => 42;
public int field = 42;

Expand Down
Loading