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

IDE0018 wanting to inline variable breaks compilation #40650

Closed
vsfeedback opened this issue Dec 30, 2019 · 3 comments · Fixed by #76822
Closed

IDE0018 wanting to inline variable breaks compilation #40650

vsfeedback opened this issue Dec 30, 2019 · 3 comments · Fixed by #76822
Labels
Area-IDE Bug Developer Community The issue was originally reported on https://developercommunity.visualstudio.com Feature - IDE0018 Inline declaration Feature - Pattern Matching Pattern Matching help wanted The issue is "up for grabs" - add a comment if you are interested in working on it IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Milestone

Comments

@vsfeedback
Copy link

This issue has been moved from a ticket on Developer Community.


Hence the following compilable code:

private static int Main(string[] args)
{
    Dictionary<int, int> dict = new Dictionary<int, int> { /* ... */ };
<span class=hljs-keyword>int</span> price; <span class=hljs-comment>// IDE0018</span>
<span class=hljs-keyword>bool</span> found = args[<span class=hljs-number>0</span>] <span class=hljs-keyword>switch</span>
{
    <span class=hljs-string>&quot;First&quot;</span> =&gt; dict.TryGetValue(<span class=hljs-number>1</span>, <span class=hljs-keyword>out</span> price),
    <span class=hljs-string>&quot;Second&quot;</span> =&gt; dict.TryGetValue(<span class=hljs-number>2</span>, <span class=hljs-keyword>out</span> price),
    _ =&gt; dict.TryGetValue(<span class=hljs-number>3</span>, <span class=hljs-keyword>out</span> price)
};

<span class=hljs-keyword>return</span> found ? <span class=hljs-number>-1</span> : price;

}

This will suggest IDE: 0018: Variable declaration can be inlined as marked by the comment above, suggesting to inline price to the first out variable:

private static int Main(string[] args)
{
    Dictionary<int, int> dict = new Dictionary<int, int> { /* ... */ };
<span class=hljs-keyword>bool</span> found = args[<span class=hljs-number>0</span>] <span class=hljs-keyword>switch</span>
{
    <span class=hljs-string>&quot;First&quot;</span> =&gt; dict.TryGetValue(<span class=hljs-number>1</span>, <span class=hljs-keyword>out</span> <span class=hljs-keyword>int</span> price), <span class=hljs-comment>// IDE0059</span>
    <span class=hljs-string>&quot;Second&quot;</span> =&gt; dict.TryGetValue(<span class=hljs-number>2</span>, <span class=hljs-keyword>out</span> price), <span class=hljs-comment>// CS0103</span>
    _ =&gt; dict.TryGetValue(<span class=hljs-number>3</span>, <span class=hljs-keyword>out</span> price) <span class=hljs-comment>// CS0103</span>
};

<span class=hljs-keyword>return</span> found ? <span class=hljs-number>-1</span> : price; <span class=hljs-comment>// CS0103</span>

}

This code does not compile due to multiple issues (all marked above):

  • 1x warning IDE0059: Unnecessary assignment of a value to 'price’
  • 3x error CS0103: The name ‘price’ does not exist in the current context

I don’t expect suggestions to break code when applied, so I’m reporting this as a bug.


Original Comments

Visual Studio Feedback System on 12/29/2019, 10:15 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.


Original Solutions

(no solutions)

@sharwell sharwell added Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it IDE-CodeStyle Built-in analyzers, fixes, and refactorings labels Dec 30, 2019
@sharwell sharwell added this to the 16.5 milestone Dec 30, 2019
@sharwell sharwell added Feature - Pattern Matching Pattern Matching Developer Community The issue was originally reported on https://developercommunity.visualstudio.com labels Dec 30, 2019
@zaytsev-victor
Copy link
Contributor

First case:

        private static int Main(string[] args)
        {
            Dictionary<int, int> dict = new Dictionary<int, int> { /* ... */ };
            int price; // IDE0018 
            bool found = args[0] switch 
            { 
                "First" => dict.TryGetValue(1, out price), 
                "Second" => dict.TryGetValue(2, out price), 
                _ => dict.TryGetValue(3, out price) 
            }; 
            
            return found ? -1 : price;
        }

Second case:

        private static int Main(string[] args)
        {
            Dictionary<int, int> dict = new Dictionary<int, int> { /* ... */ };
            bool found = args[0] switch
            {
                "First" => dict.TryGetValue(1, out int price), // IDE0059 
                "Second" => dict.TryGetValue(2, out price), // CS0103 
                _ => dict.TryGetValue(3, out price) // CS0103 
            }; 
            
            return found ? -1 : price; // CS0103
        }

@RayKoopa
Copy link

RayKoopa commented Jan 9, 2020

Thanks for adding that. I didn't see the import here messed with my samples!

@jinujoseph jinujoseph modified the milestones: 16.5, Backlog Jan 14, 2020
@zspitz
Copy link

zspitz commented May 8, 2021

Same thing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Bug Developer Community The issue was originally reported on https://developercommunity.visualstudio.com Feature - IDE0018 Inline declaration Feature - Pattern Matching Pattern Matching help wanted The issue is "up for grabs" - add a comment if you are interested in working on it IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Projects
Status: Completed
8 participants