question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IDE0063 "using statement can be simplified" code fix results in invalid code (build error)

See original GitHub issue

Version 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:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
CyrusNajmabadicommented, Dec 6, 2021

@Youssef1313 that doesn’t apply here.

When gotoing a label in the same statement list as a using var, the goto must not have a using var syntactically between the goto and the label.

The label is not in the same statement list as the using var. 😃

0reactions
Youssef1313commented, Jan 20, 2022

@sharwell This was stated as a compiler issue while #39637 is an IDE issue. Can you reopen?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found