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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.FlowAnalysis.DataFlow;
using Microsoft.CodeAnalysis.FlowAnalysis.DataFlow.DisposeAnalysis;
using Microsoft.CodeAnalysis.FlowAnalysis.DataFlow.PointsToAnalysis;
using Microsoft.CodeAnalysis.Operations;

namespace Microsoft.NetCore.Analyzers.Runtime
{
Expand Down Expand Up @@ -139,7 +140,7 @@ public override void Initialize(AnalysisContext context)
return;
}

if (disposeAnalysisResult.ControlFlowGraph.OriginalOperation.HasAnyOperationDescendant(o => o.Kind == OperationKind.None))
if (disposeAnalysisResult.ControlFlowGraph.OriginalOperation.HasAnyOperationDescendant(o => o.Kind == OperationKind.None && !(o.Parent is INameOfOperation)))
{
// Workaround for https://github.com/dotnet/roslyn/issues/32100
// Bail out in presence of OperationKind.None - not implemented IOperation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11920,5 +11920,41 @@ End Sub
}
}.RunAsync();
}

[Fact, WorkItem(3297, "https://github.com/dotnet/roslyn-analyzers/issues/3297")]
public async Task NameOfInsideTheScope_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(@"
using System;

class A : IDisposable
{
public void Dispose()
{

}
}

class Test
{
void M1()
{
var a = new A();
}

void M2()
{
var a = new A();
var b = nameof(Test);
}
}
",
// Test0.cs(16,17): warning CA2000: Call System.IDisposable.Dispose on object created by 'new A()' before all references to it are out of scope.
GetCSharpResultAt(16, 17, "new A()"),

// Test0.cs(21,17): warning CA2000: Call System.IDisposable.Dispose on object created by 'new A()' before all references to it are out of scope.
GetCSharpResultAt(21, 17, "new A()")
);
}
}
}