2

I have some code that I think should be generating an 'assigned but its value is never used' warning:

public void SomeFunction(ISomeInterface instance)
{
    ISomeInterface localReference = instance;
    //... localReference is never mentioned again
}

However, it is not generating a warning at all. The same occurs with creation inside the function:

public void SomeFunction()
{
    ISomeInterface localReference = new SomeInterfaceImplementation();
    //..localReference never mentioned again
}

Neither of these generate a warning, whereas a warning is generated for j here:

public void SomeFunction(ISomeInterface instance, int i)
{
    int j = o;
    ISomeInterface localReference = instance;
    //... neither localReference nor j is mentioned again
}

Why don't the unused objects generate a warning? Even if the assignment has side effects (possibly from an implicit cast for the first example), the variable itself is still not used and surely should generate the warning?

leppie
  • 115,091
  • 17
  • 196
  • 297

0 Answers0