Bug 352422 - [1.7][quick assist] 'Convert switch to if-else' not offered with fall-through case statements
Summary: [1.7][quick assist] 'Convert switch to if-else' not offered with fall-through...
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.5   Edit
Hardware: All All
: P5 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-19 05:01 EDT by Srikanth Sankaran CLA
Modified: 2012-07-18 13:43 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Srikanth Sankaran CLA 2011-07-19 05:01:06 EDT
BETA_JAVA7


enum Day {
	Mon, Tue, Wed, Thurs, Fri, Sat, Sun,
}
public class X {
	public static void main(final String [] args) {
		switch (Day.Wed) {
		case Wed:
			System.out.println("Correct");
			break;
			default: 
				System.out.println("Wrong");
		}
	}
}

With the cursor on switch, press ctrl-1. You see two quick assists:

(a) Add missing case statements
(b) Convert switch to if-else-if

Opt for the first one.

Now with the cursor on switch press ctrl-1, you get a "No suggestions
available" message.
Comment 1 Srikanth Sankaran CLA 2011-07-19 05:21:08 EDT
I also noticed that if a case block falls through to the subsequent
one as in:

public class X {
	public static void main(final String [] args) {
		switch (args[0]) {
		case "":
			System.out.println("");
			// break; // uncommenting out this line shows the assist
		case "a":
			System.out.println("a");
			break;
		}
	}
}

then the new quick assist is not proposed. This is intentional I presume.
Comment 2 Dani Megert CLA 2011-07-19 05:48:30 EDT
> then the new quick assist is not proposed. This is intentional I presume.

The quick assist is not new. Already an issue in older releases, e.g. 3.5.
Comment 3 Deepak Azad CLA 2011-10-01 12:39:05 EDT
(In reply to comment #1)
> I also noticed that if a case block falls through to the subsequent
> one as in:
> then the new quick assist is not proposed. This is intentional I presume.

Quick assist is not proposed in cases in which the conversion requires code duplication.

e.g. the quick assist is offered in the following case
------------------------------------------------------
	void foo(int a) {
		switch (a) {
		case 1:
		case 2:
			System.out.println(2);
			break;
		}
	}
------------------------------------------------------

the result is =>
------------------------------------------------------
	void foo(int a) {
		if (a == 1 || a == 2) {
			System.out.println(2);
		}
	}
------------------------------------------------------

but it is not offered in the following 2 cases
- code to be executed for case 1 and case 2 is different hence upon conversion 2 if statements will be required with code for case 2 duplicated in both. (This is similar to snippet from comment 1)
------------------------------------------------------
	void foo1(int a) {
		switch (a) {
		case 1:
			System.out.println();
		case 2:
			System.out.println(2);
			break;
		}
	}
------------------------------------------------------

- a 'case' statement cannot be combined with 'default' in the if-else code. To think of it, a fall through from a 'case' to 'default' is not too intelligent code, as the case statement can simply be eliminated.(This is similar to snippet from comment 0)
------------------------------------------------------
	void foo2(int a) {
		switch (a) {
		case 1:
		default:
			System.out.println(2);
			break;
		}
	}
------------------------------------------------------

Someone can argue that the conversion should happen in all cases, even if code duplication is required. However, I do not feel strongly enough about this to change anything here. Closing as WONTFIX.
Comment 4 Dani Megert CLA 2011-10-03 08:23:21 EDT
Fixing this is not so hard, even if we avoid to copy the code. But I agree that it is low priority.
Comment 5 Markus Keller CLA 2011-10-05 07:17:00 EDT
(In reply to comment #3)
> Quick assist is not proposed in cases in which the conversion requires code
> duplication.
[..]
> but it is not offered in the following 2 cases
> - code to be executed for case 1 and case 2 is different hence upon conversion
> 2 if statements will be required with code for case 2 duplicated in both. (This
> is similar to snippet from comment 1)
> ------------------------------------------------------
>     void foo1(int a) {
>         switch (a) {
>         case 1:
>             System.out.println();
>         case 2:
>             System.out.println(2);
>             break;
>         }
>     }
> ------------------------------------------------------

Code duplication is not necessary and should indeed not be implemented. Just the conditions and the ifs need to be put a bit differently:

        if (a == 1 || a == 2) {
            if (a == 1) {
                System.out.println();
            }
            System.out.println(2);
        }

> - a 'case' statement cannot be combined with 'default' in the if-else code. To
> think of it, a fall through from a 'case' to 'default' is not too intelligent
> code, as the case statement can simply be eliminated.(This is similar to
> snippet from comment 0)
> ------------------------------------------------------
>     void foo2(int a) {
>         switch (a) {
>         case 1:
>         default:
>             System.out.println(2);
>             break;
>         }
>     }
> ------------------------------------------------------

The "default:" can just be translated to condition "true".
Comment 6 Deepak Azad CLA 2011-10-05 10:04:46 EDT
(In reply to comment #5)
Makes sense. I will take another look.
Comment 7 Markus Keller CLA 2011-10-05 10:46:47 EDT
The "default:" case is easy (and is the subject of comment 0).

Comment 1 (cases without "break;") is a lot more complicated. Comment 5 only shows one example, but in practice, this can become more difficult (e.g. when the "break;" is inside another condition). That problem should go into a separate bug that is really P5 (i.e. not for now).
Comment 8 Markus Keller CLA 2012-07-18 13:43:47 EDT
I've fixed the default case (and a few unused exception warnings) with
http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=a78dd099ed0210a89cf20f09bcb7907282e0fbc2

Keeping this bug for comment 1 (case statements with fall-through).