'var' 在至少一个执行路径上为 null - sonarqube 还是它?

'var' is null on at least one execution path - sonarqube or is it?

在使用 sonarqube 分析我的代码时,我在以下代码(foreach 循环)中遇到了 'variableProducerAgreements' is null on at least one execution path

然而,在查看它并尝试各种操作后,variableProducerAgreements 似乎在这个 foreach 循环中永远不会为 null。在代码审查期间,我被告知它可以 "totally" 为 null 并且应该添加条件逻辑来处理它。但我不明白它怎么可以为空,因此不确定如何添加条件。有什么想法吗?

我看不出 variableProducerAgreements 可以为 null 的方法,因为你在顶部设置了 null 守卫(假设你的 [=31= 中没有任何疯狂代码) ]吸气剂)。

if (userProfile?.ProducerProfile == null)
    return result;

.NET 中的 WhereFindAll 方法不会 return null。

但是,每次访问 ProducerProfile 时使用 null-conditional 可能会使某些工具和人员感到困惑。如果它为 null,您会 return 早点删除它们,因此您应该删除它们:

if (IsActingAsDelegate && userProfile.ProducerProfile.IsInactiveProducer)
{
    variableProducerAgreements = userProfile.ProducerProfile.ProducerAgreements.FindAll(ag => ag.IsActive && ag.IsVariableBranchContract);
}
else
{
    variableProducerAgreements = userProfile.ProducerProfile.ActiveAgreements.Where(a => a.IsVariableContract);
}

如果在 if 语句之前有办法让它为 null,那么当您访问 IsInactiveProducer 属性.

时,您也会面临 NullReferenceException 的风险

此外,审稿人应该能够解释 his/her 推理。