-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Comments
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?) |
try using var instead if (this.TryOpenReader(sourceName, out var reader)) |
Version is 16.3.5
requested code is below. Please note this Message did not occur in VS17
with the exact same code -- I just upgraded to VS19 and this warning came
up.
Per an email from Grace3759, I tried var instead of out TextReader reader
and am still getting the same false positive.
Thanks
jack
private bool TryOpenreader(string sourceName, out TextReader reader)
{
TextReader qr;
reader = null;
switch (sourceName)
{
case "Default File":
qr = new StreamReader(Terms.QIFFilePathName);
break;
case "Clipboard":
var qText = Clipboard.GetText();
if (string.IsNullOrEmpty(qText))
{
this.WarningDialog(this.Response(IssueId.MissingQIFData));
return false;
}
qr = new StringReader(qText);
break;
default:
this.ErrorDialog(this.Response(IssueId.UnknownEntity,
Resources.QIFDataSource, sourceName));
return false;
}
var firstToken = qr.Peek();
if (firstToken == -1 || (char)firstToken != '!')
{
this.WarningDialog(this.Response(IssueId.IllegalQIFFormat));
return false;
}
reader = qr;
return true;
}
…On Tue, Oct 22, 2019 at 2:59 PM James Spinella ***@***.***> wrote:
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?)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#39443?email_source=notifications&email_token=ABKMDJBQWFWDVYNR447Y4ALQP5ERFA5CNFSM4JDSYON2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEB62NUA#issuecomment-545105616>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABKMDJHR7EV5OIRPAEGCZZDQP5ERFANCNFSM4JDSYONQ>
.
--
be well
jack
|
I tried var and still get the false positive Message. Also, please note
that I didn't get this message on VS17 -- just upgraded to VS19 16.3.5.
…On Tue, Oct 22, 2019 at 4:59 PM Grace3579 ***@***.***> wrote:
try using var instead
if (this.TryOpenReader(sourceName, out var reader))
{
using (reader)
{
var m = new Model();
Importer.Import(m, reader);
return m;
}
}
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#39443?email_source=notifications&email_token=ABKMDJAKRFOJK5R2C5IUK2LQP5SUJA5CNFSM4JDSYON2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEB7F3QQ#issuecomment-545152450>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABKMDJHAVNAIORFUOBOECMLQP5SUJANCNFSM4JDSYONQ>
.
--
be well
jack
|
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. |
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:
TextReader reader = null;
try
{
if (this.TryOpenReader(sourceName, out reader))
{
var m = new Model();
Importer.Import(m, reader);
return m;
}
}
finally
{
reader?.Dispose();
} |
Thank you for your comments.
It seems to me the disposable analysis and associated flag (IDE0068) could
be very useful so I am trying to leave it on.
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.
Thanks again.
Be well
jack
…On Wed, Oct 23, 2019 at 2:55 PM Huo Yaoyuan ***@***.***> wrote:
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.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#39443?email_source=notifications&email_token=ABKMDJAOLD5JNPAC6ND76B3QQCMYJA5CNFSM4JDSYON2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECCPZ3I#issuecomment-545586413>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABKMDJAJZUALH7VHY3WNULDQQCMYJANCNFSM4JDSYONQ>
.
--
be well
jack
|
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)
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 bool TryOpenReader(string sourceName, out TextReader reader)
{
reader = new ...
// ... Some code which can throw an exception
return true;
} |
Is there any way the ergonomics of this could be improved in future language versions by allowing a using statement in the method call?
|
@pix64 - that would need to be filed as a separate compiler feature request. |
Version Used:
VS19 Community
Steps to Reproduce:
Code example:
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.
The text was updated successfully, but these errors were encountered: