-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
e255719
commit 388e7f8
Showing
6 changed files
with
149 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 49 additions & 6 deletions
55
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/VariableShadowsField.CSharp12.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,54 @@ | ||
class MyClass(int first, int second, int third, int fourth) | ||
using System; | ||
|
||
class MyParentClass(int parentClassParam) | ||
{ | ||
private int first = first; // Compliant | ||
private int second; // FN | ||
class MyClass(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) | ||
{ | ||
public int a = a; // Compliant | ||
public int b; // FN | ||
|
||
public int c { get; set; } = c; // Compliant | ||
public int d { get; set; } // FN | ||
|
||
public int g = a; // FN {{Rename 'g' which hides the primary constructor parameter with the same name.}} | ||
|
||
class MyChildClass(int childClassParam) { } | ||
|
||
MyClass(int ctorParam, int f) : this(ctorParam, f, 1, 1, 1, 1, 1, 1, 1, 1) { } // Compliant | ||
|
||
void MyMethod() | ||
{ | ||
int parentClassParam = 42; // Compliant | ||
int childClassParam = 42; // Compliant | ||
int ctorParam = 42; // Compliant | ||
|
||
void MyMethod() | ||
int f = 42; // Noncompliant {{Rename 'f' which hides the primary constructor parameter with the same name.}} | ||
_ = a is object g; // Noncompliant | ||
for (int h = 0; h < 10; h++) { } // Noncompliant {{Rename 'h' which hides the primary constructor parameter with the same name.}} | ||
for (int a = 0; a < 10; a++) { } // Noncompliant {{Rename 'a' which hides the field with the same name.}} | ||
for (int c = 0; c < 10; c++) { } // Noncompliant {{Rename 'c' which hides the property with the same name.}} | ||
var lambda = (int h = 1) => h; // Compliant | ||
foreach (var (i, j) in new (int, int)[0]) { } // Noncompliant | ||
// Noncompliant@-1 | ||
} | ||
} | ||
} | ||
|
||
// 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() | ||
{ | ||
int third = 1; // FN | ||
_ = first is object fourth; // FN | ||
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 staticField = 2; // Noncompliant | ||
var staticProperty = 2; // Noncompliant | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/VariableShadowsField_PartialClass1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
public partial class VariableShadowsField | ||
{ | ||
public int myField; | ||
public int myProperty { get; set; } | ||
} | ||
|
||
public partial class VariableShadowsFieldPrimaryConstructor(int a) { } |
19 changes: 19 additions & 0 deletions
19
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/VariableShadowsField_PartialClass2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
public partial class VariableShadowsField | ||
{ | ||
public void Method(object someParameter) | ||
{ | ||
int myField = 0; // Noncompliant | ||
// ^^^^^^^ | ||
int myProperty = 0; // Noncompliant | ||
// ^^^^^^^^^^ | ||
} | ||
} | ||
|
||
public partial class VariableShadowsFieldPrimaryConstructor | ||
{ | ||
void Method() | ||
{ | ||
var a = 0; // Noncompliant | ||
// ^ | ||
} | ||
} |