I have read check documentation: https://checkstyle.org/checks/coding/variabledeclarationusagedistance.html
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
vivek@Viveks-MacBook-Air tmp % cat > /var/tmp/TestTryDistance.java <<'EOF'
/** Test class for VariableDeclarationUsageDistance try-block false negative. */
public class TestTryDistance {
/** Case 1: usage inside if, correctly flagged. */
void testIf() {
int x = 10;
System.lineSeparator();
System.lineSeparator();
System.lineSeparator();
if (x > 5) {
System.out.println("yes");
}
}
/** Case 2: identical pattern but usage inside try, NOT flagged (bug). */
void testTry() {
int x = 10;
System.lineSeparator();
System.lineSeparator();
System.lineSeparator();
try {
System.out.println(x);
} catch (Exception e) {
e.printStackTrace();
}
}
/** Case 3: usage inside for, correctly flagged. */
void testFor() {
int x = 10;
System.lineSeparator();
System.lineSeparator();
System.lineSeparator();
for (int i = 0; i < 1; i++) {
System.out.println(x);
}
}
}
EOF
vivek@Viveks-MacBook-Air tmp % cat > /var/tmp/config.xml <<'EOF'
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="VariableDeclarationUsageDistance"/>
</module>
</module>
EOF
vivek@Viveks-MacBook-Air tmp % javac TestTryDistance.java
vivek@Viveks-MacBook-Air tmp % RUN_LOCALE="-Duser.language=en -Duser.country=US"
java $RUN_LOCALE -jar /Users/vivek/checkstyle/target/checkstyle-13.4.1-SNAPSHOT-all.jar -c config.xml TestTryDistance.java
Starting audit...
[ERROR] /private/var/tmp/TestTryDistance.java:6:5: Distance between variable 'x' declaration and its first usage is 4, 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]
[ERROR] /private/var/tmp/TestTryDistance.java:30:5: Distance between variable 'x' declaration and its first usage is 4, 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 2 errors.
Describe what you expect in detail.
All three methods (testIf, testTry, testFor) have the identical pattern: a variable declared, 3 statements, then first usage inside a block. The distance is 4 in all cases.
Expected: All three should produce a violation (distance 4 > allowed 3).
Actual: testIf and testFor are correctly flagged, but testTry is not flagged.
I have read check documentation: https://checkstyle.org/checks/coding/variabledeclarationusagedistance.html
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 expect in detail.
All three methods (testIf, testTry, testFor) have the identical pattern: a variable declared, 3 statements, then first usage inside a block. The distance is 4 in all cases.
Expected: All three should produce a violation (distance 4 > allowed 3).
Actual: testIf and testFor are correctly flagged, but testTry is not flagged.