Skip to content

Commit

Permalink
Issue checkstyle#7584: update doc for RightCurly
Browse files Browse the repository at this point in the history
  • Loading branch information
AMOOOMA authored and ImmortalRabbit committed Apr 9, 2020
1 parent dca1c22 commit 86ad49a
Show file tree
Hide file tree
Showing 2 changed files with 242 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,51 @@
* <module name="RightCurly"/>
* </pre>
* <p>
* Example:
* </p>
* <pre>
*public class Test {
*
* public void test() {
*
* if (foo) {
* bar();
* } // violation, right curly must be in the same line as the 'else' keyword
* else {
* bar();
* }
*
* if (foo) {
* bar();
* } else { // OK
* bar();
* }
*
* if (foo) { bar(); } int i = 0; // violation
* // ^^^ statement is not allowed on same line after curly right brace
*
* if (foo) { bar(); } // OK
* int i = 0;
*
* try {
* bar();
* } // violation, rightCurly must be in the same line as 'catch' keyword
* catch (Exception e) {
* bar();
* }
*
* try {
* bar();
* } catch (Exception e) { // OK
* bar();
* }
*
* } // OK
*
* public void testSingleLine() { bar(); } // OK, because singleline is allowed
*}
* </pre>
* <p>
* To configure the check with policy {@code alone} for {@code else} and
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">
* METHOD_DEF</a> tokens:
Expand All @@ -81,9 +126,85 @@
* &lt;property name=&quot;tokens&quot; value=&quot;LITERAL_ELSE, METHOD_DEF&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <p>
* Example:
* </p>
* <pre>
*public class Test {
*
* @since 3.0
* public void test() {
*
* if (foo) {
* bar();
* } else { bar(); } // violation, right curly must be alone on line
*
* if (foo) {
* bar();
* } else {
* bar();
* } // OK
*
* try {
* bar();
* } catch (Exception e) { // OK because config is set to token METHOD_DEF and LITERAL_ELSE
* bar();
* }
*
* } // OK
*
* public void violate() { bar; } // violation, singleline is not allowed here
*
* public void ok() {
* bar();
* } // OK
*}
* </pre>
* <p>
* To configure the check with policy {@code alone_or_singleline} for {@code if}
* and <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">
* METHOD_DEF</a>
* tokens:
* </p>
* <pre>
* &lt;module name=&quot;RightCurly&quot;&gt;
* &lt;property name=&quot;option&quot; value=&quot;alone_or_singleline&quot;/&gt;
* &lt;property name=&quot;tokens&quot; value=&quot;LITERAL_IF, METHOD_DEF&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <p>
* Example:
* </p>
* <pre>
*public class Test {
*
* public void test() {
*
* if (foo) {
* bar();
* } else { // violation, right curly must be alone on line
* bar();
* }
*
* if (foo) {
* bar();
* } // OK
* else {
* bar();
* }
*
* try {
* bar();
* } catch (Exception e) { // OK because config did not set token LITERAL_TRY
* bar();
* }
*
* } // OK
*
* public void violate() { bar(); } // OK , because singleline
*}
* </pre>
*
* @since 3.0
*/
@StatelessCheck
public class RightCurlyCheck extends AbstractCheck {
Expand Down
120 changes: 120 additions & 0 deletions src/xdocs/config_blocks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,50 @@ allowedFuture.addCallback(result -> {
&lt;module name=&quot;RightCurly&quot;/&gt;
</source>

<p>Example: </p>
<source>
public class Test {

public void test() {

if (foo) {
bar();
} // violation, right curly must be in the same line as the 'else' keyword
else {
bar();
}

if (foo) {
bar();
} else { // OK
bar();
}

if (foo) { bar(); } int i = 0; // violation
// ^^^ statement is not allowed on same line after curly right brace

if (foo) { bar(); } // OK
int i = 0;

try {
bar();
} // violation, rightCurly must be in the same line as 'catch' keyword
catch (Exception e) {
bar();
}

try {
bar();
} catch (Exception e) { // OK
bar();
}

} // OK

public void testSingleLine() { bar(); } // OK, because singleline is allowed
}
</source>

<p>
To configure the check with policy <code>alone</code> for <code> else</code> and <a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">
Expand All @@ -1057,6 +1101,82 @@ allowedFuture.addCallback(result -> {
&lt;property name=&quot;tokens&quot; value=&quot;LITERAL_ELSE, METHOD_DEF&quot;/&gt;
&lt;/module&gt;
</source>

<p>Example: </p>
<source>
public class Test {

public void test() {

if (foo) {
bar();
} else { bar(); } // violation, right curly must be alone on line

if (foo) {
bar();
} else {
bar();
} // OK

try {
bar();
} catch (Exception e) { // OK because config is set to token METHOD_DEF and LITERAL_ELSE
bar();
}

} // OK

public void violate() { bar; } // violation, singleline is not allowed here

public void ok() {
bar();
} // OK
}
</source>

<p>
To configure the check with policy <code>alone_or_singleline</code> for <code> if</code>
and <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#METHOD_DEF">
METHOD_DEF</a>
tokens:
</p>
<source>
&lt;module name=&quot;RightCurly&quot;&gt;
&lt;property name=&quot;option&quot; value=&quot;alone_or_singleline&quot;/&gt;
&lt;property name=&quot;tokens&quot; value=&quot;LITERAL_IF, METHOD_DEF&quot;/&gt;
&lt;/module&gt;
</source>

<p>Example: </p>
<source>
public class Test {

public void test() {

if (foo) {
bar();
} else { // violation, right curly must be alone on line
bar();
}

if (foo) {
bar();
} // OK
else {
bar();
}

try {
bar();
} catch (Exception e) { // OK because config did not set token LITERAL_TRY
bar();
}

} // OK

public void violate() { bar(); } // OK , because singleline
}
</source>
</subsection>

<subsection name="Example of Usage" id="RightCurly_Example_of_Usage">
Expand Down

0 comments on commit 86ad49a

Please sign in to comment.