Bug 6225 – Some common null test mistakes

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-06-29T17:03:56Z
Last change time
2022-08-15T14:38:32Z
Keywords
diagnostic
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2011-06-29T17:03:56Z
In the following four cases of "if" I think it's better to receive warnings from the compiler, because the code probably contains mistakes (this compiles with no errors on DMD 2.053): struct Foo { int x; bool foo() { return true; } } void main() { Foo* p; int* arr = (new int[5]).ptr; if (p != null || p.x) {} if (p == null && p.foo()) {} if (!p && p.foo()) {} if (arr == null && arr[3]) {} }
Comment #1 by razvan.nitu1305 — 2022-08-15T14:38:32Z
This is more suited for a third party library. Also, the compiler will need to do dataflow analysis to understand these cases.