dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.23k stars 1.57k forks source link

"dead code" warning for short circuiting isn't complete #29196

Open MichaelRFairhurst opened 7 years ago

MichaelRFairhurst commented 7 years ago

In trivial cases its pretty easy to see what atually is/isn't dead code, in part because of the partial warning. But in more complicated cases, it may create confusion that the "dead code" section does not highlight all of the dead code (in mixtures of dead and live code, etc).

repro.dart:

main() {                                                                         
  return false && 1 == 2 && 3 == 4;                                              
}

What happens:

1 == 2 is highlighted as dead code because its after false && so won't do anything.

What I expect to happen:

1 == 2 && 3 == 4 is highlighted, because that entire section is dead, not just the 1 == 2.

srawlins commented 6 years ago

Yeah the dead code checking in binary logical expressions is very limited. In particular, the _getConstantBooleanValue method has a big chunk of text commented out with no explanation other than:

Don't consider situations where we could evaluate to a constant boolean expression with the ConstantVisitor

But why not? Performance? I'm not sure that the/a ConstantVisitor would reconcile this, but I shouldn't think it would be hard...

srawlins commented 6 years ago

In addition to number literals, boolean literals should also be included in this request. Basically https://github.com/dart-lang/sdk/issues/3511:

while (true) {
  if (false) break;
}