Skip to content

Commit

Permalink
Add FP repro for #4081 (#4082)
Browse files Browse the repository at this point in the history
  • Loading branch information
csaba-sagi-sonarsource authored Feb 22, 2021
1 parent 74b02c5 commit f6480c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,19 @@ namespace SonarAnalyzer.Rules.CSharp
public sealed class StaticFieldInGenericClass : SonarDiagnosticAnalyzer
{
internal const string DiagnosticId = "S2743";
private const string MessageFormat =
"A static field in a generic type is not shared among instances of different close constructed types.";
private const string MessageFormat = "A static field in a generic type is not shared among instances of different close constructed types.";
private static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorBuilder.GetDescriptor(DiagnosticId, MessageFormat, RspecStrings.ResourceManager);

private static readonly DiagnosticDescriptor rule =
DiagnosticDescriptorBuilder.GetDescriptor(DiagnosticId, MessageFormat, RspecStrings.ResourceManager);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(rule);

protected override void Initialize(SonarAnalysisContext context)
{
protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var classDeclaration = (ClassDeclarationSyntax)c.Node;
if (classDeclaration.TypeParameterList == null ||
classDeclaration.TypeParameterList.Parameters.Count < 1)
if (classDeclaration.TypeParameterList == null
|| classDeclaration.TypeParameterList.Parameters.Count < 1)
{
return;
}
Expand All @@ -65,25 +61,18 @@ protected override void Initialize(SonarAnalysisContext context)
foreach (var field in fields.Where(field => !HasGenericType(field.Declaration.Type, typeParameterNames, c)))
{
field.Declaration.Variables.ToList().ForEach(variable =>
{
CheckMember(variable, variable.Identifier.GetLocation(), typeParameterNames, c);
});
field.Declaration.Variables.ToList().ForEach(variable => CheckMember(variable, variable.Identifier.GetLocation(), typeParameterNames, c));
}
var properties = classDeclaration.Members
.OfType<PropertyDeclarationSyntax>()
.Where(p => p.Modifiers.Any(SyntaxKind.StaticKeyword))
.ToList();
properties.ForEach(property =>
{
CheckMember(property, property.Identifier.GetLocation(), typeParameterNames, c);
});
properties.ForEach(property => CheckMember(property, property.Identifier.GetLocation(), typeParameterNames, c));
},
SyntaxKind.ClassDeclaration);
}

private static void CheckMember(SyntaxNode root, Location location, IEnumerable<string> typeParameterNames,
SyntaxNodeAnalysisContext context)
Expand All @@ -93,7 +82,7 @@ private static void CheckMember(SyntaxNode root, Location location, IEnumerable<
return;
}

context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, location));
context.ReportDiagnosticWhenActive(Diagnostic.Create(Rule, location));
}

private static bool HasGenericType(SyntaxNode root, IEnumerable<string> typeParameterNames,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@ class StaticFieldInGenericClass<T/*comment*/, /*comment*/U>
public static T tProp { get; set; }

internal static string sField; //Noncompliant

internal string NestedClassUsage => NestedClass.StringField;

//https://github.com/SonarSource/sonar-dotnet/issues/4081
private static class NestedClass
{
public static readonly string StringField = "String"; // FN
}
}
}

0 comments on commit f6480c4

Please sign in to comment.