IDE0018 wanting to inline variable breaks compilation #40650
Open
Description
opened on Dec 30, 2019
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>"First"</span> => dict.TryGetValue(<span class=hljs-number>1</span>, <span class=hljs-keyword>out</span> price), <span class=hljs-string>"Second"</span> => dict.TryGetValue(<span class=hljs-number>2</span>, <span class=hljs-keyword>out</span> price), _ => 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>"First"</span> => 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>"Second"</span> => dict.TryGetValue(<span class=hljs-number>2</span>, <span class=hljs-keyword>out</span> price), <span class=hljs-comment>// CS0103</span> _ => 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)
Metadata
Assignees
Labels
Type
Projects
Status
InQueue
Activity