Sonar qube 循环迭代

问题描述 投票:0回答:2

我正在使用 SonarQube 来分析代码。我有一种方法如下。在这里,我已经检查了非空条件。

if (myentities == null && !myentities.Any())
{
    yield return default;
}
else
{
    foreach (var entity in myentities)
    {

        yield return entity.PrepareMyResponse();
    }
}

当我运行 SonarQube 时,它指向 if 条件并将其显示为错误。消息是“'myentities' 在至少一个执行路径上为空。”。如何解决这个错误?

c# asp.net-core-mvc sonarqube sonarqube-scan
2个回答
0
投票

我的经验是它更喜欢这个——合并支票并丢失

else
...

if (myentities?.Any() != true)
{
    yield return default;
}

foreach (var entity in myentities)
{

        yield return entity.PrepareMyResponse();
}

0
投票

循环中暂停循环的要求是什么,如果为null则不赋值继续循环

foreach (var entity in myentities)
{
      if (entity is null) continue;

      yield return entity.PrepareMyResponse();
}
© www.soinside.com 2019 - 2024. All rights reserved.