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

RCS1261: "Resource can be disposed asynchronously" does not take lock statement into account #1372

Closed
NoahStolk opened this issue Jan 22, 2024 · 0 comments · Fixed by #1374
Closed
Assignees

Comments

@NoahStolk
Copy link

Product and Version Used:

Roslynator.Analyzers 4.9.0

Steps to Reproduce:

Consider the following code:

object fileStreamLock = new();

await SomeAsyncMethod();

async Task SomeAsyncMethod()
{
	lock (fileStreamLock)
	{
		using FileStream fs = new("test.txt", FileMode.OpenOrCreate);
		// Do something...
	}

	await Task.Yield(); // For demonstration purposes
}

RCS1261 is reported, saying that the FileStream can be disposed asynchronously, but doing so results in compiler error CS1996 (Cannot 'await' in the body of a 'lock' statement). This is not valid C#:

lock (fileStreamLock)
{
        await using FileStream fs = new("test.txt", FileMode.OpenOrCreate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants