Community
Participate
Working Groups
Build ID: I20070621-1340 Steps To Reproduce: 1. Use this sample code: public class AmbiguousSyntaxDemo { @SuppressWarnings("boxing") public static void main(String[] args) { int i = 2; int a = 3; int b = a + -(-i); int c = a + - -i; int d = a + --i; System.out.printf("a=%d b=%d c=%d d=%d i=%d%n", a, b, c, d, i); } } 2. Ensure formatter preferences for spaces in expressions on unary operators are set to _not_ insert spaces. 3. Format the code (Ctrl-Shift-F) and watch what happens to the assignment to "c". More information: On code like this: int c = a + - -i; the formatter will change the assignment to "c" from "a + - - i" to "a + --i" if the formatter preferences for unary operators are set to not insert a space before or after the unary operator, as they are set with the eclipse defaults. The formatter should not have to rely on the formatter preferences to produce semantically identical code after formatting is applied. Hence the formatter should always insert a space between two consecutive unary minus operators, regardless of the preferences. The unary plus operator has the same problem: int b = a + +( +i); int c = a + + +i; int d = a + ++i; Workarounds: of course one could (and probably should) use parentheses in such an ambiguous case of unary operator use. Also the bug does not manifest when formatter preferences are set to insert a space either before or after unary operators.
Released for 3.4M2. Regression tests added in org.eclipse.jdt.core.tests.formatter.FormatterRegressionTests#test669 org.eclipse.jdt.core.tests.formatter.FormatterRegressionTests#test670
Verified for 3.4M2 using build I20070917-0010