Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #11655: allow suppressing Google style warnings with annotations and comments #11656

Merged
merged 2 commits into from Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/suppressions.xml
Expand Up @@ -6,7 +6,7 @@

<suppressions>
<!-- can't split long messages between lines -->
<suppress id="lineLengthXml" files="google_checks\.xml" lines="56,127"/>
<suppress id="lineLengthXml" files="google_checks\.xml" lines="58,127"/>
<!-- don't validate generated files -->
<suppress id="lineLengthXml" files="releasenotes_old_6\-0_7\-8\.xml"/>
<suppress id="lineLengthXml" files="releasenotes_old_1\-0_5\-9\.xml"/>
Expand Down
17 changes: 16 additions & 1 deletion src/main/resources/google_checks.xml
Expand Up @@ -16,7 +16,9 @@
Authors: Max Vetrenko, Ruslan Diachenko, Roman Ivanov.
-->

<module name = "Checker">
<module name="Checker">
romani marked this conversation as resolved.
Show resolved Hide resolved
<module name="SuppressWarningsFilter"/>

<property name="charset" value="UTF-8"/>

<property name="severity" value="warning"/>
Expand Down Expand Up @@ -363,5 +365,18 @@
default="checkstyle-xpath-suppressions.xml" />
<property name="optional" value="true"/>
</module>
<module name="SuppressWarningsHolder" />
<module name="SuppressionCommentFilter">
<property name="offCommentFormat" value="CHECKSTYLE.OFF\: ([\w\|]+)" />
<property name="onCommentFormat" value="CHECKSTYLE.ON\: ([\w\|]+)" />
<property name="checkFormat" value="$1" />
</module>
<module name="SuppressWithNearbyCommentFilter">
<property name="commentFormat" value="CHECKSTYLE.SUPPRESS\: ([\w\|]+)"/>
<!-- $1 refers to the first match group in the regex defined in commentFormat -->
<property name="checkFormat" value="$1"/>
<!-- The check is suppressed in the next line of code after the comment -->
<property name="influenceFormat" value="1"/>
</module>
</module>
</module>
Expand Up @@ -1541,6 +1541,10 @@ public void testAllStyleRules() throws Exception {
styleChecks.remove("SuppressionXpathSingleFilter");
styleChecks.remove("TreeWalker");
styleChecks.remove("Checker");
styleChecks.remove("SuppressWithNearbyCommentFilter");
styleChecks.remove("SuppressionCommentFilter");
styleChecks.remove("SuppressWarningsFilter");
styleChecks.remove("SuppressWarningsHolder");

assertWithMessage(
fileName + " requires the following check(s) to appear: " + styleChecks)
Expand Down
4 changes: 4 additions & 0 deletions src/xdocs/config_annotation.xml
Expand Up @@ -1411,6 +1411,10 @@ public void someFunctionWithInvalidStyle() {
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressWarningsHolder">
Checkstyle Style</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressWarningsHolder">
Google Style</a>
</li>
</ul>
</subsection>

Expand Down
12 changes: 12 additions & 0 deletions src/xdocs/config_filters.xml
Expand Up @@ -422,6 +422,10 @@ public class UserService {
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressionCommentFilter">
Checkstyle Style</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressionCommentFilter">
Google Style</a>
</li>
</ul>
</subsection>
<subsection name="Package" id="SuppressionCommentFilter_Package">
Expand Down Expand Up @@ -1934,6 +1938,10 @@ public static void foo() {
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressWarningsFilter">
Checkstyle Style</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressWarningsFilter">
Google Style</a>
</li>
</ul>
</subsection>
<subsection name="Package" id="SuppressWarningsFilter_Package">
Expand Down Expand Up @@ -2209,6 +2217,10 @@ public class UserService {
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressWithNearbyCommentFilter">
Checkstyle Style</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressWithNearbyCommentFilter">
Google Style</a>
</li>
</ul>
</subsection>
<subsection name="Package" id="SuppressWithNearbyCommentFilter_Package">
Expand Down
57 changes: 49 additions & 8 deletions src/xdocs/google_style.xml
Expand Up @@ -2302,24 +2302,65 @@
<p>
It is possible to suppress some violations by embeded filters
<a href="config_filters.html#SuppressionFilter">
SuppressionFilter</a>
and
SuppressionFilter</a>,
<a href="config_filters.html#SuppressionXpathFilter">
SuppressionXpathFilter</a>.
Location of config file for <i>SuppressionFilter</i> can be defined by system property
<i>org.checkstyle.google.suppressionfilter.config</i> (default value is
<i>checkstyle-suppressions.xml</i>).
Location of config file for <i>SuppressionXpathFilter</i>
SuppressionXpathFilter</a>,
<a href="config_filters.html#SuppressWithNearbyCommentFilter">
SuppressWithNearbyCommentFilter</a>,
<a href="config_filters.html#SuppressionCommentFilter">
SuppressionCommentFilter</a>
and
<a href="config_filters.html#SuppressWarningsFilter">
SuppressWarningsFilter</a>.
</p>
<p>
Location of config file for
<a href="config_filters.html#SuppressionFilter">SuppressionFilter</a> can
be defined by system property <i>org.checkstyle.google.suppressionfilter.config</i>
(default value is <i>checkstyle-suppressions.xml</i>).
</p>
<p>
Location of config file for
<a href="config_filters.html#SuppressionXpathFilter">SuppressionXpathFilter</a>
can be defined by system property
<i>org.checkstyle.google.suppressionxpathfilter.config</i> (default value is
<i>checkstyle-xpath-suppressions.xml</i>).
</p>
<p>
To suppress a check in the next line of code using
<a href="config_filters.html#SuppressWithNearbyCommentFilter">
SuppressWithNearbyCommentFilter</a>precede the line of code with
<i>// CHECKSTYLE.SUPPRESS: NameOfTheCheck</i> (replace <i>NameOfTheCheck</i> with
the actual check to be suppressed).
</p>
<p>
To suppress a check on a block of code using
<a href="config_filters.html#SuppressionCommentFilter">SuppressionCommentFilter</a>
add <i>// CHECKSTYLE.OFF: NameOfTheCheck</i> before the beginning of the code block and
<i>// CHECKSTYLE.ON: NameOfTheCheck</i> after the end of the code block (replace
<i>NameOfTheCheck</i> with the actual check to be suppressed).
</p>
<p>
To suppress a check using
<a href="config_filters.html#SuppressWarningsFilter">SuppressWarningsFilter</a>
use Java's<i>@SuppressWarnings({"checkstyle:name_of_the_check", ...})</i> annotation.
Each value passed in the array parameter must meet the format
<i>checkstyle:name_of_the_check</i> where the <i>checkstyle:</i> is optional and
<i>name_of_the_check</i> is the actual name of check to be suppressed in lowercase
format.
</p>
<p>
For more details please review exact configuration of Filters in google_checks.xml:
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressionFilter">
SuppressionFilter</a>,
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressionXpathFilter">
SuppressionXpathFilter</a>.
SuppressionXpathFilter</a>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressWithNearbyCommentFilter">
SuppressWithNearbyCommentFilter</a>,
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressionCommentFilter">
SuppressionCommentFilter</a>,
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressWarningsFilter">
SuppressWarningsFilter</a>,
</p>
</subsection>
</section>
Expand Down