Skip to content

readability-else-after-return breaks comments (needs newline) #148662

Open
@higher-performance

Description

@higher-performance

This check turns code such as

void f(int a) {
  if (a > 0) {
    return;
  } else {
    // comment-1
    return;
  }
}

into

void f(int a) {
  if (a > 0) {
    return;
  }     // comment-1
    // another line
    return;

}

which is problematic because comment-1 is associated with the subsequent line, not with the preceding closing brace. This trips up clang-format as well.

A better fix would have been

void f(int a) {
  if (a > 0) {
    return;
  }
    // comment-1
    // another line
    return;

}

which can then get formatted correctly by clang-format.

I'm not sure how to produce the correct fix here without breaking other cases, though.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions