You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
\test> cat src/Test.java
record ColoredPoint(int p, int x, String c) {
}
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) {
}
public class Test {
void test(Object obj) {
switch (obj) {
case ColoredPoint(int x, _, _) -> {
System.out.println("line1");
System.out.println("line2");
System.out.println("line3");
System.out.println("line4");
System.out.println(x); // 5
}
default -> System.out.println("none");
}
if (obj instanceof ColoredPoint(int x, _, _)) {
System.out.println("line1");
System.out.println("line2");
System.out.println("line3");
System.out.println("line4");
System.out.println(x);
}
int x;
System.out.println("line1");
System.out.println("line2");
System.out.println("line3");
System.out.println("line4");
x = 1; // violation
}
}
PS D:\CS\test> cat config.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<property name="charset" value="UTF-8"/>
<module name="TreeWalker">
<module name="VariableDeclarationUsageDistance">
</module>
</module>
</module>
PS D:\CS\test> java -jar checkstyle-10.17.0-all.jar -c config.xml src/Test.java
Starting audit...
[ERROR] D:\CS\test\src\Test.java:28:9: Distance between variable 'x' declaration and its first usage is 5, but allowed 3. Consider making that variable final if you still need to store its value in advance (before method calls that might have side effects on the original value). [VariableDeclarationUsageDistance]
Audit done.
Checkstyle ends with 1 errors.
PS D:\CS\test>
Describe what you want in detail.
I don't think that this check should apply to pattern variables. We have no choice in the pattern variable declaration position. They are being declared either in ifs or case labels.
The text was updated successfully, but these errors were encountered:
child of #14961
I have read check documentation:https://checkstyle.org/checks/coding/variabledeclarationusagedistance.html#VariableDeclarationUsageDistance
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words
Describe what you want in detail.
I don't think that this check should apply to pattern variables. We have no choice in the pattern variable declaration position. They are being declared either in
ifs
orcase
labels.The text was updated successfully, but these errors were encountered: