Skip to content

Commit ba955a4

Browse files
committed
Ensure that CA2213 (DisposableFieldsShouldBeDisposed) is executed on generated code
Fixes #2306
1 parent 819d724 commit ba955a4

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/Microsoft.NetCore.Analyzers/Core/Runtime/DisposableFieldsShouldBeDisposed.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public sealed class DisposableFieldsShouldBeDisposed : DiagnosticAnalyzer
3939
public override void Initialize(AnalysisContext context)
4040
{
4141
context.EnableConcurrentExecution();
42-
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
42+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze);
4343

4444
context.RegisterCompilationStartAction(compilationContext =>
4545
{

src/Microsoft.NetCore.Analyzers/UnitTests/Runtime/DisposableFieldsShouldBeDisposedTests.cs

+53
Original file line numberDiff line numberDiff line change
@@ -2745,5 +2745,58 @@ public void Dispose()
27452745
}
27462746
");
27472747
}
2748+
2749+
[Fact, WorkItem(2306, "https://github.com/dotnet/roslyn-analyzers/issues/2306")]
2750+
public void DisposableAllocationInConstructor_DisposedInGeneratedCodeFile_NoDiagnostic()
2751+
{
2752+
VerifyCSharp(@"
2753+
using System;
2754+
2755+
class A : IDisposable
2756+
{
2757+
public void Dispose()
2758+
{
2759+
}
2760+
}
2761+
2762+
class B : IDisposable
2763+
{
2764+
private readonly A a;
2765+
public B()
2766+
{
2767+
a = new A();
2768+
}
2769+
2770+
[System.CodeDom.Compiler.GeneratedCodeAttribute("""", """")]
2771+
public void Dispose()
2772+
{
2773+
a.Dispose();
2774+
}
2775+
}
2776+
");
2777+
2778+
VerifyBasic(@"
2779+
Imports System
2780+
2781+
Class A
2782+
Implements IDisposable
2783+
Public Sub Dispose() Implements IDisposable.Dispose
2784+
End Sub
2785+
End Class
2786+
2787+
Class B
2788+
Implements IDisposable
2789+
2790+
Private ReadOnly a As A
2791+
Sub New()
2792+
a = New A()
2793+
End Sub
2794+
2795+
<System.CodeDom.Compiler.GeneratedCodeAttribute("""", """")> _
2796+
Public Sub Dispose() Implements IDisposable.Dispose
2797+
a.Dispose()
2798+
End Sub
2799+
End Class");
2800+
}
27482801
}
27492802
}

0 commit comments

Comments
 (0)