google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.4k stars 1.15k forks source link

Warn when 'break;' is missing in a switch statement #223

Open blickly opened 10 years ago

blickly commented 10 years ago

This issue was imported from Closure Compiler's previous home at http://closure-compiler.googlecode.com

The original discussion is archived at: http://blickly.github.io/closure-compiler-issues/#1104

MatrixFrog commented 9 years ago

I threw together a simple patch for this one that just checked for a break/return/throw at the end of a case, and it warned on a few cases like this:

switch (foo) {
  case 1:
    if (bar) {
      return x;
    } else {
      return y;
    }
  default: ...
}

I think to do this properly we probably want to look at the control flow graph.

concavelenz commented 9 years ago

I think we need to do this CFG analysis when optimizing switch statements, you might want to take a look there.