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 #11885: Allow SuppressWarningHolder to suppress the violation with NameCheck #11889

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
Expand Up @@ -97,8 +97,8 @@
* </pre>
* <p>
* The general rule is that the argument of the {@code @SuppressWarnings} will be
* matched against class name of the checker in lower case and without {@code Check}
* suffix if present.
* matched against class name of the check in any letter case. Adding {@code check}
* suffix is also accepted.
* </p>
* <p>
* If {@code aliasList} property was provided you can use your own names e.g. below
Expand Down Expand Up @@ -155,7 +155,7 @@ public class SuppressWarningsHolder
private static final String JAVA_LANG_PREFIX = "java.lang.";

/** Suffix to be removed from subclasses of Check. */
private static final String CHECK_SUFFIX = "Check";
private static final String CHECK_SUFFIX = "check";

/** Special warning id for matching all the warnings. */
private static final String ALL_WARNING_MATCHING_ID = "all";
Expand All @@ -182,20 +182,21 @@ public class SuppressWarningsHolder

/**
* Returns the default alias for the source name of a check, which is the
* source name in lower case with any dotted prefix or "Check" suffix
* removed.
* source name in lower case with any dotted prefix or "Check"/"check"
* suffix removed.
*
* @param sourceName the source name of the check (generally the class
* name)
* @return the default alias for the given check
*/
public static String getDefaultAlias(String sourceName) {
int endIndex = sourceName.length();
if (sourceName.endsWith(CHECK_SUFFIX)) {
final String sourceNameLower = sourceName.toLowerCase(Locale.ENGLISH);
if (sourceNameLower.endsWith(CHECK_SUFFIX)) {
endIndex -= CHECK_SUFFIX.length();
}
final int startIndex = sourceName.lastIndexOf('.') + 1;
return sourceName.substring(startIndex, endIndex).toLowerCase(Locale.ENGLISH);
final int startIndex = sourceNameLower.lastIndexOf('.') + 1;
return sourceNameLower.substring(startIndex, endIndex);
}

/**
Expand Down Expand Up @@ -268,7 +269,8 @@ public static boolean isSuppressed(AuditEvent event) {
final String checkName = entry.getCheckName();
final boolean nameMatches =
ALL_WARNING_MATCHING_ID.equals(checkName)
|| checkName.equalsIgnoreCase(checkAlias);
|| checkName.equalsIgnoreCase(checkAlias)
|| getDefaultAlias(checkName).equalsIgnoreCase(checkAlias);
if (afterStart && beforeEnd
&& (nameMatches || checkName.equals(event.getModuleId()))) {
suppressed = true;
Expand Down
Expand Up @@ -43,6 +43,7 @@
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.api.Violation;
import com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck;
import com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck;
import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck;
import com.puppycrawl.tools.checkstyle.checks.whitespace.AbstractParenPadCheck;
import com.puppycrawl.tools.checkstyle.checks.whitespace.TypecastParenPadCheck;
Expand Down Expand Up @@ -505,4 +506,28 @@ public void testSuppressWarningsTextBlocks() throws Exception {
getNonCompilablePath("InputSuppressWarningsHolderTextBlocks.java"), expected);
}

@Test
public void testWithAndWithoutCheckSuffixDifferentCases() throws Exception {
final Configuration checkConfig = createModuleConfig(SuppressWarningsHolder.class);
final DefaultConfiguration treeWalker = createModuleConfig(TreeWalker.class);
final Configuration filter = createModuleConfig(SuppressWarningsFilter.class);
final DefaultConfiguration violationCheck = createModuleConfig(ConstantNameCheck.class);

treeWalker.addChild(checkConfig);
treeWalker.addChild(violationCheck);

final DefaultConfiguration root = createRootConfig(treeWalker);
root.addChild(filter);

final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
final String[] expected = {
"4:30: " + getCheckMessage(ConstantNameCheck.class,
AbstractNameCheck.MSG_INVALID_PATTERN, "a", pattern),
};

verify(root,
getPath("InputSuppressWarningsHolderWithAndWithoutCheckSuffixDifferentCases.java"),
expected);
}

}
@@ -0,0 +1,23 @@
package com.puppycrawl.tools.checkstyle.checks.suppresswarningsholder;

public class InputSuppressWarningsHolderWithAndWithoutCheckSuffixDifferentCases {
private static final int a = 0; // violation 'invalid pattern'

@SuppressWarnings("checkstyle:constantnamecheck") // filtered violation 'invalid pattern'
private static final int b = 0;

@SuppressWarnings("checkstyle:ConstantName") // filtered violation 'invalid pattern'
private static final int c = 0;

@SuppressWarnings("checkstyle:ConstantNameCheck") // filtered violation 'invalid pattern'
private static final int d = 0;

@SuppressWarnings("checkstyle:constantname") // filtered violation 'invalid pattern'
private static final int e = 0;

@SuppressWarnings("checkstyle:cOnStAnTnAmEcHeCk") // filtered violation 'invalid pattern'
private static final int f = 0;

@SuppressWarnings("checkstyle:cOnStAnTnAmE") // filtered violation 'invalid pattern'
private static final int g = 0;
}
4 changes: 2 additions & 2 deletions src/xdocs/config_annotation.xml
Expand Up @@ -1401,8 +1401,8 @@ class Test {
}
</source>
<p>The general rule is that the argument of the <code>@SuppressWarnings</code> will be
matched against class name of the checker in lower case and without <code>Check</code>
suffix if present.
matched against class name of the check in any letter case. Adding <code>check</code>
suffix is also accepted.
</p>
<p>If <code>aliasList</code> property was provided you can use your own names e.g. below
code will work if there was provided a <code>ParameterNumberCheck=paramnum</code> in
Expand Down