Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper yanks its own tail; Is it right the first time or the last?

I had some verbose code:

private bool AnyUnselectedCombox()
{
    bool anyUnselected = false;
    foreach (Control c in this.Controls)
    {
        if (c is ComboBox)
        {
            if ((c as ComboBox).SelectedIndex == -1)
            {
                anyUnselected = true;
                break;
            }
        } 
    }
    return anyUnselected;
}

...that Resharper offered to elegantize with a LINQ expression like so:

return this.Controls.OfType<ComboBox>().Any(c => (c as ComboBox).SelectedIndex == -1);

...but the subsequent Resharper inspection says about the code it generated (above): "Type cast is redundant" (referring to the "c as ComboBox" part), so that it ends up as:

return this.Controls.OfType<ComboBox>().Any(c => c.SelectedIndex == -1);

Shouldn't Resharper generate Resharper-approved code? Or does it simply sometimes need two passes for it to fully "gird up its loins"?

like image 759
B. Clay Shannon-B. Crow Raven Avatar asked Sep 26 '12 20:09

B. Clay Shannon-B. Crow Raven


People also ask

How does ReSharper help?

ReSharper helps instantly get to any code in a solution, no matter how large the solution is. It can also navigate you from any symbol to its related code such as implementations of a given interface, extension methods of a class, or usages of a field.

How make ReSharper faster?

Stale caches If you have recently updated ReSharper and observe performance degradation with solutions that were opened with previous versions, you can attempt to speed thing up by clearing the ReSharper caches and deleting the solution .


1 Answers

Yes, sometimes ReSharper corrects itself, requiring a second pass to get it "just right". I've always assumed it uses certain "safe templates" to do the conversion and in some cases some parts of the safe transformation aren't really needed.

All versions of the code are correct and equivalent though, the first "pass" is converting to Linq and the second "pass" removes some redundant code that the Linq transformation added.

like image 135
Joachim Isaksson Avatar answered Nov 02 '22 01:11

Joachim Isaksson