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.

Unnecessary assignment incorrectly marks the variable as unnecessary

See original GitHub issue

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

github_iconTop GitHub Comments

1reaction
mavasanicommented, Sep 17, 2020

I believe this is by design. Note that by attempting to fade some other location, we are trading one problem for another.

  1. If we decide to fade the entire = 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 of expr is unnecessary, which is not always true as evaluating expr 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.
  2. If we decide to fade only the =, 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: image

The current approach of fading the left hand side is designed to indicate an unnecessary write or unnecessary store, not an unnecessary variable. I don’t believe fading any other location would help here.

Read more comments on GitHub >

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

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