Bug 58565 - code formatter doesn't format blocks with a return statement correctly
Summary: code formatter doesn't format blocks with a return statement correctly
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows 2000
: P3 minor (vote)
Target Milestone: 3.0 M9   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-04-14 17:13 EDT by Steven Ostrowski CLA
Modified: 2011-09-14 13:51 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Steven Ostrowski CLA 2004-04-14 17:13:37 EDT
The code formatter (using Java style default formatter) doesn't format a block
with a return statement properly. For example:

void doX() {

  if (1 > 0) {
    return;
  }

  return;

}

It will format it as:

void doX() {

  if (1 > 0) {
  
  return; }

  return;

}
Comment 1 Olivier Thomann CLA 2004-04-27 12:51:10 EDT
What build are you using?
Comment 2 Olivier Thomann CLA 2004-04-27 13:54:30 EDT
Using java conventions settings, I get:
public class A {

    void doX() {
        if (1 > 0) { return; }
        return;
    }
}

Could you please provide build id + your code formatter preferences (export them
into a file that you attach to this PR)?

Thanks
Comment 3 Steven Ostrowski CLA 2004-04-27 20:44:37 EDT
It looks like this may have been fixed in 3.0M8 (which I'm using now).

However, the following addition does cause the error:

if (1 > 0) { //
  return;
}

gets formatted the way I reported
Comment 4 Olivier Thomann CLA 2004-04-27 21:27:04 EDT
In order to get a different formatting, it means that a block with only a return
statement and a line comment is not considered as a guard clause.
Then you would get something like this:

if (1 > 0) { //
  return;
}
after formatting.

Is this what you expect?
Comment 5 Olivier Thomann CLA 2004-04-27 21:47:05 EDT
With the change described in comment 4, I get:

public class A {

    void doX() {
        if (1 > 0) {//
            return;
        }
        return;
    }
}
Comment 6 Steven Ostrowski CLA 2004-04-27 23:55:57 EDT
I still get the issue I reported:

if (1 > 0) { //
  return;
}

becomes

if (1 > 0) { //
return; }

Not sure why we're seeing differences.
Comment 7 Olivier Thomann CLA 2004-04-29 11:33:47 EDT
Fixed and released in HEAD.
Regression test added.

The fix is not to format { // 
return;} as a guard clause, because of the line comment.
In your last test case, you would end up with:
if (1 > 0) { //
    return;
}
Comment 8 David Audel CLA 2004-05-18 11:48:19 EDT
Verified for 3.0M9
Comment 9 P.Martineau CLA 2011-09-14 13:51:25 EDT
Reproduced today on

Eclipse IDE for JavaScript Web Developers.

Version: Indigo Release
Build id: 20110615-0604

Pc W7x64 home premium


[Return] in a [If] block does not format correctly AND cause the formatter to stop working completely (ctrl+shift+f has no effect afterward).
Adding a comment tag like this :

if(i>0){ //
  return i;
}

acts as a workaround and re-enable the formatter.