Community
Participate
Working Groups
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; }
*** This bug has been marked as a duplicate of 109282 ***