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

Bug 351338

Summary: [validation] False "variable not initialized" when using for-in
Product: [WebTools] JSDT Reporter: Klaus Reimer <k>
Component: GeneralAssignee: Project Inbox <jsdt.javascript-inbox>
Status: NEW --- QA Contact: Chris Jaun <cmjaun>
Severity: normal    
Priority: P3 CC: jpadilla, lupo.bugzilla, michele
Version: unspecifiedFlags: cmjaun: review-
Target Milestone: Future   
Hardware: PC   
OS: Linux   
Whiteboard:
Attachments:
Description Flags
Patch to fix bug 351338
none
Patch to fix bug 351338 none

Description Klaus Reimer CLA 2011-07-06 10:38:56 EDT
Build Identifier: I20110613-1736

The key variable in a for-in loop is reported as "not initialized" (When the corresponding warning is enabled in the validation settings):

  function test()
  {
      var k, a;

      a = {};
      for (k in a) {}
  }

I think this is wrong. "k" does not need to be initialized, it will be initialized with the keys of the iterated object during the for-loop.

Reproducible: Always
Comment 1 Lupo L CLA 2012-05-04 02:25:52 EDT
Created attachment 215039 [details]
Patch to fix bug 351338

Flow analysis sees the "k" as a reference to "var k".

Problem can be fixed simply enough by not traversing the SingleNameReference for "k" at all; it cannot possibly change flow information, and is included as "definitely assigned" later on anyway.
Comment 2 Lupo L CLA 2012-05-07 15:56:24 EDT
Created attachment 215209 [details]
Patch to fix bug 351338

Improved (and reduced) patch
Comment 3 Jorge Padilla CLA 2013-06-19 15:43:49 EDT
Chris, I have reviewed this patch and validated it resolves this problem.  I recommend it to be included in 3.5.1.  Could you officially complete 
this review?
Comment 4 Chris Jaun CLA 2013-06-25 14:53:43 EDT
The issue with this patch is it just shifts the problem. If it doesn't enter the analyseCode method, then the variable does not get flagged as "used", so now that error message incorrectly shows up instead.

Will look to see if there is another solution. There is also some movement towards removing some of these types of error messages from JSDT going forward, so it could get cleaned up that way too.
Comment 5 Chris Jaun CLA 2013-07-10 16:08:00 EDT
*** Bug 391406 has been marked as a duplicate of this bug. ***