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

Don't emit CA1002 when type is sealed #6729

Open
wants to merge 2 commits 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
Expand Up @@ -47,7 +47,8 @@ public override void Initialize(AnalysisContext context)
var field = (IFieldSymbol)context.Symbol;

// FxCop compat: only analyze externally visible symbols by default.
if (!context.Options.MatchesConfiguredVisibility(Rule, field, context.Compilation))
if (!context.Options.MatchesConfiguredVisibility(Rule, field, context.Compilation) ||
field.ContainingType is { TypeKind: TypeKind.Class, IsSealed: true })
{
return;
}
Expand All @@ -65,7 +66,8 @@ public override void Initialize(AnalysisContext context)
var property = (IPropertySymbol)context.Symbol;

if (property.IsOverride ||
property.IsImplementationOfAnyInterfaceMember())
property.IsImplementationOfAnyInterfaceMember() ||
property.ContainingType is { TypeKind: TypeKind.Class, IsSealed: true })
{
return;
}
Expand Down Expand Up @@ -95,7 +97,8 @@ public override void Initialize(AnalysisContext context)
}

if (methodSymbol.IsOverride ||
methodSymbol.IsImplementationOfAnyInterfaceMember())
methodSymbol.IsImplementationOfAnyInterfaceMember() ||
methodSymbol.ContainingType is { TypeKind: TypeKind.Class, IsSealed: true })
{
return;
}
Expand Down
Expand Up @@ -2,6 +2,7 @@

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<
Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotExposeGenericLists,
Expand Down Expand Up @@ -361,6 +362,24 @@ End Class
}.RunAsync();
}

[Theory, WorkItem(6626, "https://github.com/dotnet/roslyn-analyzers/issues/6626")]
[InlineData("public List<int> List { get; set; }")]
[InlineData("public List<string> _list;")]
[InlineData("public static List<byte> List;")]
[InlineData("public readonly List<float> _list;")]
[InlineData("public readonly List<int> _list;")]
[InlineData("public List<char> GetList() => throw null;")]
[InlineData("public void ConsumeList(List<double> list) {}")]
public Task SealedClass_NoDiagnostic(string code)
{
return VerifyCS.VerifyAnalyzerAsync(@$"
using System.Collections.Generic;

public sealed class Test {{
{code}
}}");
}

private static DiagnosticResult GetCSharpExpectedResult(int line, int col, string returnTypeName, string typeDotMemberName)
#pragma warning disable RS0030 // Do not use banned APIs
=> VerifyCS.Diagnostic()
Expand Down