Hey trying to get hands on with checkstyle, am trying to use the below checkstyle config:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"https://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
<property name="basedir" value="${basedir}"/>
<!-- Checks that a package.html file exists for each package. -->
<!-- See http://checkstyle.sf.net/config_javadoc.html#JavadocPackage -->
<module name="JavadocPackage">
<property name="allowLegacy" value="${checkstyle.legacypackagedocs}"
default="true" />
</module>
<!-- Checks whether files end with a new line. -->
<!-- See http://checkstyle.sf.net/config_misc.html#NewlineAtEndOfFile -->
<module name="NewlineAtEndOfFile"/>
<!-- Allows the suppression of checkstyle event violations within a block. -->
<!-- More here http://checkstyle.sourceforge.net/config_filters.html#SuppressionCommentFilter -->
<module name="SuppressionCommentFilter"/>
<!-- Checks that property files contain the same keys. -->
<!-- See http://checkstyle.sf.net/config_misc.html#Translation -->
<module name="Translation"/>
<!-- Checks for Size Violations. -->
<!-- See http://checkstyle.sf.net/config_sizes.html -->
<module name="FileLength"/>
<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
<module name="FileTabCharacter"/>
<module name="RegexpSingleline">
<property name="format" value="\s+$"/>
<property name="message" value="Line has trailing spaces."/>
</module>
<module name="SuppressionFilter">
<property name="file" value="${checkstyle.suppression.filter}" />
</module>
<module name="SuppressionCommentFilter">
<property name="offCommentFormat" value="CHECKSTYLE\:SUPPRESS\:([\w\|]+)"/>
<property name="onCommentFormat" value="CHECKSTYLE\:UNSUPPRESS\:([\w\|]+)$"/>
<property name="checkFormat" value="$1"/>
</module>
<module name="SuppressWarningsFilter" />
<module name="TreeWalker">
<!-- Make comments available for the suppression comment filter. -->
<module name="FileContentsHolder"/>
<module name="SuppressWarningsHolder" />
<!-- Checks for Javadoc comments. -->
<!-- See http://checkstyle.sf.net/config_javadoc.html -->
<module name="JavadocMethod">
<property name="allowUndeclaredRTE" value="true" />
<property name="scope" value="package" />
</module>
<module name="JavadocType">
<property name="scope" value="package" />
</module>
<module name="JavadocStyle">
<property name="checkFirstSentence" value="true" />
<property name="checkEmptyJavadoc" value="true" />
</module>
<!-- Checks for Naming Conventions. -->
<!-- See http://checkstyle.sf.net/config_naming.html -->
<module name="ConstantName">
<property name="format" value="^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$|^log$" />
</module>
<module name="LocalFinalVariableName"/>
<module name="LocalVariableName"/>
<module name="MemberName"/>
<module name="MethodName"/>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="StaticVariableName"/>
<module name="TypeName"/>
<!-- Checks for imports -->
<!-- See http://checkstyle.sf.net/config_import.html -->
<module name="AvoidStarImport"/>
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
<module name="RedundantImport"/>
<module name="UnusedImports"/>
<!-- Checks for Size Violations. -->
<!-- See http://checkstyle.sf.net/config_sizes.html -->
<module name="LineLength">
<property name="max" value="${checkstyle.linelength}" default="150"/>
<property name="ignorePattern" value="(^ *\** *\$Id\:)|(^(package|import) .*)"/>
</module>
<module name="MethodLength"/>
<module name="ParameterNumber">
<property name="max" value="9" />
</module>
<module name="ReturnCount">
<property name="max" value="3" />
</module>
<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
<module name="EmptyForIteratorPad"/>
<module name="GenericWhitespace"/>
<module name="MethodParamPad"/>
<module name="NoWhitespaceAfter"/>
<module name="NoWhitespaceBefore"/>
<module name="OperatorWrap"/>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>
<module name="WhitespaceAfter">
<property name="tokens" value="COMMA, SEMI" />
</module>
<!-- Enforce WhitespaceAround the usual tokens, except GENERIC_*. -->
<module name="WhitespaceAround">
<property
name="tokens"
value="ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR,
BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN,
EQUAL, GE, GT, LAND, LCURLY, LE, LITERAL_ASSERT,
LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY,
LITERAL_FOR, LITERAL_IF, LITERAL_RETURN,
LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, LOR,
LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL,
PLUS, PLUS_ASSIGN, QUESTION, RCURLY, SL, SLIST, SL_ASSIGN,
SR, SR_ASSIGN, STAR, STAR_ASSIGN, TYPE_EXTENSION_AND"/>
</module>
<!-- Modifier Checks -->
<!-- See http://checkstyle.sf.net/config_modifiers.html -->
<module name="ModifierOrder"/>
<module name="RedundantModifier"/>
<!-- Checks for blocks. You know, those {}'s -->
<!-- See http://checkstyle.sf.net/config_blocks.html -->
<module name="EmptyBlock"/>
<module name="LeftCurly"/>
<module name="NeedBraces"/>
<module name="RightCurly"/>
<!-- Checks for common coding problems -->
<!-- See http://checkstyle.sf.net/config_coding.html -->
<module name="EmptyStatement"/>
<module name="EqualsHashCode"/>
<module name="HiddenField">
<property name="ignoreSetter" value="true"/>
<property name="ignoreConstructorParameter" value="true"/>
</module>
<module name="IllegalImport">
<property name="illegalPkgs"
value="edu.emory.mathcs.backport,
org.joda,
sun, com.sun"/>
</module>
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<module name="MagicNumber"/>
<module name="MissingSwitchDefault"/>
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>
<!-- Checks for class design -->
<!-- See http://checkstyle.sf.net/config_design.html -->
<module name="FinalClass"/>
<module name="HideUtilityClassConstructor"/>
<module name="InterfaceIsType"/>
<module name="VisibilityModifier">
<property name="protectedAllowed" value="true" />
</module>
<!-- Miscellaneous other checks. -->
<!-- See http://checkstyle.sf.net/config_misc.html -->
<module name="ArrayTypeStyle"/>
<module name="FinalParameters"/>
<module name="TodoComment">
<property name="severity" value="info"/>
</module>
<module name="UpperEll"/>
</module>
</module>
I keep getting the error: Unable to create Root Module: config {MY_PATH}
. When I used ./gradlew checkstyleMain -s
I get the exception:
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: SuppressionCommentFilter is not allowed as a child in Checker
at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:496)
at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:201)
at com.puppycrawl.tools.checkstyle.ant.CheckstyleAntTask.createRootModule(CheckstyleAntTask.java:424)
... 39 more
I went through the other answers but something or the other keeps breaking. I need help.
Gradle: 7.6.1
Kotlin: 1.7.10
Groovy: 3.0.13
Checkstyle: 8.45.1
Below is my checkstyle plugin definition:
checkstyle {
toolVersion = "8.45.1"
isIgnoreFailures = false
sourceSets = listOf(the<SourceSetContainer>()["main"])
configFile = file("checkstyle/checkstyle-rules.xml")
val suppressionFile = file("checkstyle/checkstyle-suppressions.xml")
configProperties["checkstyle.suppression.filter"] = suppressionFile
}
The SuppressionCommentFilter
after the version 8.1 of checkstyle is no longer child of Checker
but TreeWalker
instead.
This breaking change is described in the release notes for 8.1
Here is the PR for the changes along with a sugestion on how to migrate the SuppressionCommentFilter
in the newer versions with an example:
SuppressWithNearbyCommentFilter - just move config part under TreeWalker (from Checker). SuppressCommentFilter - Unfortunately we caused regression by this fix till 8.6 version for SuppressCommentFilter, upgrade is to use new Filter http://checkstyle.sourceforge.net/config_filters.html#SuppressWithPlainTextCommentFilter - just rename name of module. SuppressCommentFilter become a bit different Filter by functionality - become java code related only (can not suppress violations in comments anymore).