'Fix' of unassigned local variable is bad

In 216 - when presented with a warning regarding an unused local variable
- e.g.:

public void Test()
{
foreach(MyClass thing in things)
{
int result = thing.DoSomething();
}
}

There is a warning that the variable 'result' isn't used - the auto-fix seems
to be to remove the line completely which is NOT desirable - what would be
desirable would be to remove the 'int result = ' - otherwise the (maybe important)
operation happening inside 'thing.DoSomething()' would be lost.

Ronnie


2 comments
Comment actions Permalink

Hello Ronnie,

We will provide two different quick fixes for such situation. One removes
the right part, the other leaves it.

In 216 - when presented with a warning regarding an unused local
variable - e.g.:

public void Test()
{
foreach(MyClass thing in things)
{
int result = thing.DoSomething();
}
}
There is a warning that the variable 'result' isn't used - the
auto-fix seems to be to remove the line completely which is NOT
desirable - what would be desirable would be to remove the 'int result
= ' - otherwise the (maybe important) operation happening inside
'thing.DoSomething()' would be lost.

Ronnie

Thanks,
Andrey Simanovsky


0
Comment actions Permalink

Hello Andrey,

That sounds great - which will be the default (first in the list) - presumably
the 'safer' one (ie leaving the right hand side).

Ronnie

Hello Ronnie,

We will provide two different quick fixes for such situation. One
removes the right part, the other leaves it.

>> In 216 - when presented with a warning regarding an unused local
>> variable - e.g.:
>>
>> public void Test()
>> {
>> foreach(MyClass thing in things)
>> {
>> int result = thing.DoSomething();
>> }
>> }
>> There is a warning that the variable 'result' isn't used - the
>> auto-fix seems to be to remove the line completely which is NOT
>> desirable - what would be desirable would be to remove the 'int
>> result
>> = ' - otherwise the (maybe important) operation happening inside
>> 'thing.DoSomething()' would be lost.
>> Ronnie
>>

Thanks,
Andrey Simanovsky



0

Please sign in to leave a comment.