Bug 578127 - [Code Clean-Up] "Reduce indentation" fails

Ignore this unbracketted case:

public static void main(String[] args) {
    if (args != null)
        if (args.length == 0) {
            System.out.println("no args");
        } else
            throw new IllegalArgumentException(); // comment
}

Change-Id: I50ab754c4753c7e40e16d83d55c3ddfc1b6ea813
Signed-off-by: Fabrice Tiercelin <fabrice.tiercelin@yahoo.fr>
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.ui/+/189930
Tested-by: JDT Bot <jdt-bot@eclipse.org>
Tested-by: Jeff Johnston <jjohnstn@redhat.com>
Reviewed-by: Jeff Johnston <jjohnstn@redhat.com>
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
index 62564be..74c4002 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
@@ -18180,6 +18180,17 @@
 				+ "        return -1;\n" //
 				+ "    }\n" //
 				+ "\n" //
+				+ "    public int reduceWithUnbrackettedThenAndParent(boolean isValid, boolean isActive) {\n" //
+				+ "        if (isValid)\n" //
+				+ "            if (isActive)\n" //
+				+ "                return 0; // This kind of comment is correctly handled\n" //
+				+ "            else {\n" //
+				+ "                System.out.println(\"Valid and active\");\n" //
+				+ "            }\n" //
+				+ "\n" //
+				+ "        return -1;\n" //
+				+ "    }\n" //
+				+ "\n" //
 				+ "    public int refactorElseInSwitch(int discriminant, boolean isVisible) {\n" //
 				+ "        switch (discriminant) {\n" //
 				+ "        case 0:\n" //
@@ -18487,6 +18498,16 @@
 				+ "        return -1;\n" //
 				+ "    }\n" //
 				+ "\n" //
+				+ "    public int reduceWithUnbrackettedThenAndParent(boolean isValid, boolean isActive) {\n" //
+				+ "        if (isValid) {\n" //
+				+ "            if (isActive)\n" //
+				+ "                return 0; // This kind of comment is correctly handled\n" //
+				+ "            System.out.println(\"Valid and active\");\n" //
+				+ "        }\n" //
+				+ "\n" //
+				+ "        return -1;\n" //
+				+ "    }\n" //
+				+ "\n" //
 				+ "    public int refactorElseInSwitch(int discriminant, boolean isVisible) {\n" //
 				+ "        switch (discriminant) {\n" //
 				+ "        case 0:\n" //
@@ -18687,6 +18708,16 @@
 				+ "\n" //
 				+ "        return i;\n" //
 				+ "    }\n" //
+				+ "\n" //
+				+ "    public int doNotRefactorWithUnbrackettedNodeAndParent(boolean isValid, boolean isActive) {\n" //
+				+ "        if (isValid)\n" //
+				+ "            if (isActive) {\n" //
+				+ "                System.out.println(\"Valid and active\");\n" //
+				+ "            } else\n" //
+				+ "                return 0; // This kind of comment is badly handled\n" //
+				+ "\n" //
+				+ "        return -1;\n" //
+				+ "    }\n" //
 				+ "}\n";
 		ICompilationUnit cu= pack.createCompilationUnit("E.java", sample, false, null);
 
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/ReduceIndentationCleanUp.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/ReduceIndentationCleanUp.java
index f1320d0..6c71efa 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/ReduceIndentationCleanUp.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/ReduceIndentationCleanUp.java
@@ -190,6 +190,13 @@
 		unit.accept(new ASTVisitor() {
 			@Override
 			public boolean visit(final IfStatement visited) {
+				// The parsing crashes when there are two embedded lone ifs with an end of line comment at the right of the statement
+				// So we disable the rule on double lone if
+				if (!(visited.getElseStatement() instanceof Block)
+						&& !ASTNodes.canHaveSiblings(visited)) {
+					return true;
+				}
+
 				if (visited.getElseStatement() != null && !ASTNodes.isInElse(visited)) {
 					if (ASTNodes.fallsThrough(visited.getThenStatement())) {
 						if (ASTNodes.fallsThrough(visited.getElseStatement())) {