-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Issue #14344: Fix GenericWhitespace Check ignoring exception to the '>' rule for succeeding whitespaces #14497
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 #14344: Fix GenericWhitespace Check ignoring exception to the '>' rule for succeeding whitespaces #14497
Conversation
Report generation failed on phase "parse_body", |
GitHub, generate report or else... 😈🔪 |
@@ -54,6 +54,7 @@ public void testWhitespaceAroundGenerics() throws Exception { | |||
"14:46: " + getCheckMessage(messages, msgPreceded, ">"), | |||
"15:33: " + getCheckMessage(messages, msgFollowed, "<"), | |||
"15:33: " + getCheckMessage(messages, msgPreceded, "<"), | |||
"15:46: " + getCheckMessage(messages, msgFollowed, ">"), |
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.
All the similar additions in expected violations is due to following:
In this instance the extra violation is in the following line:
Line 15 in 9842608
new java.util.Hashtable < Integer, D > (); //warn |
Before the fix, check didn't considered the whitespace between
>
(
as a violation, this is now correctly identified in the fix.
src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck.java
Outdated
Show resolved
Hide resolved
* @param ast ast | ||
* @return true if generic after {@code LITERAL_NEW} | ||
*/ | ||
private static boolean isGenericAfterNew(DetailAST ast) { |
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.
Method name change from isGenericBeforeCtor()
to isGenericAfterNew()
, this describes method's behaviour more accurately, as it only identified the cases where generic existed after new
keyword.
The method didn't identified cases where generic preceded constructor invocation ()
which was the cause for the false negative.
The method now also identifies a new similar case for Annotations (case 3.)
// | ||
// new <String>Object() | ||
// ^ | ||
// 3. +--- ws required |
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.
Added example new <String>Object()
in the comments as isGenericBeforeCtor()
(now isGenericAfterNew()
) was added earlier to be used in this method to detect a new type of case for which a preceding whitespace was required, but the comment was not updated in that commit to reflect this new behaviour.
package com.puppycrawl.tools.checkstyle.checks.whitespace.genericwhitespace; | ||
|
||
public class InputGenericWhitespaceBeforeRecordHeader { | ||
record Session<T>() {} |
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.
Disclaimer: Contains some off-topic observations.
The purpose of this input and the corresponding test is to kill the mutation which survived after the changes in Check.
Surviving mutation was replacing charAfter == '('
with false
in the following line:
Line 347 in 9842608
return charAfter == '(' || charAfter == ')' |
Why this didn't survived before:
Before the fix, check didn't identified all the cases where generic was present before constructor invocation, which meant some CTOR
cases where >
was succeeded with (
weren't filtered through the following if
statement:
Line 222 in 9842608
if (isGenericBeforeMethod(ast) || isGenericBeforeCtor(ast)) { |
Such cases went through
isCharacterValidAfterGenericEnd()
in the following else if
block, making the mutant produce illegal follow violation
, hence killing the mutant.Due to the fix, all the possible existing cases where
>
could be succeeded by (
were filtered through the if
statement, meaning none of the inputs went through isCharacterValidAfterGenericEnd()
, hence creating that surviving mutation.
So after a bit of mutant hunting to find a valid input where >
could be succeeded by (
but wasn't before Method or Ctor, found out that such valid input exist for generic before record header, which was able to kill the mutant.
As of now, we don't have a clear definition in the documentation on how to handle whitespace for generic that is before a record header, current check does not count space between >
and (
of record header as a violation, will open an Issue shortly for discussing if it should(See #14502), based on the decision of the discussion, will update the Check accordingly and strengthen the test inputs for the records in a separate PR.
Funny thing is that if there is supposed to be violation for the case above, this will revive the mutant, as we will now be filtering the killing input in the if statement(this in my knowledge makes for all possible cases where generic can be followed by (
), which will mean that the surviving mutated operand could be safely removed if no regressions are found and no one is aware of such input.
*/ | ||
private static boolean isGenericBeforeCtor(DetailAST ast) { | ||
private static boolean isGenericBeforeCtorInvocation(DetailAST ast) { |
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.
The method is used to identify cases where >
should not be followed by whitespace, fixing the false negative.
Failing checks in CI are unrelated. (Cirrus, YAML Validation for site.xml) |
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.
Please extend inputs by a bit more cases.
We need to not rely on regression too much in future, so we collecting all different types of how code can be written.
...rawl/tools/checkstyle/checks/whitespace/genericwhitespace/InputGenericWhitespaceDefault.java
Show resolved
Hide resolved
28c7818
to
0eaeb24
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.
items:
...eckstyle/checks/whitespace/genericwhitespace/InputGenericWhitespaceBeforeCtorInvocation.java
Outdated
Show resolved
Hide resolved
3c611a0
to
c5abe70
Compare
…n to the '>' rule for succeeding whitespaces
c5abe70
to
d79288a
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.
ok to merge
Resolves #14344
Reasoning for the changes are mentioned in the corresponding review comments of mine.
Regression: (Report)
Differences found (expected).
This added violation was not detected before (a false negative, reasoning in the description of the Issue this PR resolves)
This removed violation is a false positive which was detected as a violation before because generics enclosing Annotations that were succeeding
new
keyword weren't detected as the cases where they should be preceded with a whitespace.These cases are now detected properly in the fix (See comment).
Diff Regression config: https://gist.githubusercontent.com/sktpy/44a31ea4e946a0755fa3e331fd65de98/raw/50e5257788a0f001f2ad184b1a7cbac7910e8b5b/check.xml