Bug 167472 - [quick fix] Suggest to add missing type for for-each variable
Summary: [quick fix] Suggest to add missing type for for-each variable
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.2.1   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 4.3 M7   Edit
Assignee: Markus Keller CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 139547 (view as bug list)
Depends on: 180456
Blocks:
  Show dependency tree
 
Reported: 2006-12-11 14:29 EST by Luke Hutchison CLA
Modified: 2013-05-01 05:38 EDT (History)
4 users (show)

See Also:


Attachments
quick fix v0.1 (4.87 KB, patch)
2012-06-12 13:54 EDT, Markus Keller CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Luke Hutchison CLA 2006-12-11 14:29:43 EST
Given the code:

  ArrayList<T> list = new ArrayList<T>();
  for (item : list) {  // error
  }

There is an error on "item", as it is undeclared.  Quickfix asks if you want to create a class, interface or enum for "item", or add a type parameter "item" to the class.  I would expect a suggestion of declaring an iterator variable "T item".  (Also the compiler is confused because it says on "list" that "Cannot convert element type T to item", so it thinks item is a type.)

This looks like a trivial content-assist but saves a lot of typing when iterating through complex generic containers-inside-containers.
Comment 1 Martin Aeschlimann CLA 2006-12-13 09:31:40 EST
Its actually a recovered syntax error (type or name missing). We could detect that case. Does it happen a lot?
Comment 2 Luke Hutchison CLA 2006-12-13 09:45:06 EST
Yes, I hit this *all* the time... mostly because I'm lazy about filling in types (they are tedious and automatically-deducable), so when I declare a variable I just do "x = value();" or something then use Eclipse Quickfix to fill in the type declaration.  That doesn't work with foreach loops.  (I wish Java had a "var x = value()" syntax like Javascript...)

It's worth it when you have types like ArrayList<ArrayList<ConcurrentHashMap<Integer,String>>>... typing in types like that all over the place is a real pain.
Comment 3 Luke Hutchison CLA 2011-04-02 02:39:54 EDT
Current behavior as of Eclipse downloaded today (Helios SR-2, 20110218-0911) is almost the same as when reported in 2006.

Both "item" and "list" in the example above are underlined in red.

Selecting Quick Fix on "item" gives:
 - Create class/interface "item"
 - Change to "<similar name>" (<package>)
 - Create enum "item"
 - Add type parameter "item" to <EnclosingClass>/<enclosingFunction()>
...none of which are helpful.

Selecting Quick Fix on "list" gives:
 - Change type of '$missing$' to 'ArrayList<T>'  (I think this was added in about 2007.)

Selecting this last option gives the following code and leaves the cursor at position '|':

  ArrayList<T> list = new ArrayList<T>();
  for (ArrayList<T>  : |list) {  // error
  }

i.e. "item" is completely eliminated.
Comment 4 Ewout Graswinckel CLA 2011-08-04 06:32:00 EDT
I agree with Luke, this would be very helpful for me. I use quickfix to add type declarations all the time and it's really annoying that it doesn't work in foreach loops.
Comment 5 Luke Hutchison CLA 2011-08-04 14:05:08 EDT
There are other quickfix options that are available for standard for loops too, but not for foreach loops, for example converting the statement after the for loop into a block or vice versa.
Comment 6 Markus Keller CLA 2011-08-05 11:31:34 EDT
Yes, we should fix that for 3.8. The AST already contains all the information we need.

Manual workaround is to insert e.g. 'x ' in front of 'item' and then use the quick fix on 'list' to change the type of 'item'.
Comment 7 Luke Hutchison CLA 2011-08-05 13:12:44 EDT
I use the 'x' fix all the time now..

Makes me think Eclipse should implement a "var" type as some sort of "dynamic macro" that is expanded into the correct type as soon as you leave the line.
Comment 8 Markus Keller CLA 2012-06-12 13:21:10 EDT
*** Bug 139547 has been marked as a duplicate of this bug. ***
Comment 9 Markus Keller CLA 2012-06-12 13:54:05 EDT
Created attachment 217226 [details]
quick fix v0.1

Here's what I did until I realized that most of this should be fixed in the compiler first.
Comment 10 Markus Keller CLA 2013-04-28 18:28:28 EDT
This is quite annoying, so I went forward without a fix for bug 180456. This fix needs to be revised when bug 180456 gets fixed.

http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=9f57afcb99ecfb6b3aa663ebe978b051fb75c842
Comment 11 Dani Megert CLA 2013-05-01 05:38:03 EDT
Verified in I20130430-2000.