S2930: Dispose when 'item' is no longer needed is not recognized coming from factory methods

We have following observation:

  • C#
  • S2930
  • IDisposables created by static factory methods should be reported as S2930.
  • We are using: Data Center Edition * Version 10.1 (build 73491)

Following code produces a S2930:

FileStream s = new FileStream("D:\\TEST", FileMode.Open);

whereas this code does not report S2930:

FileStream s = File.OpenRead("D:\\TEST");

Hello @msedi,

Thank you for reporting this issue!

S2930 is indeed not reporting in this example. I confirm this as an FN and I have opened a ticket in our repository

To give some info, it is stated in the rule description, that we cover a predefined list of factory method that we check:

  • System.IO.File.Create()
  • System.IO.File.Open()
  • System.Drawing.Image.FromFile()
  • System.Drawing.Image.FromStream()

Since File.OpenRead is not part of the list, it is “expected” for S2930 to not report.

Have a nice end of the day!

1 Like

Thanks for the feedback. I have also opened a request on the dotnet/runtime to mark methods explicitly so that these things can be discovered: [API Proposal]: Possibility to check if a return value needs to be disposed. · Issue #94832 · dotnet/runtime (github.com)

1 Like