diff --git a/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/RedundantDeclaration.cs b/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/RedundantDeclaration.cs index 6bf0d75f67a..6a91bae065c 100644 --- a/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/RedundantDeclaration.cs +++ b/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/RedundantDeclaration.cs @@ -231,7 +231,8 @@ private static void ReportRedundantArrayTypeSpecifier(SyntaxNodeAnalysisContext { if (array.Initializer == null || !array.Initializer.Expressions.Any() || - array.Type == null) + array.Type == null || + array.Type.RankSpecifiers.Count > 1) { return; } @@ -250,7 +251,7 @@ private static void ReportRedundantArrayTypeSpecifier(SyntaxNodeAnalysisContext var canBeSimplified = array.Initializer.Expressions .Select(exp => context.SemanticModel.GetTypeInfo(exp).Type) - .All(type => object.Equals(type, arrayType.ElementType)); + .All(type => Equals(type, arrayType.ElementType)); if (canBeSimplified) { diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ArraySize.Fixed.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ArraySize.Fixed.cs index 5281ce51d4a..04e04fbd033 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ArraySize.Fixed.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ArraySize.Fixed.cs @@ -32,8 +32,14 @@ public RedundantDeclaration() var xxx = new int[,] { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } }; var yyy = new int[,] { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } }; - var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; // Fixed - var www = new int[][][] { new[] { new[] { 0 } } }; // Fixed + + // see https://github.com/SonarSource/sonar-csharp/issues/1840 + var multiDimIntArray = new int[][] // Compliant - type specifier is mandatory here + { + new int[] { 1 } // Fixed + }; + var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; + var www = new int[][][] { new[] { new[] { 0 } } }; int? xx = ((new int?(5))); // Fixed xx = new Nullable(5); // Fixed diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ArrayType.Fixed.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ArrayType.Fixed.cs index 63de4eb64ef..f775175e0ab 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ArrayType.Fixed.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ArrayType.Fixed.cs @@ -34,8 +34,14 @@ public RedundantDeclaration() var yyy = new int[3 // Fixed , 3// Fixed ] { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } }; - var zzz = new[] { new[] { 1, 2, 3 }, new int[0], new int[0] }; // Fixed - var www = new[] { new[] { new[] { 0 } } }; // Fixed + + // see https://github.com/SonarSource/sonar-csharp/issues/1840 + var multiDimIntArray = new int[][] // Compliant - type specifier is mandatory here + { +new[]{ 1 } // Fixed + }; + var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; + var www = new int[][][] { new[] { new[] { 0 } } }; int? xx = ((new int?(5))); // Fixed xx = new Nullable(5); // Fixed diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.DelegateParameterList.Fixed.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.DelegateParameterList.Fixed.cs index 6e4abfb52ef..103419da0ae 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.DelegateParameterList.Fixed.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.DelegateParameterList.Fixed.cs @@ -34,8 +34,14 @@ public RedundantDeclaration() var yyy = new int[3 // Fixed , 3// Fixed ] { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } }; - var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; // Fixed - var www = new int[][][] { new[] { new[] { 0 } } }; // Fixed + + // see https://github.com/SonarSource/sonar-csharp/issues/1840 + var multiDimIntArray = new int[][] // Compliant - type specifier is mandatory here + { + new int[] { 1 } // Fixed + }; + var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; + var www = new int[][][] { new[] { new[] { 0 } } }; int? xx = ((new int?(5))); // Fixed xx = new Nullable(5); // Fixed diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ExplicitDelegate.Fixed.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ExplicitDelegate.Fixed.cs index 65d0f263772..1015326e7ae 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ExplicitDelegate.Fixed.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ExplicitDelegate.Fixed.cs @@ -33,8 +33,14 @@ public RedundantDeclaration() var yyy = new int[3 // Fixed , 3// Fixed ] { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } }; - var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; // Fixed - var www = new int[][][] { new[] { new[] { 0 } } }; // Fixed + + // see https://github.com/SonarSource/sonar-csharp/issues/1840 + var multiDimIntArray = new int[][] // Compliant - type specifier is mandatory here + { + new int[] { 1 } // Fixed + }; + var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; + var www = new int[][][] { new[] { new[] { 0 } } }; int? xx = ((new int?(5))); // Fixed xx = new Nullable(5); // Fixed diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ExplicitNullable.Fixed.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ExplicitNullable.Fixed.cs index ca82121b841..50cb68a6ecc 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ExplicitNullable.Fixed.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ExplicitNullable.Fixed.cs @@ -34,8 +34,14 @@ public RedundantDeclaration() var yyy = new int[3 // Fixed , 3// Fixed ] { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } }; - var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; // Fixed - var www = new int[][][] { new[] { new[] { 0 } } }; // Fixed + + // see https://github.com/SonarSource/sonar-csharp/issues/1840 + var multiDimIntArray = new int[][] // Compliant - type specifier is mandatory here + { + new int[] { 1 } // Fixed + }; + var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; + var www = new int[][][] { new[] { new[] { 0 } } }; int? xx = ((5)); // Fixed xx = 5; // Fixed diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.LambdaParameterType.Fixed.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.LambdaParameterType.Fixed.cs index 265c07b8bfd..08952e0849e 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.LambdaParameterType.Fixed.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.LambdaParameterType.Fixed.cs @@ -34,8 +34,14 @@ public RedundantDeclaration() var yyy = new int[3 // Fixed , 3// Fixed ] { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } }; - var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; // Fixed - var www = new int[][][] { new[] { new[] { 0 } } }; // Fixed + + // see https://github.com/SonarSource/sonar-csharp/issues/1840 + var multiDimIntArray = new int[][] // Compliant - type specifier is mandatory here + { + new int[] { 1 } // Fixed + }; + var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; + var www = new int[][][] { new[] { new[] { 0 } } }; int? xx = ((new int?(5))); // Fixed xx = new Nullable(5); // Fixed diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ObjectInitializer.Fixed.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ObjectInitializer.Fixed.cs index 174a68b5dcb..846dd55f5e7 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ObjectInitializer.Fixed.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.ObjectInitializer.Fixed.cs @@ -34,8 +34,14 @@ public RedundantDeclaration() var yyy = new int[3 // Fixed , 3// Fixed ] { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } }; - var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; // Fixed - var www = new int[][][] { new[] { new[] { 0 } } }; // Fixed + + // see https://github.com/SonarSource/sonar-csharp/issues/1840 + var multiDimIntArray = new int[][] // Compliant - type specifier is mandatory here + { + new int[] { 1 } // Fixed + }; + var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; + var www = new int[][][] { new[] { new[] { 0 } } }; int? xx = ((new int?(5))); // Fixed xx = new Nullable(5); // Fixed diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.cs index c82cc44db87..c7b2bd03be1 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.cs @@ -40,8 +40,14 @@ public RedundantDeclaration() var yyy = new int[3 // Noncompliant, we report two issues on this to keep the comma unfaded , 3// Noncompliant ] { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } }; - var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; // Noncompliant - var www = new int[][][] { new[] { new[] { 0 } } }; // Noncompliant + + // see https://github.com/SonarSource/sonar-csharp/issues/1840 + var multiDimIntArray = new int[][] // Compliant - type specifier is mandatory here + { + new int[] { 1 } // Noncompliant + }; + var zzz = new int[][] { new[] { 1, 2, 3 }, new int[0], new int[0] }; + var www = new int[][][] { new[] { new[] { 0 } } }; int? xx = ((new int?(5))); // Noncompliant {{Remove the explicit nullable type creation; it is redundant.}} // ^^^^^^^^