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
Issue #11720: Kill surviving mutation in VariableDeclarationUsageDistanceCheck in getFirstNodeInsideIfBlock #11972
Conversation
Github, generate report |
143b6e0
to
78d2c37
Compare
Github, generate report |
validateBetweenScopesFalse: https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/143b6e0_2022191919/reports/diff/index.html |
validateBetweenScopesFalse: https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/78d2c37_2022195745/reports/diff/index.html |
Github, generate report |
validateBetweenScopesTrue: https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/78d2c37_2022043220/reports/diff/index.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Vyom-Yadav are we able to cleanly separate changes here into two PRs?
We can, but it is not recommended, the |
86b7ae3
to
3fe931a
Compare
Github, generate report |
validateBetweenScopesTrue: https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/3fe931a_2022125544/reports/diff/index.html |
3fe931a
to
510a266
Compare
@Vyom-Yadav why such a large discrepancy between reports? |
I added support for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Item:
...ava/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java
Outdated
Show resolved
Hide resolved
cb2d9ce
to
e998b83
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Item:
...s/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceGeneral2.java
Outdated
Show resolved
Hide resolved
e998b83
to
4bf25e9
Compare
4bf25e9
to
fd21687
Compare
} | ||
final Optional<DetailAST> slistToken = TokenUtil | ||
.findFirstTokenByPredicate(block, token -> token.getType() == TokenTypes.SLIST); | ||
final DetailAST currentNode = block.getLastChild(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please rename it to lastNode
(since there is no loop anymore and it has only one value)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
c++; | ||
if (true) | ||
d++; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When return
is the first statement in if
the last child is not SEMI
:
if (true) return a;
LITERAL_IF -> if [3:8]
|--LPAREN -> ( [3:11]
|--EXPR -> EXPR [3:12]
| `--LITERAL_TRUE -> true [3:12]
|--RPAREN -> ) [3:16]
`--LITERAL_RETURN -> return [3:18]
|--EXPR -> EXPR [3:25]
| `--IDENT -> a [3:25]
`--SEMI -> ; [3:26]
And it doesn't catch a violation in such code:
String testReturn() {
int a = 1; // violation
System.out.println();
System.out.println();
System.out.println();
if (true)
return String.valueOf(a);
return null;
}
Can you please add it to the inputs too and test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested it, it won't give a violation, the if
statement evaluations works correctly, but this is considered by check logic as an initialization sequence
(isInitializationSequence = true
), so it does not give a violation finally at:
Lines 417 to 418 in f4ab4aa
if (dist > allowedDistance | |
&& !isInitializationSequence(variableUsageAst, variable.getText())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a similar example as proof of working on current changes.
…nUsageDistanceCheck in getFirstNodeInsideIfBlock
fd21687
to
a1fa6a8
Compare
Closes #11720
Check documentation: https://checkstyle.sourceforge.io/config_coding.html#VariableDeclarationUsageDistance
Diff Reports (Contains diff, Issue for NPE at #11973):
Rationale
There is no need to loop and check all the statements in the
if-else
chain at once, just check the current statement, the next statement (if present), and the outer method i.e.calculateDistanceBetweenScopes
will iterate over the remaining branches, we just add the head of the remaining branches tovariableUsageExpressions
for it to be analyzed in the next iteration.Also support for
if
statements without{}
was added, it rationale is present below.Previous Logic:
If we see the initial logic, we get the if statements last-child, for if statements without
{}
, it will be:AST-
Now the last child is
SEMI
and the according to the logic, it was supposed to be if statements body.The logic was built for if statements with
{}
where the last child isSLIST
i.e. if statement's body.Cases like this in combination with
else
were missing, support for that was added.Generating reports again:
Diff Regression config: https://gist.githubusercontent.com/Vyom-Yadav/98dceb63a79f4833e85fff9b2e1464a6/raw/a06fa3c8f525c4466a67c1ee057d4ed843db2301/my_checks.xml
Report label: validateBetweenScopesTrue