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

ConfigureAwait diagnostic + code fix for IAsyncEnumerable (and other types) #3703

Open
jnm2 opened this issue Jun 1, 2020 · 12 comments · May be fixed by #5377
Open

ConfigureAwait diagnostic + code fix for IAsyncEnumerable (and other types) #3703

jnm2 opened this issue Jun 1, 2020 · 12 comments · May be fixed by #5377
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design help wanted The issue is up-for-grabs, and can be claimed by commenting
Milestone

Comments

@jnm2
Copy link
Contributor

jnm2 commented Jun 1, 2020

Could the existing ConfigureAwait refactoring be loosened to offer this refactoring on any type that has a .ConfigureAwait(bool) instance or extension method in scope?

IAsyncEnumerable and IAsyncDisposable are two that were recently added to the BCL. It seems like all future types and user-defined types could be treated uniformly.

Analyzer package

Microsoft.CodeAnalysis.FxCopAnalyzers

Package Version

v3.0.0 (Latest)

Diagnostic ID

Example: CA2007

@mavasani
Copy link
Member

mavasani commented Jun 1, 2020

Sounds reasonable. I would want an affirmation from @AArnott and @stephentoub before proceeding.

@stephentoub
Copy link
Member

Seems ok. I expect it won't be as easy as treating types "uniformly", as the constructs here vary, e.g. await task.ConfigureAwait(false); vs await using (disposable.ConfigureAwait(false)) vs await foreach (var item in source.ConfigureAwait(false)), but the crux of the suggestion is fine.

@jnm2
Copy link
Contributor Author

jnm2 commented Jun 2, 2020

That's a good point. Treating uniformly doesn't make sense. I would specifically ask for code fixes just the two BCL types then. IAsyncDisposable in particular is a pain to manually refactor to start consuming with ConfigureAwait(false).

@sharwell
Copy link
Member

sharwell commented Mar 9, 2021

IAsyncDisposable support was filed separately as #4888 and fixed in #4896

@genlu genlu added Enhancement Bug The product is not behaving according to its current intended design Area-Microsoft.CodeAnalysis.NetAnalyzers help wanted The issue is up-for-grabs, and can be claimed by commenting and removed Enhancement labels Mar 9, 2021
@genlu genlu added this to the .NET 6.x milestone Mar 9, 2021
@MartyIX
Copy link
Contributor

MartyIX commented Aug 9, 2021

.NET 6.0 (the latest released preview) shows me CA2007 warnings for lines like:

await using DatabaseStorage storage = /* warning starts here */new(testPath, this.storageSettings, this.shutdown, this.storageLock, this.storageSerializer, nameof(this.CheckKeyExistsAsync)); /* warning ends here */

How can I actually fix it? Or is it an analyzer bug?

Hopefully, it's relevant here.

@jnm2
Copy link
Contributor Author

jnm2 commented Aug 9, 2021

These are our two options currently, and I've been doing the first one.

var storage = new DatabaseStorage(testPath, this.storageSettings, this.shutdown, this.storageLock, this.storageSerializer, nameof(this.CheckKeyExistsAsync))
await using (storage.ConfigureAwait(false))
{
    // ...
var storage = new DatabaseStorage(testPath, this.storageSettings, this.shutdown, this.storageLock, this.storageSerializer, nameof(this.CheckKeyExistsAsync))
await using var _1 = storage.ConfigureAwait(false);

// ...

See dotnet/csharplang#2235 which requests the ability to do await using _ = expr; or similar.

@MartyIX
Copy link
Contributor

MartyIX commented Aug 9, 2021

@jnm2 Thanks!

@MartyIX
Copy link
Contributor

MartyIX commented Aug 9, 2021

Hm, using the first approach, I get CA2000 instead. 😞

@jnm2
Copy link
Contributor Author

jnm2 commented Aug 9, 2021

I turn off CA2000 because there are too many false positives for me in solid situations that are just hard for the analyzer to follow.

@MartyIX
Copy link
Contributor

MartyIX commented Aug 9, 2021

(I like CA2000 even though it would be great to have some attributes specifying owners of objects or something. It helps me find bugs from time to time.)

@manfred-brands
Copy link
Contributor

@genlu Still looking for help to implement this?

@genlu
Copy link
Member

genlu commented Aug 12, 2021

@manfred-brands Absolutely, any contribute is welcomed!

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 help wanted The issue is up-for-grabs, and can be claimed by commenting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants