Skip to content

IDE0018 wanting to inline variable breaks compilation #40650

Open
@vsfeedback

Description

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Area-IDEBugDeveloper CommunityThe issue was originally reported on https://developercommunity.visualstudio.comFeature - IDE0018Inline declarationFeature - Pattern MatchingPattern MatchingIDE-CodeStyleBuilt-in analyzers, fixes, and refactoringshelp wantedThe issue is "up for grabs" - add a comment if you are interested in working on it

    Type

    No type

    Projects

    • Status

      InQueue

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions