D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6225 - Some common null test mistakes
Summary: Some common null test mistakes
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2011-06-29 17:03 UTC by bearophile_hugs
Modified: 2022-08-15 14:38 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-06-29 17:03:56 UTC
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 RazvanN 2022-08-15 14:38:32 UTC
This is more suited for a third party library. Also, the compiler will need to do dataflow analysis to understand these cases.