Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDE0068 False Positive #39443

Closed
jmolinelli opened this issue Oct 22, 2019 · 10 comments
Closed

IDE0068 False Positive #39443

jmolinelli opened this issue Oct 22, 2019 · 10 comments
Assignees
Labels
Area-IDE Bug IDE-CodeStyle Built-in analyzers, fixes, and refactorings Resolution-By Design The behavior reported in the issue matches the current design
Milestone

Comments

@jmolinelli
Copy link

jmolinelli commented Oct 22, 2019

Version Used:
VS19 Community
Steps to Reproduce:
Code example:

 if (this.TryOpenReader(sourceName, out TextReader reader))
            {
                using (reader)
                {
                var m = new Model();
                Importer.Import(m, reader);
                return m;
                }
            }

where the TryOpenReader retruns true if it is successful in opening a reader, false otherwise.

Expected Behavior:
Would NOT expect to get an IDE0068 message.
Actual Behavior:
Get an IDE0068 Message on the out variable. Tried a couple of approaches, but none seem to clear it.

@jspinella
Copy link

What specific version of VS 2019 Community are you running (Help -> About Microsoft Visual Studio -> Version 16.2.1)?

It would also help to have the full code (method definition for TryOpenReader? Where does Importer come from?)

@Grace3579
Copy link

try using var instead

if (this.TryOpenReader(sourceName, out var reader))
{
using (reader)
{
var m = new Model();
Importer.Import(m, reader);
return m;
}
}

@jmolinelli
Copy link
Author

jmolinelli commented Oct 22, 2019 via email

@jmolinelli
Copy link
Author

jmolinelli commented Oct 22, 2019 via email

@huoyaoyuan
Copy link
Member

please note that I didn't get this message on VS17

Just because the message was recently added in VS2019. (16.2 or 16.3)

I choose to turn of it totally; the disposable analysis is totally problematic without concept of ownership in language.

@jinujoseph jinujoseph added Area-IDE Bug IDE-CodeStyle Built-in analyzers, fixes, and refactorings labels Oct 23, 2019
@jinujoseph jinujoseph added this to the 16.5.P1 milestone Oct 23, 2019
@mavasani mavasani modified the milestones: 16.5.P1, Backlog Oct 23, 2019
@mavasani
Copy link
Contributor

IDE0068 is a suggestion to refactor your code to write it in such a way that the disposable object is disposed on all paths, including exceptional code paths:

<value>Use recommended dispose pattern to ensure that locally scoped disposable objects are disposed on all paths. If possible, wrap the creation within a 'using' statement or a 'using' declaration. Otherwise, use a try-finally pattern, with a dedicated local variable declared before the try region and an unconditional Dispose invocation on non-null value in the 'finally' region, say 'x?.Dispose()'. If the object is explicitly disposed within the try region or the dispose ownership is transferred to another object or method, assign 'null' to the local variable just after such an operation to prevent double dispose in 'finally'</value>

         TextReader reader = null;
         try
         {
            if (this.TryOpenReader(sourceName, out reader))
            {
                var m = new Model();
                Importer.Import(m, reader);
                return m;
            }
         }
         finally
         {
              reader?.Dispose();
         }

@jmolinelli
Copy link
Author

jmolinelli commented Oct 24, 2019 via email

@mavasani
Copy link
Contributor

mavasani commented Oct 25, 2019

It seems to me the disposable analysis and associated flag (IDE0068) could
be very useful so I am trying to leave it on.

Note that the dispose analyzers have been turned off by default in VS2019 16.4 due to some performance issues on larger solutions. You can turn them on explicitly by using the editorconfig entries mentioned here: #38984 (comment)

I know how to write the code without the "using" approach but the code I
tried seems like it should work so I am hoping to find out whether the
False Positive is wrong and if it isn't', whether there is any solution
using the "using" construct that is valid.

Your code will most likely be fine on normal program execution code paths, but the recommended implementation in #39443 (comment) will safe guard you in case of exceptions. Assume the below code for TryOpenReader:

   bool TryOpenReader(string sourceName, out TextReader reader)
   {
       reader = new ...

      // ... Some code which can throw an exception

      return true;
   }

@mavasani mavasani added the Resolution-By Design The behavior reported in the issue matches the current design label Oct 25, 2019
@adam-frisby
Copy link

Is there any way the ergonomics of this could be improved in future language versions by allowing a using statement in the method call?

this.TryOpenReader(sourceName, using out TextReader reader)

@mavasani
Copy link
Contributor

@pix64 - that would need to be filed as a separate compiler feature request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Bug IDE-CodeStyle Built-in analyzers, fixes, and refactorings Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

7 participants