yoya / js2-mode

Automatically exported from code.google.com/p/js2-mode
0 stars 0 forks source link

Possible spurious "code has no side effects" warning #66

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Note: I initially added this as a comment in the Discussion wiki page.

The following code gives a Code has no side effects warning:

function foo() {
  while (bar) {
    function baz(x) {
      return function() {
        x.qux();
      };
    }; // warning underlined here

    window.setTimeout(baz(o), 0);
  }
};

However, this code gives no warning:

function foo() {
  while (bar) {
    var baz = function(x) {
      return function() {
        x.qux();
      };
    };

    window.setTimeout(baz(o), 0);
  }
};

Original issue reported on code.google.com by huntermo...@gmail.com on 30 Apr 2008 at 1:02

GoogleCodeExporter commented 8 years ago
I've got another one of those -- "m[2]||0" on the last line of the below code 
causes
the "Code has no side effect" warning:

Date.parseDuration = function(str) {
    /* Converts an iCalendar (ISO8601 except year and month values) duration to a
number of milliseconds. */
    var m;
    if (m =
str.match(/^\s*([+-]?)P(?:(\d+)W)?(?:(\d+)D)?T?(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?
\s*$/)) {
        return (m[1] === '-' ? -1000 : 1000) * (parseInt(m[6]||0,10) +
                                                60 * (parseInt(m[5]||0,10) +
                                                      60 * (parseInt(m[4]||0,10) +
                                                            24 * (parseInt(m[3]||0,10) +
                                                                  7 *
parseInt((m[2]||0,10))))));
    }
};

Original comment by andreasl...@gmail.com on 11 Jun 2008 at 8:36

GoogleCodeExporter commented 8 years ago
The first example is fixed already.

Andreas's example is fixed for the next release.
I'm not sure why Rhino checks expressions for side-effects; the check at the
statement level seems sufficient.  I've removed the former and kept the latter, 
and
it fixes this.

Original comment by steve.ye...@gmail.com on 16 Jun 2008 at 6:06

GoogleCodeExporter commented 8 years ago

Original comment by steve.ye...@gmail.com on 16 Jun 2008 at 8:04

GoogleCodeExporter commented 8 years ago
Whoops, wait a second. I didn't see the extra set of parentheses way down there:

  parseInt((m[2]||0,10))

That comma is actually interpreted as the C-like comma operator that takes two
expressions and returns the second one, so (m[2]||0,10) will always evaluate to 
10,
no matter what m[2] is.

Please keep that warning after all!

Original comment by andreasl...@gmail.com on 16 Jun 2008 at 10:05