【问题标题】:Is there unreachable code in this snippet? I don't think so, but Resharper is telling me otherwise此代码段中是否有无法访问的代码?我不这么认为,但 Resharper 告诉我不是这样
【发布时间】:2011-02-21 00:07:54
【问题描述】:

我在代码审查中遇到了以下方法。在循环内部,Resharper 告诉我 if (narrativefound == false) 不正确,因为 narrativeFound 总是正确的。我不认为是这种情况,因为为了将narrativeFound 设置为true,它必须首先通过条件字符串比较,那么它怎么可能总是正确的呢?我错过了什么吗?这是 Resharper 中的错误还是我们的代码中的错误?

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; 
}

【问题讨论】:

  • 对我来说似乎触手可及。为它编写一个测试用例并放入您的测试工具中以查看您是否可以将其用于代码覆盖可能是一件好事。你确实有测试,对吧? :)
  • 我从来没有发现一个 resharper “代码无法访问”是不正确的。尽管有时我花了一些认真的思考才能找出原因。我并不是说您应该始终只信任 resharper 并删除它告诉您可以安全删除的任何内容,但不要贸然下结论它不正确。这段代码的调试步骤会告诉你发生了什么。
  • 似乎这里的错误在于解释错误代码,似乎“Extraneous code”错误会比“Unreachable code”更有帮助。

标签: c# .net visual-studio .net-3.5 resharper


【解决方案1】:

当控制到达该语句时,变量narrativefound 永远不会为真:

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

我认为 Resharper 试图告诉你条件 narrativefound == false 将永远为真。

【讨论】:

  • 完全正确 - 条件 将始终为真; narrativeFound 永远是假的。
  • 好眼光。我自己在第一次传球时就错过了。
  • 我误解了 R# 告诉我的内容。感谢您的精彩回答!
【解决方案2】:

您根本不需要narrativeFound 变量。在您将其设置为 true 的范围内,您将打破 foreach 循环。如果不设置为true,则不会中断,并将newNarrative 添加到newNarrativeList

所以,这可以改写为

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

            newNarrativeList.Add(newNarrative);                 
     }
}

【讨论】:

    【解决方案3】:

    这是您代码中的错误。

    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);                             
                          } 
    // narrativeFound == true, but now we exit the for loop
                          break; 
                } 
    // narrativeFound is always false here.  The test is redundant
                if (narrativefound == false) 
                { 
                     newNarrativeList.Add(newNarrative);  
                } 
         } 
    } 
    

    【讨论】:

      【解决方案4】:

      R# 是正确的,因为如果您将narrativefound 设置为true,您将在设置后立即脱离foreach。

      【讨论】:

        【解决方案5】:

        我相信它告诉你,因为如果 narriativefound 设置为 true,则退出 for 循环 (break;)。因此,如果 if (narriativefound == false) 被评估,它将始终具有 false 值。

        【讨论】:

          猜你喜欢
          • 2022-06-10
          • 1970-01-01
          • 1970-01-01
          • 2020-08-27
          • 1970-01-01
          • 1970-01-01
          • 2021-06-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多