Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Meziantou.Analyzer/Rules/ClassMustBeSealedAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -113,9 +113,11 @@ private bool IsPotentialSealed(AnalyzerOptions options, INamedTypeSymbol symbol,
if (symbol.GetMembers().Any(member => member.IsVirtual) && !SealedClassWithVirtualMember(options, symbol))
return false;

if (symbol.IsVisibleOutsideOfAssembly() && !PublicClassShouldBeSealed(options, symbol))
var canBeInheritedOutsideOfAssembly = symbol.IsVisibleOutsideOfAssembly() && symbol.GetMembers().OfType<IMethodSymbol>().Where(member => member.MethodKind is MethodKind.Constructor).Any(member => member.IsVisibleOutsideOfAssembly());
if (canBeInheritedOutsideOfAssembly && !PublicClassShouldBeSealed(options, symbol))
return false;


return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Threading.Tasks;
using System.Threading.Tasks;
using Meziantou.Analyzer.Rules;
using TestHelper;
using Xunit;
Expand Down Expand Up @@ -256,6 +256,72 @@ internal sealed record Sample();
.ValidateAsync();
}

[Theory]
[InlineData("private")]
[InlineData("internal")]
[InlineData("private protected")]
public async Task ClassWithPrivateCtor(string visibility)
{
await CreateProjectBuilder()
.WithSourceCode($$"""
public class [||]Sample
{
{{visibility}} Sample() { }
}
""")
.ValidateAsync();
}

[Theory]
[InlineData("private")]
[InlineData("internal")]
[InlineData("private protected")]
public async Task ClassWithMultiplePrivateCtors(string visibility)
{
await CreateProjectBuilder()
.WithSourceCode($$"""
public class [||]Sample
{
private Sample(int a) { }
{{visibility}} Sample() { }
}
""")
.ValidateAsync();
}

[Theory]
[InlineData("public")]
[InlineData("protected")]
[InlineData("protected internal")]
public async Task ClassWithPublicCtor(string visibility)
{
await CreateProjectBuilder()
.WithSourceCode($$"""
public class Sample
{
{{visibility}} Sample() { }
}
""")
.ValidateAsync();
}

[Theory]
[InlineData("public")]
[InlineData("protected")]
[InlineData("protected internal")]
public async Task ClassWithPrivateAndPublicCtor(string visibility)
{
await CreateProjectBuilder()
.WithSourceCode($$"""
public class Sample
{
private Sample(int a) { }
{{visibility}} Sample() { }
}
""")
.ValidateAsync();
}

#if CSHARP10_OR_GREATER
[Fact]
public async Task TopLevelStatement_10()
Expand Down