Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 405038

Summary: [formatter] infix expression formatting broken without spaces before/after binary operator
Product: [Eclipse Project] JDT Reporter: Markus Keller <markus.kell.r>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: VERIFIED FIXED QA Contact:
Severity: major    
Priority: P3 CC: jarthana, stephan.herrmann
Version: 4.3   
Target Milestone: 4.3 M7   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Attachments:
Description Flags
Proposed fix
none
Regression tests none

Description Markus Keller CLA 2013-04-05 15:50:48 EDT
If you set FORMATTER_INSERT_SPACE_BEFORE_BINARY_OPERATOR and
FORMATTER_INSERT_SPACE_AFTER_BINARY_OPERATOR to DO_NOT_INSERT, then code like this:

	int foo(int a, int b, int c) {
		return a + b + ++c;
	}

gets mangled into:

		return a+b+++c;

Note that the compiler parses this as:

		return (a)+(b++)+(c);

=> The ++ moved from ++c to b++ !
Comment 1 Markus Keller CLA 2013-04-05 16:08:10 EDT
org.eclipse.jdt.core.tests.rewrite.describing.ASTRewritingExpressionsTest.testBug404251() has a disabled test case for this.
Comment 2 Olivier Thomann CLA 2013-04-08 09:52:03 EDT
Created attachment 229443 [details]
Proposed fix
Comment 3 Olivier Thomann CLA 2013-04-08 09:52:20 EDT
Created attachment 229444 [details]
Regression tests
Comment 4 Jay Arthanareeswaran CLA 2013-04-08 11:40:45 EDT
(In reply to comment #2)
> Created attachment 229443 [details]
> Proposed fix

Thanks for the patch, Olivier. Patch looks good, will release after running the tests.
Comment 6 Stephan Herrmann CLA 2013-04-30 08:28:07 EDT
Verified for 4.3 M7 using build I20130428-2000 that
- formatting no longer changes the semantics.

I was slightly surprised to see that these

		int s = b-- - --c;
		int t = b-- - -c;

are still formatted as

		int s = b--- --c;
		int t = b--- -c;

which is not wrong, but still makes for a confusing read (but then the 
programmer writing such code is more to blame than the formatter :) ).
Comment 7 Markus Keller CLA 2013-04-30 09:12:17 EDT
> (but then the 
> programmer writing such code is more to blame than the formatter :) ).

I'd mainly blame the one who sets the *INSERT_SPACE* options to DO_NOT_INSERT...