Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there unreachable code in this snippet? I don't think so, but Resharper is telling me otherwise

I have the following method I came across in a code review. Inside the loop Resharper is telling me that if (narrativefound == false) is incorrect becuase narrativeFound is always true. I don't think this is the case, because in order to set narrativeFound to true it has to pass the conditional string compare first, so how can it always be true? Am I missing something? Is this a bug in Resharper or in our code?

public Chassis GetChassisForElcomp(SPPA.Domain.ChassisData.Chassis asMaintained, SPPA.Domain.ChassisData.Chassis newChassis)
{
    Chassis c = asMaintained;
    List<Narrative> newNarrativeList = new List<Narrative>();

    foreach (Narrative newNarrative in newChassis.Narratives)
    {
         bool narrativefound = false; 

         foreach (Narrative orig in asMaintained.Narratives)
         {
                if (string.Compare(orig.PCode, newNarrative.PCode) ==0 )
                {
                          narrativefound = true;
                          if (newNarrative.NarrativeValue.Trim().Length != 0)
                          {
                             orig.NarrativeValue = newNarrative.NarrativeValue;
                             newNarrativeList.Add(orig);                            
                          }
                          break;
                }
                if (narrativefound == false)
                {
                     newNarrativeList.Add(newNarrative); 
                }
         }
    }

    c.SalesCodes = newChassis.SalesCodes;
    c.Narratives = newNarrativeList;
    return c; 
}
like image 918
The Matt Avatar asked May 18 '10 16:05

The Matt


1 Answers

The variable narrativefound will never be true when control reaches that statement:

narrativefound = true;
// ...
break;  // This causes control to break out of the loop.

I think Resharper is trying to tell you that the condition narrativefound == false will always be true.

like image 160
Mark Byers Avatar answered Oct 05 '22 13:10

Mark Byers