Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 109285 - [refactoring][extract method] Extract Method rejected unnecessarily when selection followed by "break"
Summary: [refactoring][extract method] Extract Method rejected unnecessarily when sele...
Status: RESOLVED DUPLICATE of bug 109282
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-09-12 09:08 EDT by Ran Ettinger CLA
Modified: 2005-09-12 10:27 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ran Ettinger CLA 2005-09-12 09:08:46 EDT
Trying to apply the Extract Method refactoring on the following selection 
results in an unnecessary rejection.

Original program: 

  private int g(boolean b) {
    int n = 10;
    int i = 0;
    while (i < n) {
      if (b) {
        // Extract Method from here
        i++;
        n -= i;
	// to here
        break;
      }
      else i++;
    }
    return n;
  }

Applying Extract Method on the selected code (signalled by the comments)
results in a rejection with the message: "Ambiguous return value: selected 
block contains more than one assignment to local variable".

In fact, because of the "break" statement, only the modified local variable n 
should be returned. The local variable i will never be relevant on return from 
the extracted method. As a result, variable i can be localized in the new 
method.

I expected the refactored source to look like this:

  private int g(boolean b) {
    int n = 10;
    int i = 0;
    while (i < n) {
      if (b) {
        n = newMethod(i, n);
        break;
      }
      else i++;
    }
    return n;
  }

  private int newMethod(int i, int n) {
    i++;
    n -= i;
    return n;
  }
Comment 1 Dirk Baeumer CLA 2005-09-12 10:27:22 EDT

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