Skip to content

Commit

Permalink
CA1861: Do not warn if not array initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
buyaa-n committed Jun 26, 2023
1 parent f4cc17e commit 340f69d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public override void Initialize(AnalysisContext context)
}
// Must be literal array
if (arrayCreationOperation.Initializer is { } initializer &&
if (arrayCreationOperation.Initializer is not { } initializer ||
initializer.ElementValues.Any(x => x is not ILiteralOperation))
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,28 +806,34 @@ private void M(string eventName, string msg)
}

[Fact, WorkItem(6686, "https://github.com/dotnet/roslyn-analyzers/issues/6686")]
public Task ArrayWithoutInitializer_Diagnostic()
public Task ArrayWithoutInitializer_NoDiagnostic()
{
var source = @"using System.Collections.Generic;
return new VerifyCS.Test
{
TestCode = @"using System.Collections.Generic;
public class MyClass
{
public List<object> Cases => new() { {|CA1861:new object[0]|} };
}";
var fixedSource = @"using System.Collections.Generic;
public List<object> Cases => new() { new object[0] };
}",
LanguageVersion = LanguageVersion.CSharp10
}.RunAsync();
}

[Fact, WorkItem(6686, "https://github.com/dotnet/roslyn-analyzers/issues/6697")]
public async Task ArrayWithoutInitializer_NoDiagnostic2()
{
await VerifyCS.VerifyAnalyzerAsync(@"
using System;
public class MyClass
{
public List<object> Cases => new() { item };
private static readonly object[] item = new object[0];
}";
return new VerifyCS.Test
{
TestCode = source,
FixedCode = fixedSource,
LanguageVersion = LanguageVersion.CSharp10
}.RunAsync();
public void M1(Type[] types) { }
public void M2(int length)
{
M1(new Type[length]);
}
}");
}
}
}

0 comments on commit 340f69d

Please sign in to comment.