Unnecessary assignment incorrectly marks the variable as unnecessary
See original GitHub issueVersion Used: VS 2019 16.8.0 Preview 2.1
Steps to Reproduce:
Have code like the following open in VS:
int F(bool condition)
{
int variable = 0;
if (condition)
variable = 1;
else
variable = 2;
return variable;
}
Expected Behavior: IDE0059 “Unnecessary assignment of a value” highlights the assignment of 0 to variable
as unnecessary:
int variable[| = 0|];
Actual Behavior:
The variable itself is highlighted as unnecessary by IDE0059, indicating that the variable should be removed, not just the assignment:
int [|variable|] = 0;
If you actually run the code fix, it does the right thing, i.e. it only removes the assignment (and also the empty line, for some reason):
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
IDE0059 Unnecessary assignment of a vlue to 'variable' · ...
This results in a compilation error, because the variable a is not assigned prior to its use in the call to bar ....
Read more >c# - Unnecessary assignment of variables
First things first. The biggest problem here is that you log stuff in logger constructor. That is not what constructor should be doing....
Read more >IDE0059: Remove unnecessary value assignment - .NET
Overview · If the expression on the right side of the assignment has no side effects, remove the expression or the entire assignment...
Read more >VS underlining variable saying unnecessary assignment of ...
So the line is string response = string.empty; And VS is just giving me a warning saying what I mentioned in the title....
Read more >no-unused-vars - ESLint - Pluggable JavaScript Linter
A variable is not considered to be used if it is only ever declared ( var foo = 5 ) or assigned to...
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
I believe this is by design. Note that by attempting to fade some other location, we are trading one problem for another.
= expr
, the expression on right might even span across multiple lines, so fading the entire equals value clause is also not really desirable. Additionally, user might feel the evaluation ofexpr
is unnecessary, which is not always true as evaluatingexpr
might have side effects. The code fix will not suggesting removing the assignment for such a case, but replace the left side with a discard.=
, it will hardly be visible, if at all. Additionally, I am not sure how do we handle the unnecessary assignment for an out variable like below:The current approach of fading the left hand side is designed to indicate an
unnecessary write
orunnecessary store
, not anunnecessary variable
. I don’t believe fading any other location would help here.https://github.com/dotnet/roslyn/blob/e16d3906a6d6a560da72144a942dbac7c8dc2e11/src/Analyzers/CSharp/Analyzers/RemoveUnusedParametersAndValues/CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer.cs#L62
Probably this will be a good start in debugging. I’ll give it a try soon.