Skip to content

Commit

Permalink
Fix S3257: should not recommend removing explicit type for multidimen… (
Browse files Browse the repository at this point in the history
#1922)

…sional array
  • Loading branch information
Amaury Levé authored Oct 2, 2018
1 parent 1cbc816 commit 2ead440
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(5); // Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(5); // Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(5); // Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(5); // Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(5); // Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(5); // Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.}}
// ^^^^^^^^
Expand Down

0 comments on commit 2ead440

Please sign in to comment.