Skip to content

Commit

Permalink
Issue #14788: fix NPE for MagicNumberCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Lmh-java authored and rnveach committed Apr 24, 2024
1 parent ad6bfa8 commit ef2f38a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Expand Up @@ -387,7 +387,7 @@ private boolean isInIgnoreList(DetailAST ast) {
private static boolean isFieldDeclaration(DetailAST ast) {
DetailAST varDefAST = null;
DetailAST node = ast;
while (node.getType() != TokenTypes.OBJBLOCK) {
while (node != null && node.getType() != TokenTypes.OBJBLOCK) {
if (node.getType() == TokenTypes.VARIABLE_DEF) {
varDefAST = node;
break;
Expand Down
Expand Up @@ -632,6 +632,19 @@ public void testMagicNumberIgnoreFieldDeclarationRecords()
expected);
}

@Test
public void testMagicNumberIgnoreFieldDeclarationWithAnnotation()
throws Exception {
final String[] expected = {
"16:38: " + getCheckMessage(MSG_KEY, "3"),
"20:40: " + getCheckMessage(MSG_KEY, "60"),
"21:34: " + getCheckMessage(MSG_KEY, "20"),
};
verifyWithInlineConfigParser(
getPath("InputMagicNumberIgnoreFieldDeclarationWithAnnotation.java"),
expected);
}

@Test
public void testIgnoreInAnnotationElementDefault() throws Exception {
final String[] expected = {
Expand Down
@@ -0,0 +1,26 @@
/*
MagicNumber
ignoreNumbers = 0.99
ignoreHashCodeMethod = (default)false
ignoreAnnotation = (default)false
ignoreFieldDeclaration = true
ignoreAnnotationElementDefaults = false
constantWaiverParentToken = (default)TYPECAST, METHOD_CALL, EXPR, ARRAY_INIT, UNARY_MINUS, \
UNARY_PLUS, ELIST, STAR, ASSIGN, PLUS, MINUS, DIV, LITERAL_NEW
tokens = (default)NUM_DOUBLE, NUM_FLOAT, NUM_INT, NUM_LONG
*/
package com.puppycrawl.tools.checkstyle.checks.coding.magicnumber;

@InputMagicNumberIntMethodAnnotation(3) // violation
public class InputMagicNumberIgnoreFieldDeclarationWithAnnotation {
public void createEvents(Double d, String s) {
if ((d != null) && (s != null && s.equalsIgnoreCase("Fiit"))) {
double anotherDouble = d / 60; // violation
if (anotherDouble >= 20) { // violation
// do something
}
}
}
}

0 comments on commit ef2f38a

Please sign in to comment.