Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does using += on a nullable type result in a FORWARD_NULL defect

No doubt there are other, perhaps better ways to do this, but I'm trying to understand what is going on here.

In the below example, coverity is reporting a FORWARD_NULL defect on the fourth line.

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

it reports:

assign_zero: Assigning: foo = null.

on the foo += bar line.

in += Operator (C# Reference), I see that x += y is equivalent to x = x + y, and in Using nullable types (C+ Programming Guide), I see that

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

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

like image 390
Michael J. Avatar asked Mar 08 '19 15:03

Michael J.


1 Answers

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

Yes.

like image 51
spodger Avatar answered Oct 11 '22 17:10

spodger