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

CA2000 emitted when await using (false positive) #3042

Closed
DrGriff opened this issue Nov 14, 2019 · 12 comments
Closed

CA2000 emitted when await using (false positive) #3042

DrGriff opened this issue Nov 14, 2019 · 12 comments
Assignees
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design DataFlow

Comments

@DrGriff
Copy link

DrGriff commented Nov 14, 2019

Analyzer package

Microsoft.CodeAnalysis.FxCopAnalyzers

Package Version

v2.9.7 (Latest)

Diagnostic ID

Example: CA2000

Repro steps

  1. .Net Core 3.1 (latest preview). I happen to be using Blazor Server.
  2. Create a Service class that accepts a DbContextOptions
  3. Instantate a DbContext in a method. Use the "await using..." C#8 syntax
public class MyService
{
	public DocTypesService(DbContextOptions<MyDbContext> options)
	{
		this.options = options;
	}

	private readonly DbContextOptions<MyDbContext> options;

	public async Task<Something> GetSomethingAsync(string id, CancellationToken token)
	{
		await using var context = new DocTypesContext(this.options);
			....
	}
}

Expected behavior

await using var context = new DocTypesContext(this.options);

The context is created with a using statement, and will be disposed of when it goes out of scope and so no CA2000 error should not be emitted.

Actual behavior

await using var context = new DocTypesContext(this.options);

A CA2000 error is emitted.

However, were we to drop the await command using var context = new DocTypesContext(this.options); then the error is not emitted, but we really do need to put the await in.

@mscrivo
Copy link

mscrivo commented Dec 3, 2019

Seeing the same behaviour after updating to VS 2019 16.4. No way to fix, other than suppress the issue.

@Kharos
Copy link

Kharos commented Dec 4, 2019

Same here. For teams like ours who use code analysis and warnings as errors (we consider both "best practice"), this means that our existing solution is no longer building after upgrading Visual Studio. Not really a great way of introducing a new .net release 🐞

Edit: Some quick tests show that the error only seems to happen when using a class that inherits from a class that implements IDisposable and IAsyncDisposable. When the used class directly implements IDisposable and IAsyncDisposable, the analyzer works as expected.

@DrGriff
Copy link
Author

DrGriff commented Dec 5, 2019

Similarly to @Kharos, we're also setting all warnings to Errors when compiling in Release mode.

It is a bit naff having to wrap the "offending" lines with pragma warning disable comments.

#pragma warning disable CA2000 // Dispose objects before losing scope. Justification: False positive, see #3042
await using var context = new MyContext(this.options);
#pragma warning restore CA2000 // Dispose objects before losing scope

mavasani added a commit to mavasani/roslyn-analyzers that referenced this issue Dec 5, 2019
@mavasani mavasani self-assigned this Dec 5, 2019
@mavasani mavasani added this to the vNext milestone Dec 5, 2019
@mavasani mavasani added the Bug The product is not behaving according to its current intended design label Dec 5, 2019
@mavasani
Copy link
Contributor

mavasani commented Dec 5, 2019

#3098 should fix this.

@mavasani
Copy link
Contributor

Fixed with #3098. I will post the beta package that you can try out to verify in a bit.

@mavasani
Copy link
Contributor

@DenSmoke
Copy link

DenSmoke commented Feb 6, 2020

https://dotnet.myget.org/feed/roslyn-analyzers/package/nuget/Microsoft.CodeAnalysis.FxCopAnalyzers/2.9.9-beta1.19610.1+906398e7 should have the fix.

When will the fix be available in release build? This issue is really annoying.

@Yanal-Yves
Copy link

@DenSmoke, I have Just upgraded my project to "Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0" and I don't have the issue anymore.

@jmarolf jmarolf modified the milestones: vNext, .NET 5 Preview 8 Aug 12, 2020
@GeeWee
Copy link

GeeWee commented Sep 1, 2020

I still have it on 3.3.0 with the following code

await using var newMemoryStream = new MemoryStream();

@TaffarelJr
Copy link

I'm getting the false positives on MemoryStream as well as GZipStream.

I'm using <EnableNETAnalyzers>true</EnableNETAnalyzers> instead of FxCop, in a library targeting .NET Standard 2.1.

@daviddunson
Copy link

Please re-open this bug. I'm using VS 17.0.6 and experiencing the same problem with this code:

var lockStream = new FileStream(lockPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

await using (lockStream.ConfigureAwait(false))
{
}

"new FileStream" triggers CA2000.

@cytoph
Copy link

cytoph commented Jul 28, 2022

Yeah, the issue still exists. Please re-open this bug! It's super annoying that such a common feature is not handled correctly by the analyzers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design DataFlow
Projects
None yet
Development

No branches or pull requests