diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheckTest.java index 6c189aa7f51..a01780e2de2 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheckTest.java @@ -727,4 +727,21 @@ public void testMagicNumber3() throws Exception { verifyWithInlineConfigParser( getPath("InputMagicNumberMagicNumber3.java"), expected); } + + @Test + public void testMagicNumberInGuards() throws Exception { + final String[] expected = { + "21:63: " + getCheckMessage(MSG_KEY, "3"), + "21:72: " + getCheckMessage(MSG_KEY, "8"), + "27:58: " + getCheckMessage(MSG_KEY, "3"), + "27:68: " + getCheckMessage(MSG_KEY, "6"), + "31:50: " + getCheckMessage(MSG_KEY, "10.88"), + "33:46: " + getCheckMessage(MSG_KEY, "6"), + "39:59: " + getCheckMessage(MSG_KEY, "0.5"), + "39:67: " + getCheckMessage(MSG_KEY, "5"), + "44:23: " + getCheckMessage(MSG_KEY, "6"), + }; + verifyWithInlineConfigParser( + getNonCompilablePath("InputMagicNumberMagicNumberInGuards.java"), expected); + } } diff --git a/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/magicnumber/InputMagicNumberMagicNumberInGuards.java b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/magicnumber/InputMagicNumberMagicNumberInGuards.java new file mode 100644 index 00000000000..a61f631757b --- /dev/null +++ b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/magicnumber/InputMagicNumberMagicNumberInGuards.java @@ -0,0 +1,51 @@ +/* +MagicNumber +ignoreNumbers = (default)-1, 0, 1, 2 +ignoreHashCodeMethod = (default)false +ignoreAnnotation = (default)false +ignoreFieldDeclaration = (default)false +ignoreAnnotationElementDefaults = (default)true +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 + + +*/ + +//non-compiled with javac: Compilable with Java21 +package com.puppycrawl.tools.checkstyle.checks.coding.magicnumber; + +public class InputMagicNumberMagicNumberInGuards { + + void m(Object o) { + if (o instanceof Point(int x, int y, double z) && x < 3 && y > 8) {} + // 2 violations above: + // ''3' is a magic number' + // ''8' is a magic number' + + switch (o) { + case Point(int x, int y, double z) when (x < 3 && y != 6) -> {} + // 2 violations above: + // ''3' is a magic number.' + // ''6' is a magic number.' + case Point(_, _, double z) when z > (10.88) -> {} + // violation above, ''10.88' is a magic number' + case String s when s.length() != 6 -> {} + // violation above, ''6' is a magic number' + default -> {} + } + + int w = switch (o) { + case Point(int x, int y, double z) when (z == 0.5) -> 5; + // 2 violations above: + // ''0.5' is a magic number.' + // ''5' is a magic number.' + case String s -> { + yield 6; // violation, ''6' is a magic number' + } + default -> 0; + }; + } + + record Point(int x, int y, double z) { } +}