Since Checkstyle 3.4
The check to ensure that lines with code do not end with comment.
For the case of //
comments that means that the only thing
that should precede it is whitespace. It doesn't check comments if
they do not end a line; for example, it accepts the following:
Thread.sleep( 10 /*some comment here*/ );
Format
property is intended to deal with the } // while
example.
Rationale: Steve McConnell in Code Complete suggests that endline comments are a bad practice. An end line comment would be one that is on the same line as actual code. For example:
a = b + c; // Some insightful comment d = e / f; // Another comment for this line
Quoting Code Complete for the justification:
McConnell's comments on being hard to maintain when the size of the line changes are even more important in the age of automated refactorings.
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="TrailingComment"/> </module> </module>
Example:
// OK if (/* OK */ x > 5) {} int a = 5; // violation doSomething( param1 ); // OK, by default such trailing of method/code-block ending is allowed
To configure the check to enforce only comment on a line:
<module name="Checker"> <module name="TreeWalker"> <module name="TrailingComment"> <property name="format" value="^\s*$"/> </module> </module> </module>
Example:
// OK if (/* OK, this comment does not end the line */ x > 5) {} int a = 5; // violation, line content before comment should match pattern "^\s*$" doSomething( param1 ); // violation, line content before comment should match pattern "^\s*$"
To configure check so that trailing comment with exact comments like "SUPPRESS CHECKSTYLE", "NOPMD", "NOSONAR" are suppressed:
<module name="Checker"> <module name="TreeWalker"> <module name="TrailingComment"/> <module name="SuppressionXpathSingleFilter"> <property name="checks" value="TrailingCommentCheck"/> <property name="query" value="//SINGLE_LINE_COMMENT [./COMMENT_CONTENT[@text=' NOSONAR\n' or @text=' NOPMD\n' or @text=' SUPPRESS CHECKSTYLE\n']]"/> </module> </module> </module>
Example for trailing comments check to suppress specific trailing comment:
public class Test { int a; // SUPPRESS CHECKSTYLE int b; // NOPMD int c; // NOSONAR int d; // violation, not suppressed }
To configure check so that trailing comment starting with "SUPPRESS CHECKSTYLE", "NOPMD", "NOSONAR" are suppressed:
<module name="Checker"> <module name="TreeWalker"> <module name="TrailingComment"/> <module name="SuppressionXpathSingleFilter"> <property name="checks" value="TrailingCommentCheck"/> <property name="query" value="//SINGLE_LINE_COMMENT [./COMMENT_CONTENT[starts-with(@text, ' NOPMD')]]"/> <property name="query" value="//SINGLE_LINE_COMMENT [./COMMENT_CONTENT[starts-with(@text, ' SUPPRESS CHECKSTYLE')]]"/> <property name="query" value="//SINGLE_LINE_COMMENT [./COMMENT_CONTENT[starts-with(@text, ' NOSONAR')]]"/> </module> </module> </module>
Example:
public class Test { int a; // SUPPRESS CHECKSTYLE - OK, comment starts with " SUPPRESS CHECKSTYLE" int b; // NOPMD - OK, comment starts with " NOPMD" int c; // NOSONAR - OK, comment starts with " NOSONAR" int d; // violation, not suppressed }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks