为什么在可空类型上使用+ =会导致FORWARD_NULL缺陷

问题描述 投票:5回答:1

毫无疑问,还有其他的,或许更好的方法可以做到这一点,但我试图了解这里发生了什么。

在下面的示例中,Coverity在第四行报告FORWARD_NULL缺陷。

double? foo = null;
double bar = 1.23;
foo += bar;
System.Windows.Point point = new System.Windows.Point(foo,bar);  

它报道:

assign_zero:分配:foo = null。

在foo + = bar线上。

+= Operator (C# Reference)中,我看到x + = y等于x = x + y,而在Using nullable types (C+ Programming Guide),我看到

如果一个或两个操作数为空,则这些运算符[二元运算符]产生空值

那是怎么回事? foo + = bar变成foo = foo + bar,因为foo为null,foo + bar为null?

c# nullable static-analysis coverity
1个回答
5
投票

那是怎么回事? foo + = bar变成foo = foo + bar,因为foo为null,foo + bar为null?

是。

© www.soinside.com 2019 - 2024. All rights reserved.