Skip to content

Commit

Permalink
Issue checkstyle#3126: Fix CommentsIndentation false-positive in sing…
Browse files Browse the repository at this point in the history
…leline comment after a block comment
  • Loading branch information
Vladlis committed May 22, 2016
1 parent aa45a04 commit 9801983
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 163 deletions.
2 changes: 2 additions & 0 deletions config/suppressions.xml
Expand Up @@ -121,6 +121,8 @@

<!-- There are a lot of setters/getters in the Check. A small number of methods is left for Check's logic -->
<suppress checks="MethodCount" files="[\\/]JavadocMethodCheck.java$"/>
<!-- Apart from a complex logic there is a lot of small methods for a better readability. -->
<suppress checks="MethodCount" files="[\\/]CommentsIndentationCheck.java$"/>

<!-- getDetails() method - huge Switch, it has to be monolithic -->
<suppress checks="ExecutableStatementCount" files="RightCurlyCheck\.java" lines="316"/>
Expand Down
Expand Up @@ -90,7 +90,7 @@ public void testCommentIsAtTheEndOfBlock() throws Exception {
"322: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
323, 0, 4),
"336: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
337, 0, 4),
333, 0, 8),
"355: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
352, 9, 8),
};
Expand All @@ -106,6 +106,8 @@ public void testCommentIsAtTheEndOfBlock() throws Exception {
@Test
public void testCommentIsInsideSwitchBlock() throws Exception {
final String[] expected = {
"19: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.block",
20, 12, 16),
"25: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
"24, 26", 19, "16, 12"),
"31: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
Expand Down
Expand Up @@ -16,7 +16,7 @@ private static void fooSwitch() {
// comment
break;
case "3":
/* com */
/* // warn */
foo1();
/* com */
break;
Expand Down

Large diffs are not rendered by default.

Expand Up @@ -37,7 +37,7 @@
/**
*
* @author <a href="mailto:nesterenko-aleksey@list.ru">Aleksey Nesterenko</a>
* @author <a href="mailto:andreyselkin@gmail.com">Aleksey Nesterenko</a>
* @author <a href="mailto:andreyselkin@gmail.com">Andrei Selkin</a>
*
*/
public class CommentsIndentationCheckTest extends BaseCheckTestSupport {
Expand Down Expand Up @@ -78,11 +78,12 @@ public void testCommentIsAtTheEndOfBlock() throws Exception {
"277: " + getCheckMessage(MSG_KEY_SINGLE, 276, 9, 8),
"316: " + getCheckMessage(MSG_KEY_SINGLE, 315, 9, 8),
"322: " + getCheckMessage(MSG_KEY_SINGLE, 323, 0, 4),
"336: " + getCheckMessage(MSG_KEY_SINGLE, 337, 0, 4),
"336: " + getCheckMessage(MSG_KEY_SINGLE, 333, 0, 8),
"355: " + getCheckMessage(MSG_KEY_SINGLE, 352, 9, 8),
"380: " + getCheckMessage(MSG_KEY_BLOCK, 381, 12, 8),
"393: " + getCheckMessage(MSG_KEY_SINGLE, 392, 12, 8),
"400: " + getCheckMessage(MSG_KEY_SINGLE, 401, 8, 10),
"456: " + getCheckMessage(MSG_KEY_SINGLE, 454, 0, 8),
};
final String testInputFile = "InputCommentsIndentationCommentIsAtTheEndOfBlock.java";
verify(checkConfig, getPath(testInputFile), expected);
Expand All @@ -93,6 +94,7 @@ public void testCommentIsInsideSwitchBlock() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(CommentsIndentationCheck.class);
final String[] expected = {
"19: " + getCheckMessage(MSG_KEY_BLOCK, 20, 12, 16),
"25: " + getCheckMessage(MSG_KEY_SINGLE, "24, 26", 19, "16, 12"),
"31: " + getCheckMessage(MSG_KEY_SINGLE, "30, 32", 19, "16, 12"),
"48: " + getCheckMessage(MSG_KEY_SINGLE, 49, 6, 16),
Expand All @@ -109,6 +111,8 @@ public void testCommentIsInsideSwitchBlock() throws Exception {
"204: " + getCheckMessage(MSG_KEY_SINGLE, 205, 20, 17),
"205: " + getCheckMessage(MSG_KEY_SINGLE, "202, 206", 17, "16, 12"),
"229: " + getCheckMessage(MSG_KEY_SINGLE, "228, 230", 6, "12, 12"),
"276: " + getCheckMessage(MSG_KEY_BLOCK, "275, 279", 11, "16, 12"),
"281: " + getCheckMessage(MSG_KEY_SINGLE, "280, 282", 11, "16, 12"),
};
final String testInputFile = "InputCommentsIndentationInSwitchBlock.java";
verify(checkConfig, getPath(testInputFile), expected);
Expand Down Expand Up @@ -143,6 +147,9 @@ public void testSurroundingCode() throws Exception {
"90: " + getCheckMessage(MSG_KEY_SINGLE, 91, 14, 8),
"98: " + getCheckMessage(MSG_KEY_SINGLE, 99, 13, 8),
"108: " + getCheckMessage(MSG_KEY_SINGLE, 109, 33, 8),
"130: " + getCheckMessage(MSG_KEY_BLOCK, 131, 12, 8),
"135: " + getCheckMessage(MSG_KEY_BLOCK, 136, 4, 8),
"141: " + getCheckMessage(MSG_KEY_BLOCK, 140, 4, 8),
};
final String testInputFile = "InputCommentsIndentationSurroundingCode.java";
verify(checkConfig, getPath(testInputFile), expected);
Expand Down Expand Up @@ -183,7 +190,10 @@ public void testCheckOnlyBlockComments() throws Exception {
"25: " + getCheckMessage(MSG_KEY_BLOCK, 27, 16, 12),
"28: " + getCheckMessage(MSG_KEY_BLOCK, 31, 16, 12),
"51: " + getCheckMessage(MSG_KEY_BLOCK, 53, 23, 36),
};
"130: " + getCheckMessage(MSG_KEY_BLOCK, 131, 12, 8),
"135: " + getCheckMessage(MSG_KEY_BLOCK, 136, 4, 8),
"141: " + getCheckMessage(MSG_KEY_BLOCK, 140, 4, 8),
};
final String testInputFile = "InputCommentsIndentationSurroundingCode.java";
verify(checkConfig, getPath(testInputFile), expected);
}
Expand Down
Expand Up @@ -397,11 +397,65 @@ void foo58() {
/*
comment
*/
// comment
// violation
foo1();
// comment
}

void foo59() {
foo1();
/*
comment */
// comment
}


void foo61() {
foo1();
/*
* comment
*/
/*
* comment
*/
}

void foo62() {
if (true) {
System.out.println();
}
else {

}
/*
comment
*/
/*
comment
*/
}

void foo63() {
try {
System.out.println();
}
catch (Exception e){

}
/*
comment
*/
/*
comment
*/
}

void foo64() {
foo1();

// comment
}

// We almost reached the end of the class here.
}
// The END of the class.
Expand Up @@ -16,7 +16,7 @@ private static void fooSwitch() {
// comment
break;
case "3":
/* com */
/* violation */
foo1();
/* com */
break;
Expand Down Expand Up @@ -265,4 +265,41 @@ public void foo12() {
case 1:
}
}

public void foo13() {
int a = 5;
switch (a) {
case 1:
/* comment */
case 2:
hashCode();
/*
violation
*/
case 3: // comment
hashCode();
// violation
case 4: // comment
if (true) {

}
else {

}
// comment
case 5:
String s = ""
+ 1
+ "123";
break;
// comment
case 6:
String q = ""
+ 1
+ "123";
// comment
case 7:
break;
}
}
}
Expand Up @@ -125,5 +125,21 @@ public void foo10()
};
}

public void foo11() {

/* empty */
hashCode();
}

public void foo12() {
/* empty */
hashCode();
}

public void foo13() {
hashCode();
/* empty */
}

} // The Check should not throw NPE here!
// The Check should not throw NPE here!

0 comments on commit 9801983

Please sign in to comment.