Roslyn-analyzers: No warning for undisposed stream in C# UWP code

Created on 23 Mar 2018  路  5Comments  路  Source: dotnet/roslyn-analyzers

I originally reported this here https://github.com/MicrosoftDocs/visualstudio-docs/issues/734#issuecomment-375765639 and was asked to post it here.

Repro steps

Put this code in a C# UWP app

{
    var tempFolder = ApplicationData.Current.TemporaryFolder;
    var filename = Guid.NewGuid().ToString();
    var tmpFile = await tempFolder.CreateFileAsync(filename, CreationCollisionOption.GenerateUniqueName);

    // Expect a warning that stream is not disposed
    var stream = await tmpFile.OpenAsync(FileAccessMode.ReadWrite);
    var dw = new DataWriter(stream);
    dw.WriteString("New file");
    // stream.Dispose();
}

Run code analysis with all rules turned on.

Expected behavior

Warning CA2000 should be emitted

https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2000-dispose-objects-before-losing-scope

Actual behavior

No warning.

0 - Backlog Area-Microsoft.CodeQuality.Analyzers Bug DataFlow

All 5 comments

Thank you @phraemer. I will investigate.

@phraemer Do you happen to remember if there was a throw on your method?

@mavasani I have created a UWP application and tried the code snippet mentioned and I have noticed that the issue was correctly raised. Although, doing some more tests, I have noticed that if there is an unconditional throw statement in the method then the analysis is stopped and no issue is reported (regardless of the type of the project). This looks like a limitation of the CFG or dataflow but I will need to investigate a bit more. Shall we close this ticket and create a new one for the issue with the throw?

@Evangelink I don't think there was a throw. However, I just tried the FxCop analyzers since the binary analyzers seem to have been deprecated and it picks up the lack of a call to dispose. So from my point of view we could consider this closed.

Thank you for the feedback.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paulomorgado picture paulomorgado  路  3Comments

paulomorgado picture paulomorgado  路  3Comments

NtFreX picture NtFreX  路  3Comments

sharwell picture sharwell  路  4Comments

x3ntrix picture x3ntrix  路  3Comments