Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 351470

Summary: [validation] The local variable is never read
Product: [WebTools] JSDT Reporter: Victor Homyakov <vkhomyackov>
Component: GeneralAssignee: Project Inbox <jsdt.javascript-inbox>
Status: NEW --- QA Contact: Chris Jaun <cmjaun>
Severity: normal    
Priority: P3 CC: alex-pub.eclipse, pwampeeters
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: All   
Whiteboard:
Attachments:
Description Flags
Minimal testcase to demonstrate bug none

Description Victor Homyakov CLA 2011-07-07 12:10:49 EDT
Overview:
The local variable used in closure is marked as never read.

Steps to Reproduce:
Create JavaScript file
(function() {
  var pasteEventName = (Prototype.Browser.IE ? 'paste' : 'input'), storageKey = "mask_data";

  function setMask(element) {
    (function(element) {
      var input = element;
      if (!input.readAttribute("readonly")) {
        input.observe("mask:unmask", function() {
          input.store(storageKey, undefined).stopObserving(pasteEventName);
        });
      }
    })(element);
    return element;
  }

  setMask.storageKey = storageKey;
})();

Actual Results:
Warning "The local variable pasteEventName is never read".

Expected Results:
No warning.

Build Date & Platform:
Eclipse J2EE Indigo 20110615-0604, JSDT 1.3.0.v201103031824
Comment 1 Alex J Burke CLA 2011-11-28 12:26:34 EST
Created attachment 207611 [details]
Minimal testcase to demonstrate bug

I think the bug comes about when the function is assigned to the global object. See the line:
GLOBAL_OBJ.foo = foo;

When it is present the variable 'read' will be incorrectly marked as never having been accessed, but commenting out the assignment above makes the warning
disappear.
Comment 2 Victor Homyakov CLA 2011-11-29 06:02:47 EST
In your example variable "read" is actually never read :) - there is no invocation of bar(), so IMHO warning is absolutely correct, although there may be also some indication of dead code e.g. "bar() is never invoked directly".

On the other side, when I rewrite my example as

(function() {
  var pasteEventName = (Prototype.Browser.IE ? 'paste' : 'input'), storageKey = "mask_data";

  function setMask(element) {
    (function(element) {
      var input = element;
      if (!input.readAttribute("readonly")) {
        input.observe("mask:unmask", function() {
          input.store(storageKey, undefined).stopObserving(pasteEventName);
        });
      }
    })(element);
    return element;
  }

  setMask.storageKey = storageKey;
  window.setMask = setMask;
})();

there is assignment to global variable, setMask() is publicly accessible, and "pasteEventName" is definitely read when invoking setMask(). Commenting line

  window.setMask = setMask;

has no effect, but commenting/uncommenting line

  setMask.storageKey = storageKey;

toggles warning. Note also incorrect highlighting of "storageKey" - 1st and last appearances are highlighted separately from 2nd and 3rd.
Comment 3 Victor Homyakov CLA 2012-02-10 09:57:56 EST
Another example:

(function() {
  var moveCaretTimer = -1;

  function setMask() {
    (function() {
      function focusEvent() {
        var moveCaret = function() {
          // empty
        };
        clearTimeout(moveCaretTimer);
        moveCaretTimer = setTimeout(moveCaret, 0);
      }
    })();
  }

  setMask.storageKey = storageKey;
})();

moveCaretTimer is marked as never read, its occurrences not highlighted.
Comment 4 Victor Homyakov CLA 2012-06-29 05:49:28 EDT
Bug still present in Eclipse Java EE IDE for Web Developers.
Version: Juno Release
Build id: 20120322-1740
Comment 5 Alex Pogrebnyak CLA 2013-07-12 11:50:49 EDT
Still present in Kepler

JavaScript Development Tools

Version: 1.5.0.v201301022000-7H7DFhZFC7sReqSz-frflXk
Build id: 20120820000423
Comment 6 Pieter Peeters CLA 2014-06-24 03:56:53 EDT
Still present in Luna.

JavaScript Development Tools 1.6.0.v201405071612-7G7FFyXFBBoPc8Pvc8c0wn