Skip to content

Commit

Permalink
Fix S1117 FP: Local variables declared in static context (#8937)
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-kolbay-sonarsource authored Mar 20, 2024
1 parent 065a35c commit 3d22fab
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 53 deletions.
30 changes: 0 additions & 30 deletions analyzers/its/expected/Nancy/S1117-Nancy-net452.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
{
"Issues": [
{
"Id": "S1117",
"Message": "Rename \u0027assemblies\u0027 which hides the field with the same name.",
"Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/Nancy/src/Nancy/AppDomainAssemblyCatalog.cs#L32",
"Location": "Line 32 Position 17-27"
},
{
"Id": "S1117",
"Message": "Rename \u0027assemblies\u0027 which hides the field with the same name.",
"Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/Nancy/src/Nancy/AppDomainAssemblyCatalog.cs#L41",
"Location": "Line 41 Position 17-27"
},
{
"Id": "S1117",
"Message": "Rename \u0027assemblies\u0027 which hides the field with the same name.",
"Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/Nancy/src/Nancy/AppDomainAssemblyCatalog.cs#L56",
"Location": "Line 56 Position 17-27"
},
{
"Id": "S1117",
"Message": "Rename \u0027parameters\u0027 which hides the field with the same name.",
"Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/Nancy/src/Nancy/HttpLink.cs#L230",
"Location": "Line 230 Position 17-27"
},
{
"Id": "S1117",
"Message": "Rename \u0027prefix\u0027 which hides the field with the same name.",
"Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/Nancy/src/Nancy/HttpLinkRelation.cs#L152",
"Location": "Line 152 Position 21-27"
},
{
"Id": "S1117",
"Message": "Rename \u0027value\u0027 which hides the field with the same name.",
"Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/Nancy/src/Nancy/HttpLinkRelation.cs#L153",
"Location": "Line 153 Position 21-26"
}
]
}
12 changes: 0 additions & 12 deletions analyzers/its/expected/Nancy/S1117-Nancy-netstandard2.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@
"Message": "Rename \u0027parameters\u0027 which hides the field with the same name.",
"Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/Nancy/src/Nancy/HttpLink.cs#L230",
"Location": "Line 230 Position 17-27"
},
{
"Id": "S1117",
"Message": "Rename \u0027prefix\u0027 which hides the field with the same name.",
"Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/Nancy/src/Nancy/HttpLinkRelation.cs#L152",
"Location": "Line 152 Position 21-27"
},
{
"Id": "S1117",
"Message": "Rename \u0027value\u0027 which hides the field with the same name.",
"Uri": "https://github.com/SonarSource/sonar-dotnet/blob/master/analyzers/its/sources/Nancy/src/Nancy/HttpLinkRelation.cs#L153",
"Location": "Line 153 Position 21-26"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ private static List<ISymbol> GetContextSymbols(SonarSyntaxNodeReportingContext c

private static void ReportOnVariableMatchingField(SonarSyntaxNodeReportingContext context, IEnumerable<ISymbol> members, SyntaxToken identifier)
{
if (members.FirstOrDefault(x => x.Name == identifier.ValueText) is { } matchingMember)
if (members.FirstOrDefault(x => x.Name == identifier.ValueText
&& (x.IsStatic || !identifier.Parent.EnclosingScope().GetModifiers().Any(x => x.Kind() == SyntaxKind.StaticKeyword))) is { } matchingMember)
{
context.ReportIssue(Diagnostic.Create(Rule, identifier.GetLocation(), identifier.Text, GetSymbolName(matchingMember)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,13 @@ void MyMethod()
// Reproducer for https://github.com/SonarSource/sonar-dotnet/issues/8260
class Repro_8260(int primaryCtorParam)
{
private int instanceField = 1;
private int instanceProperty { get; set; } = 1;
private static int staticField = 1;
private static int staticProperty = 1;

public static void StaticMethod()
{
var instanceField = 2; // Noncompliant FP (instance field is not accessible from static method)
var instanceProperty = 2; // Noncompliant FP (instance property is not accessible from static method)
var primaryCtorParam = 2; // Noncompliant FP (primary ctor parameter is not accessible from static method)
var primaryCtorParam = 42; // Compliant (primary ctor parameter is not accessible from static method)
}

var staticField = 2; // Noncompliant
var staticProperty = 2; // Noncompliant
public void NonStaticMethod()
{
var primaryCtorParam = 42; // Noncompliant
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,59 @@ public void OutParameter(out object parameter)
parameter = null;
}
}

public class Base
{
private int privateInstanceField = 1;
private int privateInstanceProperty { get; set; } = 1;
private static int privateStaticField = 1;
private static int privateStaticProperty = 1;

protected int protectedInstanceField = 1;
protected int protectedInstanceProperty { get; set; } = 1;
protected static int protectedStaticField = 1;
protected static int protectedStaticProperty = 1;

public static void BaseStaticMethod()
{
var privateInstanceField = 2; // Compliant (instance field is not accessible from static method)
var privateInstanceProperty = 2; // Compliant (instance property is not accessible from static method)

var privateStaticField = 2; // Noncompliant
var privateStaticProperty = 2; // Noncompliant
}

public void BaseInstanceMethod()
{
var privateInstanceField = 2; // Noncompliant
var privateInstanceProperty = 2; // Noncompliant

var privateStaticField = 2; // Noncompliant
var privateStaticProperty = 2; // Noncompliant
}
}

public class Derived : Base
{
public static void DerivedStaticMethod()
{
var privateStaticField = 2; // Compliant (private field from the base class is not accessible in the derived class)
var privateStaticProperty = 2; // Compliant (private property from the base class is not accessible in the derived class)

var protectedInstanceField = 2; // Compliant (instance field is not accessible from static method)
var protectedInstanceProperty = 2; // Compliant (instance property is not accessible from static method)

var protectedStaticField = 2; // FN - members from the base class are not checked
var protectedStaticProperty = 2; // FN
}

public void DerivedInstanceMethod()
{
var protectedInstanceField = 2; // FN
var protectedInstanceProperty = 2; // FN

var protectedStaticField = 2; // FN
var protectedStaticProperty = 2; // FN
}
}
}

0 comments on commit 3d22fab

Please sign in to comment.