public class Test
{
public long M(bool flag)
{
long a = 0; // IDE0059
long b = 0; // IDE0059
long c = 0; // IDE0059
if (flag)
{
a = 1;
b = 1;
c = 1;
}
else
{
a = 2;
b = 2;
c = 2;
}
return a + b + c;
}
}
Run "Remove unnecessary assignment" for c
Run "Remove unnecessary assignment" for b
Expected Behavior:
In both steps, the = 0 characters should be removed
long a = 0; // IDE0059
- long b = 0; // IDE0059
- long c = 0; // IDE0059
+ long b; // IDE0059
+ long c; // IDE0059
if (flag)
Actual Behavior:
After running "Remove unnecessary assignment" for c, the empty line below the declaration and the comment is also removed:
long a = 0; // IDE0059
long b = 0; // IDE0059
- long c = 0; // IDE0059
-
+ long c;
if (flag)
After then running "Remove unnecessary assignment" for b , the b and c declarations are reordered and the comment is removed:
long a = 0; // IDE0059
- long b = 0; // IDE0059
- long c = 0; // IDE0059
-
+ long c;
+ long b;
if (flag)
Design guidance: Don't reorder variable declarations within same group of declarations.
Originally there was no reordering with this codefix, but over time we noticed the action was almost always followed by needing to move the declaration nearer the first necessary assignment block. We want to preserve this behavior only if the declaration is moving to a different block. If the codefix would only cause a reordering within the same block then it should not be applied.
Version Used: 16.6.2
Steps to Reproduce:
c
b
Expected Behavior:
In both steps, the
= 0
characters should be removedActual Behavior:
After running "Remove unnecessary assignment" for
c
, the empty line below the declaration and the comment is also removed:After then running "Remove unnecessary assignment" for
b
, theb
andc
declarations are reordered and the comment is removed: