IDE0063 "using statement can be simplified" code fix results in invalid code (build error)
See original GitHub issueVersion Used: Visual Studio 17.0.1
Steps to Reproduce:
Paste the following code:
using System;
using System.Collections.Generic;
static void Repro()
{
{
// Any branch Roslyn can't predict is fine
if (new Random().Next() > 0)
{
goto End;
}
using (var _ = new List<int>().GetEnumerator())
{
}
}
End:
return;
}
Expected Behavior:
Nothing should happen and no code fixes should be proposed, this code is fine.
Actual Behavior:
IntelliSense suggests that the “using statement can be simplified”, and applying the code fix rewrites that using
declaration into a using
statement. This results in a build error though on that goto End
statement, with error CS8648 saying “a goto cannot jump to a location after a using declaration”.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
c# - When does IDE0063 dispose?
IDE tells me I can simplify this using statement by writing this instead: void Method() { using (var client = new Client()); ...
Read more >IDE0063: Use simple 'using' statement - .NET
Learn about code analysis rule IDE0063, which recommends a 'using' declaration instead of a 'using' statement.
Read more >using statement - ensure the correct use of disposable ...
Use the C# using statement or declaration to ensure the correct use of disposable objects.
Read more >Using, using, using with C# 8 - csharp.christiannagel.com
With the new C# 8 using declaration, the code with the using statement can be simplified. Curly brackets are no longer needed.
Read more >C# 8 – Making Use of Using Declarations
To fix this, make sure your using declaration is placed in the method that uses the class. You can still refactor your declaration...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Youssef1313 that doesn’t apply here.
The label is not in the same statement list as the using var. 😃
@sharwell This was stated as a compiler issue while #39637 is an IDE issue. Can you reopen?