diff --git a/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidUninstantiatedInternalClasses.cs b/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidUninstantiatedInternalClasses.cs index 614889bd70..5ac90531cd 100644 --- a/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidUninstantiatedInternalClasses.cs +++ b/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidUninstantiatedInternalClasses.cs @@ -48,6 +48,7 @@ public sealed override void Initialize(AnalysisContext context) var internalTypes = new ConcurrentDictionary(); var compilation = startContext.Compilation; + var entryPointContainingType = compilation.GetEntryPoint(startContext.CancellationToken)?.ContainingType; var wellKnownTypeProvider = WellKnownTypeProvider.GetOrCreate(compilation); // If the assembly being built by this compilation exposes its internals to @@ -98,7 +99,8 @@ public sealed override void Initialize(AnalysisContext context) { var type = (INamedTypeSymbol)context.Symbol; if (!type.IsExternallyVisible() && - !IsOkToBeUninstantiated(type, compilation, + !IsOkToBeUninstantiated(type, + entryPointContainingType, systemAttributeSymbol, iConfigurationSectionHandlerSymbol, configurationSectionSymbol, @@ -282,7 +284,7 @@ private bool HasInstantiatedNestedType(INamedTypeSymbol type, IEnumerable m is IMethodSymbol) - .Cast() - .Any(m => IsEntryPoint(m, taskSymbol, genericTaskSymbol)); - } - - private static bool IsEntryPoint(IMethodSymbol method, ITypeSymbol? taskSymbol, ITypeSymbol? genericTaskSymbol) - { - if (!method.IsStatic) - { - return false; - } - - if (!IsSupportedReturnType(method, taskSymbol, genericTaskSymbol)) - { - return false; - } - - if (!method.Parameters.Any()) - { - return true; - } - - if (method.Parameters.HasMoreThan(1)) - { - return false; - } - - return true; - } - - private static bool IsSupportedReturnType(IMethodSymbol method, ITypeSymbol? taskSymbol, ITypeSymbol? genericTaskSymbol) - { - if (method.ReturnType.SpecialType == SpecialType.System_Int32) - { - return true; - } - - if (method.ReturnsVoid) - { - return true; - } - - if (taskSymbol != null && Equals(method.ReturnType, taskSymbol)) - { - return true; - } - - if (genericTaskSymbol != null && Equals(method.ReturnType.OriginalDefinition, genericTaskSymbol) && ((INamedTypeSymbol)method.ReturnType).TypeArguments.Single().SpecialType == SpecialType.System_Int32) - { - return true; - } - - return false; - } - /// /// If a type is passed a generic argument to another type or a method that specifies that the type must have a constructor, /// we presume that the method will be constructing the type, and add it to the list of instantiated types. diff --git a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidUninstantiatedInternalClassesTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidUninstantiatedInternalClassesTests.cs index d876245601..a658ab01bd 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidUninstantiatedInternalClassesTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidUninstantiatedInternalClassesTests.cs @@ -366,7 +366,7 @@ End Sub } [Fact] - public async Task CA1812_Basic_NoDiagnostic_MainMethodIsDifferentlyCasedAsync() + public async Task CA1812_Basic_Diagnostic_MainMethodIsDifferentlyCasedAsync() { await new VerifyVB.Test { @@ -375,7 +375,7 @@ public async Task CA1812_Basic_NoDiagnostic_MainMethodIsDifferentlyCasedAsync() OutputKind = OutputKind.ConsoleApplication, Sources = { -@"Friend Class C +@"Friend Class [|C|] Private Shared Sub mAiN() End Sub End Class",