Skip to content

Commit

Permalink
Fix analyzer UseExplicitlyOrImplicitlyTypedArray (RCS1014)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Jan 28, 2022
1 parent f4e0d53 commit 004a837
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,21 @@ private static void AnalyzeArrayCreationExpression(SyntaxNodeAnalysisContext con
if (!expressions.Any())
return;

ITypeSymbol typeSymbol = null;

if (kind == ArrayCreationTypeStyle.ImplicitWhenTypeIsObvious)
{
foreach (ExpressionSyntax expression in expressions)
{
if (!CSharpTypeAnalysis.IsTypeObvious(expression, null, context.SemanticModel, context.CancellationToken))
if (typeSymbol == null)
{
typeSymbol = context.SemanticModel.GetTypeSymbol(arrayCreation.Type.ElementType, context.CancellationToken);

if (typeSymbol?.IsErrorType() != false)
return;
}

if (!CSharpTypeAnalysis.IsTypeObvious(expression, typeSymbol, context.SemanticModel, context.CancellationToken))
return;
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/CSharp/CSharp/CSharpTypeAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,20 @@ public static bool IsTypeObvious(ExpressionSyntax expression, ITypeSymbol typeSy
case SyntaxKind.CharacterLiteralExpression:
case SyntaxKind.TrueLiteralExpression:
case SyntaxKind.FalseLiteralExpression:
case SyntaxKind.ThisExpression:
{
return true;
}
case SyntaxKind.ObjectCreationExpression:
case SyntaxKind.ArrayCreationExpression:
case SyntaxKind.CastExpression:
case SyntaxKind.AsExpression:
case SyntaxKind.ThisExpression:
case SyntaxKind.DefaultExpression:
{
return true;
return typeSymbol == null
|| SymbolEqualityComparer.IncludeNullability.Equals(
typeSymbol,
semanticModel.GetTypeSymbol(expression, cancellationToken));
}
case SyntaxKind.ImplicitArrayCreationExpression:
{
Expand Down
42 changes: 20 additions & 22 deletions src/Tests/Analyzers.Tests/RCS1014UseImplicitlyTypedArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ void M()
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseExplicitlyOrImplicitlyTypedArray)]
public async Task Test_TypesAreNotEqual()
{
await VerifyDiagnosticAndFixAsync(@"
await VerifyNoDiagnosticAsync(@"
class C : A
{
void M()
{
var x = new [|A|][]
var x = new A[]
{
default(B),
default(C)
Expand All @@ -102,26 +102,6 @@ class A
{
}
class B : A
{
}
", @"
class C : A
{
void M()
{
var x = new[]
{
default(B),
(A)default(C)
};
}
}
class A
{
}
class B : A
{
}
Expand Down Expand Up @@ -183,5 +163,23 @@ void M()
}
", options: Options.AddConfigOption(ConfigOptionKeys.ArrayCreationTypeStyle, ConfigOptionValues.ArrayCreationTypeStyle_Implicit));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseExplicitlyOrImplicitlyTypedArray)]
public async Task TestNoDiagnostic_ArrayInitializer_DerivedType()
{
await VerifyNoDiagnosticAsync(@"
class C
{
void M()
{
var items = new object[]
{
new double[] { 1d },
new double[] { 1d }
};
}
}
", options: Options.AddConfigOption(ConfigOptionKeys.ArrayCreationTypeStyle, ConfigOptionValues.ArrayCreationTypeStyle_ImplicitWhenTypeIsObvious));
}
}
}

0 comments on commit 004a837

Please sign in to comment.