Skip to content

Commit

Permalink
Forbid code that would reformat poorly
Browse files Browse the repository at this point in the history
Current checkstyle rules somehow allow things like

    void run() { throw new UnsupportedOperationException(); }

    Runnable r = () -> { throw new RuntimeException(); }

    Runnable asRunnable()
    { a_field++; }

    void update()
    { throw new RuntimeException(); }

however, when such code is then auto-formatted by IntelliJ with Airbase
codestyle, some whitespace is removed, resulting in a code that violates
checkstyle rules. This state is undesired: sometimes a person working on
a class file may need to undo auto-formatting so pass checkstyle checks.

The new rules aim to reduce such situation by recognizing some common
would-format-incorrectly patterns.
  • Loading branch information
findepi committed Jan 10, 2024
1 parent 859b24e commit 95eeac6
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions airbase-policy/src/main/resources/checkstyle/airbase-checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,22 @@
<property name="ignoreComments" value="true" />
</module>

<module name="RegexpSinglelineJava">
<!-- when "{" denotes conditional/loop statement body, IntelliJ would reformat adding line break -->
<!-- when "{" denotes method/lambda body, and closing brace is on the same line, IntelliJ would remove whitespace before throw, violating "{" whitespace expectations-->
<property name="format" value="\{\s*throw\b" />
<property name="message" value="No new line before throw" />
<property name="ignoreComments" value="true" />
</module>

<module name="RegexpSinglelineJava">
<!-- when "}" denotes end of conditional/loop statement body, IntelliJ would reformat adding line break -->
<!-- when "}" denotes end of method/lambda body and opening brace is on the same line, IntelliJ would remove whitespace before the brace, violating "}" whitespace expectations-->
<property name="format" value=";\s*\}" />
<property name="message" value="No new line before closing brace" />
<property name="ignoreComments" value="true" />
</module>

<!-- javadoc -->
<module name="RequireEmptyLineBeforeBlockTagGroup" />
<module name="NonEmptyAtclauseDescription" />
Expand Down

0 comments on commit 95eeac6

Please sign in to comment.