diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/Rules/RedundantDeclarationTest.cs b/analyzers/tests/SonarAnalyzer.UnitTest/Rules/RedundantDeclarationTest.cs index 22676105017..80a749b1f87 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/Rules/RedundantDeclarationTest.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/Rules/RedundantDeclarationTest.cs @@ -70,6 +70,30 @@ public void RedundantDeclaration_CSharp10_CodeFix_ExplicitDelegate() => .WithOptions(ParseOptionsHelper.FromCSharp10) .VerifyCodeFix(); + [TestMethod] + public void RedundantDeclaration_CSharp12() => + builder.AddPaths("RedundantDeclaration.CSharp12.cs") + .WithOptions(ParseOptionsHelper.FromCSharp12) + .Verify(); + + [TestMethod] + public void RedundantDeclaration_CSharp12_CodeFix_ArraySize() => + codeFixBuilder.AddPaths("RedundantDeclaration.CSharp12.cs") + .WithCodeFix() + .WithCodeFixTitle(RedundantDeclarationCodeFix.TitleRedundantArraySize) + .WithCodeFixedPaths("RedundantDeclaration.CSharp12.ArraySize.Fixed.cs") + .WithOptions(ParseOptionsHelper.FromCSharp12) + .VerifyCodeFix(); + + [TestMethod] + public void RedundantDeclaration_CSharp12_CodeFix_LambdaParameterType() => + codeFixBuilder.AddPaths("RedundantDeclaration.CSharp12.cs") + .WithCodeFix() + .WithCodeFixTitle(RedundantDeclarationCodeFix.TitleRedundantLambdaParameterType) + .WithCodeFixedPaths("RedundantDeclaration.CSharp12.LambdaParameterType.Fixed.cs") + .WithOptions(ParseOptionsHelper.FromCSharp12) + .VerifyCodeFix(); + #endif [TestMethod] diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.CSharp12.ArraySize.Fixed.cs b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.CSharp12.ArraySize.Fixed.cs new file mode 100644 index 00000000000..c4168356fac --- /dev/null +++ b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.CSharp12.ArraySize.Fixed.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; + +namespace AliasAnyType +{ + using Int = int; + using IntArray = int[]; + using IntNullable = int?; + using Point = (int x, int y); + using ThreeInts = int[3]; // Error [CS0270]: Array size cannot be specified in a variable declaration + using IntToIntFunc = Func; + + class AClass + { + void AliasWithArraySize() + { + IntArray a1 = new Int[] { 1, 2, 3 }; // Fixed + } + + void AliasWithUnnecessaryType() + { + IntToIntFunc f = (Int i) => i; // Fixed + } + + void AliasWithInitializer() + { + IntArray a1 = new IntArray { }; // Error [CS8386]: Invalid object creation + var a2 = new IntArray { }; // Error [CS8386]: Invalid object creation + int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation + } + + void AliasWithEmptyParamsList() + { + IntArray a1 = new IntArray(); // Error [CS8386]: Invalid object creation + var a2 = new IntArray(); // Error [CS8386]: Invalid object creation + int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation + } + } +} diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.CSharp12.LambdaParameterType.Fixed.cs b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.CSharp12.LambdaParameterType.Fixed.cs new file mode 100644 index 00000000000..879f8e8f346 --- /dev/null +++ b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.CSharp12.LambdaParameterType.Fixed.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; + +namespace AliasAnyType +{ + using Int = int; + using IntArray = int[]; + using IntNullable = int?; + using Point = (int x, int y); + using ThreeInts = int[3]; // Error [CS0270]: Array size cannot be specified in a variable declaration + using IntToIntFunc = Func; + + class AClass + { + void AliasWithArraySize() + { + IntArray a1 = new Int[3] { 1, 2, 3 }; // Fixed + } + + void AliasWithUnnecessaryType() + { + IntToIntFunc f = (i) => i; // Fixed + } + + void AliasWithInitializer() + { + IntArray a1 = new IntArray { }; // Error [CS8386]: Invalid object creation + var a2 = new IntArray { }; // Error [CS8386]: Invalid object creation + int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation + } + + void AliasWithEmptyParamsList() + { + IntArray a1 = new IntArray(); // Error [CS8386]: Invalid object creation + var a2 = new IntArray(); // Error [CS8386]: Invalid object creation + int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation + } + } +} diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.CSharp12.cs b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.CSharp12.cs new file mode 100644 index 00000000000..9be1211a743 --- /dev/null +++ b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.CSharp12.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; + +namespace AliasAnyType +{ + using Int = int; + using IntArray = int[]; + using IntNullable = int?; + using Point = (int x, int y); + using ThreeInts = int[3]; // Error [CS0270]: Array size cannot be specified in a variable declaration + using IntToIntFunc = Func; + + class AClass + { + void AliasWithArraySize() + { + IntArray a1 = new Int[3] { 1, 2, 3 }; // Noncompliant + } + + void AliasWithUnnecessaryType() + { + IntToIntFunc f = (Int i) => i; // Noncompliant {{Remove the type specification; it is redundant.}} + } + + void AliasWithInitializer() + { + IntArray a1 = new IntArray { }; // Error [CS8386]: Invalid object creation + var a2 = new IntArray { }; // Error [CS8386]: Invalid object creation + int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation + } + + void AliasWithEmptyParamsList() + { + IntArray a1 = new IntArray(); // Error [CS8386]: Invalid object creation + var a2 = new IntArray(); // Error [CS8386]: Invalid object creation + int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation + } + } +}