Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark type as instantiated when used with a collection expression initializer #7229

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Analyzer.Utilities.Lightup;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
Expand Down Expand Up @@ -78,12 +79,11 @@ public sealed override void Initialize(AnalysisContext context)

startContext.RegisterOperationAction(context =>
{
var expr = (IObjectCreationOperation)context.Operation;
if (expr.Type is INamedTypeSymbol namedType && namedType.ContainingAssembly.Equals(context.ContainingSymbol.ContainingAssembly))
if (context.Operation.Type is INamedTypeSymbol namedType && namedType.ContainingAssembly.Equals(context.ContainingSymbol.ContainingAssembly))
{
instantiatedTypes.TryAdd(namedType, null);
}
}, OperationKind.ObjectCreation);
}, OperationKind.ObjectCreation, OperationKindEx.CollectionExpression);

startContext.RegisterSymbolAction(context =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;

Expand Down Expand Up @@ -1656,6 +1657,47 @@ internal class {|#0:C|} { }";
await test.RunAsync();
}

[Fact, WorkItem(7223, "https://github.com/dotnet/roslyn-analyzers/issues/7223")]
public Task CA1812_CollectionExpressionsInField_NoDiagnostic()
{
const string code = """
using System.Collections.ObjectModel;

public class Class1
{
private readonly MyCollection _items = [];
}
internal sealed class MyCollection : Collection<int>;
""";
return new VerifyCS.Test
{
TestCode = code,
LanguageVersion = LanguageVersion.CSharp12
}.RunAsync();
}

[Fact, WorkItem(7223, "https://github.com/dotnet/roslyn-analyzers/issues/7223")]
public Task CA1812_CollectionExpressionsInLocalVariable_NoDiagnostic()
{
const string code = """
using System.Collections.ObjectModel;

public class Class1
{
private void M()
{
MyCollection c = [];
}
}
internal sealed class MyCollection : Collection<int>;
""";
return new VerifyCS.Test
{
TestCode = code,
LanguageVersion = LanguageVersion.CSharp12
}.RunAsync();
}

private static DiagnosticResult GetCSharpResultAt(int line, int column, string className)
#pragma warning disable RS0030 // Do not use banned APIs
=> VerifyCS.Diagnostic()
Expand Down