Bug 4558 – To spot a possible bug in code that doesn't change a value

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-08-01T14:20:33Z
Last change time
2022-08-15T12:00:41Z
Keywords
diagnostic
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-08-01T14:20:33Z
This is a semantically wrong D2 program, the programmer has used a "&=" instead of "|=" to build a flag result: enum Flags { NONE = 0b00, FIRST = 0b01, SECOND = 0b10 } Flags foo(int x) { Flags result = Flags.NONE; if (x < 10) result |= Flags.FIRST; else result &= Flags.SECOND; // line 10 return result; } void main() {} The D compiler can find this problem in the code at line 10 because in that program the variable "result" is certantly zero after the assignment with Flags.SECOND, so it's an error or it's useless code.
Comment #1 by lt.infiltrator — 2017-06-10T06:54:07Z
So, if I understand correctly, you want the compiler to statically calculate results where all the inputs are known at compile-time and detect when no change has occurred in a "&=" (and presumably in other operator-assignment operators?) In such cases, should the compiler raise a warning or an error? This all fits in nicely with CTFE.
Comment #2 by razvan.nitu1305 — 2022-08-15T12:00:41Z
Walter has repeatedly stated that dmd-fe does not and will not do dataflow analysis. The problem with these sort of examples is that they seem trivial to implement for the simple cases, but once you start to think about more complex scenarios, the implementation becomes very complicated very fast. I will close this as WONTFIX.