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

Yet another CA2000 false positive. #7510

Open
JJLovesLife opened this issue Dec 17, 2024 · 0 comments
Open

Yet another CA2000 false positive. #7510

JJLovesLife opened this issue Dec 17, 2024 · 0 comments

Comments

@JJLovesLife
Copy link

Analyzer

Diagnostic ID: CA2000: Dispose objects before losing scope

Analyzer source

SDK: Built-in CA analyzers in .NET 5 SDK or later

Version: SDK 9.0.101

Describe the bug

Looks like there are quite a lot of issues relate to CA2000, not sure if this one is reported, and if the root cause is the same. But here is another repro, I guess.
Func is necessary, and the line of code inside is also necessary, delete neither the warning is gone.

foreach (var i in Enumerable.Range(0, 10))
{
	using var reader = new StreamReader(string.Empty); // <-- CA2000 reported here
	Func(reader);
}

static void Func(TextReader param)
{
	using var reader = new StreamReader(string.Empty);
}

Steps To Reproduce

See above.

Expected behavior

No CA2000 warning.

Actual behavior

Program.cs(3,21): warning CA2000: Use recommended dispose pattern to ensure that object created by 'new StreamReader(string.Empty)' is 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 transfered to another object or method, assign 'null' to the local variable just after such an operation to prevent double dispose in 'finally'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Additional context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant