-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
VariableDeclarationUsageDistance: false negative when processing SWITCH_RULE
#12103
Comments
@stoyanK7 please add to your issue description actual/ expected for the following:
|
@nick-mancuso Added more examples. I hope this is what you meant. I have not added examples of switch statements because they are handled properly by the check as shown in the test above. The problem is with switch expressions in particular. |
Prove via CLI output that switch statement behavior is what you expect with variable initialization outside of the switch statement. |
@nick-mancuso stoyan@ZenBook:~/Desktop/checkstyle$ cat config.xml <?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="VariableDeclarationUsageDistance">
<property name="validateBetweenScopes" value="true"/>
<property name="allowedDistance" value="1"/>
</module>
</module>
</module> stoyan@ZenBook:~/Desktop/checkstyle$ cat Test1.java class Test {
void switchExample(int x) {
int variable = 0;
switch(x) {
case 1:
x++;
x++;
x++;
x++;
variable++;
break;
}
}
} stoyan@ZenBook:~/Desktop/checkstyle$RUN_LOCALE="-Duser.language=en -Duser.country=US"
stoyan@ZenBook:~/Desktop/checkstyle$ java $RUN_LOCALE -jar checkstyle-10.3.2-all.jar -c config.xml Test1.java
Starting audit...
[ERROR] /home/stoyan/Desktop/checkstyle/Test1.java:3:9: Distance between variable 'variable' declaration and its first usage is 5, but allowed 1. 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.
|
@stoyanK7 why did we extend original example?
|
@nick-mancuso Alright, this now seems more messed up than before. int m2(int x) {
int y = 5; // no violation
switch (x) {
case 0:
x++;
y = 0;
break;
}
return y;
} But if we remove int m2(int x) {
int y = 5; // violation here
switch (x) {
case 0:
x++;
y = 0;
break;
}
return 5;
} This behavior is not expected, right? |
I think it makes sense to not violate variable if we use it after whatever the "inner scope" is. But - I wonder if this is by accident or design; I am not sure if this logic is included in the check. It looks like we need to do some investigation to identify what the real issue here is. Please continue to test different constructs (loops, static blocks, etc.) and variable usage before/after "inner scope" and refine the PR description as you discover real issue. |
yes. I pretty sure it is by design as scopes significantly increase complexity of this validation. If there is usage after nested scope, we can not do violation on variable. lets check on code like:
and
on second there should be no violation, so first case might also fall in this pattern. Can you double check by CLI ? May be it is good extension in validaiton, but it is more like problem of reducing scope/visibility of variable rather then Distance. |
@romani apparently, both are violation stoyan@ZenBook:~/Desktop/checkstyle$ cat config.xml <?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="VariableDeclarationUsageDistance">
<property name="validateBetweenScopes" value="true"/>
<property name="allowedDistance" value="1"/>
</module>
</module>
</module> stoyan@ZenBook:~/Desktop/checkstyle$ cat Test1.java class Test1 {
void doo() { }
void example1() {
int a = 0;
if (true) {
doo();
doo();
doo();
a++;
}
}
void example2() {
int a = 0;
if (true) {
doo();
doo();
doo();
a++;
}
}
} stoyan@ZenBook:~/Desktop/checkstyle$ java -jar checkstyle-10.3.2-all.jar -c config.xml Test1.java
|
you did mistake in Input, that is why, see a difference:
So logic is smart to handle scopes already for IF, so any other scope should be handled kind of the same. |
I have read check documentation: https://checkstyle.org/config_coding.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
Discovered while working on #12052.
stoyan@ZenBook:~/Desktop/checkstyle$ cat config.xml
stoyan@ZenBook:~/Desktop/checkstyle$ cat Test.java
Current output
Expected output
I believe this issue needs to be resolved before killing the surviving mutation in #12052. A test case already exists for
CASE_GROUP
that kills a mutation similar to the surviving one:checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceGeneral.java
Lines 299 to 305 in 83c91b1
The text was updated successfully, but these errors were encountered: