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

标签 c# nullable static-analysis coverity

毫无疑问,还有其他可能更好的方法可以做到这一点,但我正在努力了解这里发生了什么。

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

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

它报告:

assign_zero: Assigning: foo = null.

在 foo += bar 行上。

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

These operators [the binary operator] produce a null value if one or both operands are null

这是怎么回事? foo += bar 变为 foo = foo + bar 并且由于 foo 为空,foo + bar 为空?

最佳答案

so is that what is going on? foo += bar becomes foo = foo + bar and since foo is null, foo + bar is null?

是的。

关于c# - 为什么在可空类型上使用 += 会导致 FORWARD_NULL 缺陷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55066002/

相关文章:

c# - 从 Controller 继承并重定向

c# - RedisClient.Get<T> C# 与 ServiceStack.Redis 的性能

c# - 可空字段不能接受空值?

java - 哪种 Java 静态分析工具最容易扩展?

c# - 了解不安全代码及其用途

c# - `lock`(监视器)在 .NET 中如何工作?

kotlin - 是否有一种优雅的 kotlin 方式可以让编译器相信我刚刚为其分配了真实值的可空字段不能再为空?

c# - 来自 DatePicker 的可空日期时间值

java - 如何查明某些代码是否与某个 Android API 级别兼容

java - JSR 305 注释可以与在 Android 上运行的代码一起使用吗?