Bug 351338 - [validation] False "variable not initialized" when using for-in
Summary: [validation] False "variable not initialized" when using for-in
Status: NEW
Alias: None
Product: JSDT
Classification: WebTools
Component: General (show other bugs)
Version: unspecified   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: Future   Edit
Assignee: Project Inbox CLA
QA Contact: Chris Jaun CLA
URL:
Whiteboard:
Keywords:
: 391406 (view as bug list)
Depends on:
Blocks:
 
Reported: 2011-07-06 10:38 EDT by Klaus Reimer CLA
Modified: 2013-07-10 16:08 EDT (History)
3 users (show)

See Also:
cmjaun: review-


Attachments
Patch to fix bug 351338 (2.61 KB, patch)
2012-05-04 02:25 EDT, Lupo L CLA
no flags Details | Diff
Patch to fix bug 351338 (1.27 KB, patch)
2012-05-07 15:56 EDT, Lupo L CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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. ***