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

S2743: Add support for record struct #5607

Merged
merged 3 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -24,6 +24,7 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using SonarAnalyzer.Extensions;
using SonarAnalyzer.Helpers;
using StyleCop.Analyzers.Lightup;

Expand All @@ -40,41 +41,41 @@ public sealed class StaticFieldInGenericClass : SonarDiagnosticAnalyzer
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);

protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
context.RegisterSyntaxNodeActionInNonGenerated(c =>
{
var typeDeclaration = (TypeDeclarationSyntax)c.Node;
if (c.ContainingSymbol.Kind != SymbolKind.NamedType
if (c.IsRedundantPositionalRecordContext()
|| typeDeclaration.TypeParameterList == null
|| typeDeclaration.TypeParameterList.Parameters.Count < 1)
Comment on lines 48 to 49

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
|| typeDeclaration.TypeParameterList == null
|| typeDeclaration.TypeParameterList.Parameters.Count < 1)
|| c.Node is not TypeDeclarationSyntax { TypeParameterList: { Parameters: { Count: > 0 } } } typeDeclaration)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@csaba-sagi-sonarsource That's one of the places where C#10 would remove some clutter:

// C# 9
c.Node is not TypeDeclarationSyntax { TypeParameterList: { Parameters: { Count: > 0 } } } typeDeclaration)
// C# 10
c.Node is not TypeDeclarationSyntax { TypeParameterList.Parameters.Count: > 0  } typeDeclaration)

{
return;
}
var typeParameterNames = typeDeclaration.TypeParameterList.Parameters.Select(x => x.Identifier.ToString()).ToArray();
var fields = typeDeclaration.Members.OfType<FieldDeclarationSyntax>().Where(f => f.Modifiers.Any(SyntaxKind.StaticKeyword));
foreach (var field in fields.Where(field => !HasGenericType(field.Declaration.Type, typeParameterNames, c)))
var fields = typeDeclaration.Members.OfType<FieldDeclarationSyntax>().Where(x => x.Modifiers.Any(SyntaxKind.StaticKeyword));
foreach (var field in fields.Where(x => !HasGenericType(c, x.Declaration.Type, typeParameterNames)))
{
field.Declaration.Variables.ToList().ForEach(variable => CheckMember(variable, variable.Identifier.GetLocation(), typeParameterNames, c));
field.Declaration.Variables.ToList().ForEach(variable => CheckMember(c, variable, variable.Identifier.GetLocation(), typeParameterNames));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified:

                    var variables = typeDeclaration.Members
                        .OfType<FieldDeclarationSyntax>()
                        .Where(x => x.Modifiers.Any(SyntaxKind.StaticKeyword) && !HasGenericType(c, x.Declaration.Type, typeParameterNames))
                        .SelectMany(x => x.Declaration.Variables);
                    foreach (var variable in variables)
                    {
                        CheckMember(c, variable, variable.Identifier.GetLocation(), typeParameterNames);
                    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

foreach (var property in typeDeclaration.Members.OfType<PropertyDeclarationSyntax>().Where(x => x.Modifiers.Any(SyntaxKind.StaticKeyword)))
{
CheckMember(property, property.Identifier.GetLocation(), typeParameterNames, c);
CheckMember(c, property, property.Identifier.GetLocation(), typeParameterNames);
}
},
SyntaxKind.ClassDeclaration,
SyntaxKind.InterfaceDeclaration,
SyntaxKindEx.RecordClassDeclaration,
SyntaxKindEx.RecordStructDeclaration,
SyntaxKind.StructDeclaration);

private static void CheckMember(SyntaxNode root, Location location, string[] typeParameterNames, SyntaxNodeAnalysisContext context)
private static void CheckMember(SyntaxNodeAnalysisContext context, SyntaxNode root, Location location, string[] typeParameterNames)
{
if (!HasGenericType(root, typeParameterNames, context))
if (!HasGenericType(context, root, typeParameterNames))
{
context.ReportIssue(Diagnostic.Create(Rule, location));
}
}

private static bool HasGenericType(SyntaxNode root, string[] typeParameterNames, SyntaxNodeAnalysisContext context) =>
private static bool HasGenericType(SyntaxNodeAnalysisContext context, SyntaxNode root, string[] typeParameterNames) =>
root.DescendantNodesAndSelf()
.OfType<IdentifierNameSyntax>()
.Any(x => typeParameterNames.Contains(x.Identifier.Value) && context.SemanticModel.GetSymbolInfo(x).Symbol is { Kind: SymbolKind.TypeParameter });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
record struct StaticFieldInGenericRecordStruct<T>
where T : class
{
internal static string field; // FN
internal static string field; // Noncompliant

public static string Prop1 { get; set; } // FN
public static string Prop1 { get; set; } // Noncompliant

public string Prop2 { get; set; } = "";

Expand All @@ -13,9 +13,9 @@
record struct StaticFieldInGenericPositionalRecordStruct<T>(int Property)
where T : class
{
internal static string field; // FN
internal static string field; // Noncompliant

public static string Prop1 { get; set; } // FN
public static string Prop1 { get; set; } // Noncompliant

public string Prop2 { get; set; } = "";

Expand Down