Skip to content

Analyzer complaining about not disposing object (False positives) #5199

@roywill

Description

@roywill
    <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>

Describe the bug

var item = new ItemData(scannedFileName: filePath);
return parseChannel.Writer.WriteAsync(item);

The class ItemData is indeed IDisposable and WriteAsync can throw, so yep I would an IDisposable error, which there is.

Changing to:

So, to address the issue I change that to this.

var item = new ItemData(scannedFileName: filePath);
try
{
var parseTask = parseChannel.Writer.WriteAsync(item);
item = null;
return parseTask;
}
finally
{
item?.Dispose();
}

Only results in:

Severity Code Description Project File Line Suppression State Tool
Warning CA1508 'item' is always 'null'. Remove or refactor the condition(s) to avoid dead code. ItemCheck ... 636 Active Microsoft.CodeAnalysis.NetAnalyzers
Severity Code Description Project File Line Suppression State Tool
Warning CA2000 Call System.IDisposable.Dispose on object created by 'new ItemData(scannedFileName: filePath)' before all references to it are out of scope ItemCheck ... 627 Active Microsoft.CodeAnalysis.NetAnalyzers

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Microsoft.CodeAnalysis.NetAnalyzersBugThe product is not behaving according to its current intended designDataFlowFalse_PositiveA diagnostic is reported for non-problematic casehelp wantedThe issue is up-for-grabs, and can be claimed by commenting

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions