Bug 285554 - [extract method] Erroneous return type while extracting code out of a nested for loop
Summary: [extract method] Erroneous return type while extracting code out of a nested ...
Status: CLOSED DUPLICATE of bug 488942
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.6   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: fix candidate
Keywords:
Depends on:
Blocks:
 
Reported: 2009-08-04 06:47 EDT by Raksha Vasisht CLA
Modified: 2019-04-09 08:58 EDT (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Raksha Vasisht CLA 2009-08-04 06:47:44 EDT
Build ID: I20090803-1800

Steps To Reproduce:
Ref: bug 213519

Testcase :
1.
public class Snippet {
	
	void foo(){
		ArrayList<Integer> list = new ArrayList<Integer>();
		for (Integer var: list) {
			int a = 0;		
			for (int count = 0; count < 10; count++) {
				list.add(a++);			
			}
		
		}
		System.out.println(list.toString());	
	}	
	
}

2. Select 'list.add(a++);' Extract method gives the refactoring with 'void' as the return type: 

public class Snippet {
	
	void foo(){
		ArrayList<Integer> list = new ArrayList<Integer>();
		for (Integer var: list) {
			int a = 0;		
			for (int count = 0; count < 10; count++) {
				zzzz(list, a);			
			}		
		}
		System.out.println(list.toString());	
	}

	private void zzzz(ArrayList<Integer> list, int a) {
		list.add(a++);
	}	
	
}

3. Select 'list.add(a++)' (without ;) Extract method gives 
a method with return type 'boolean' :

public class Snippet {
	
	void foo(){
		ArrayList<Integer> list = new ArrayList<Integer>();
		for (Integer var: list) {
			int a = 0;		
			for (int count = 0; count < 10; count++) {
				zzzz(list, a);			
			}
		
		}
		System.out.println(list.toString());	
	}

	private boolean zzzz(ArrayList<Integer> list, int a) {
		return list.add(a++);
	}	
	
}


More information:
Comment 1 Raksha Vasisht CLA 2009-08-04 06:51:35 EDT
Expected behaviour : return type should be int since we return 'a' :

public class Snippet {
	
	void foo(){
		ArrayList<Integer> list = new ArrayList<Integer>();
		for (Integer var: list) {
			int a = 0;		
			for (int count = 0; count < 10; count++) {
				a = zzzz(list, a);			
			}		
		}
		System.out.println(list.toString());	
	}

	private int zzzz(ArrayList<Integer> list, int a) {
		list.add(a++);
		return a;
	}	
	
}
Comment 2 Markus Keller CLA 2009-08-05 10:41:57 EDT
The difference between selecting an ExpressionStatement (2.) and an Expression (3.) would be OK (see bug 285551), but we need to fix nested for loops for M2.
Comment 3 Dani Megert CLA 2011-04-12 06:29:38 EDT
Ping! Benny, any plans to work on this?
Comment 4 Jay Arthanareeswaran CLA 2019-04-08 05:52:20 EDT
(In reply to Dani Megert from comment #3)
> Ping! Benny, any plans to work on this?

Who's Benny? I don't see anyone by that name copied on the bug? Is this a duplicate of bug 488942?
Comment 5 Dani Megert CLA 2019-04-08 05:56:26 EDT
(In reply to Jay Arthanareeswaran from comment #4)
> (In reply to Dani Megert from comment #3)
> > Ping! Benny, any plans to work on this?
> Who's Benny? I don't see anyone by that name copied on the bug?
Probably Benno who was in our team at some point.

> Is this a duplicate of bug 488942?
I would have to look at it which is something you can also do ;-).
Comment 6 Jay Arthanareeswaran CLA 2019-04-08 06:58:58 EDT
(In reply to Dani Megert from comment #5)
> > Is this a duplicate of bug 488942?
> I would have to look at it which is something you can also do ;-).

I see the symptoms being different and thought them to be different. But Dan in the below list suggests they are same. So, I thought someone who knows more about this could take the final call.

https://www.eclipse.org/lists/jdt-dev/msg01159.html

I suppose that would be Noopur?
Comment 7 Lars Vogel CLA 2019-04-09 08:58:37 EDT
I also think this is a dup of Bug 488942, please reopen if that was wrong.

*** This bug has been marked as a duplicate of bug 488942 ***