Skip to content
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 #11973: Fix NPE on empty switch statements in VariableDeclarati… #12052

Conversation

stoyanK7
Copy link
Collaborator

@stoyanK7 stoyanK7 commented Aug 11, 2022

@stoyanK7 stoyanK7 force-pushed the issue/11973-fix-NPE-empty-switch-VariableDeclarationUsageDistance branch from c6daab6 to 83e8eb9 Compare August 11, 2022 16:07
@nrmancuso nrmancuso self-requested a review August 12, 2022 01:38
@nrmancuso nrmancuso self-assigned this Aug 12, 2022
Copy link
Member

@nrmancuso nrmancuso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see failures in pitest and generate check regression report.

Item:

@stoyanK7
Copy link
Collaborator Author

stoyanK7 commented Aug 25, 2022

@nick-mancuso

While attempting to kill the mutation I discovered that the following code does not raise any violations. Shall I make an issue for it? I believe the newfound bug first needs to be fixed before I can proceed with the current issue.

int issue11973(int a) {
    int sw = 5; // violation 'Distance between variable ''sw'' declaration and its first usage is 2, but allowed 1.'
    return switch (a) {
        case 0 -> {
            a++;
            yield sw + a;
        }
    };
}

@nrmancuso
Copy link
Member

@nick-mancuso

While attempting to kill the mutation I discovered that the following code does not raise any violations. Shall I make an issue for it? I believe the newfound bug first needs to be fixed before I can proceed with the current issue.

Please make an issue for this, make sure to explain why we need to fix new issue before we complete this PR.

@romani
Copy link
Member

romani commented Sep 5, 2022

yes, better to separate issue to be addressed in separate issues/PRs.
you can combine fixing few problems in one PR, if code is fix in code is close to each other and "same" problem/context.

@romani
Copy link
Member

romani commented Sep 5, 2022

@stoyanK7 , NPE is blocker to use Check at all, can we continue this PR to at least fix NPE and even fine with false positived.

Exception - force users to disable Check at all in config.
false positive - force users to suppress certain file of user code or specific violation.
false negative - does not bother users at all.

So lets fix exception as primary goal.

@stoyanK7
Copy link
Collaborator Author

stoyanK7 commented Sep 5, 2022

@romani Alright, will focus my attention to fix this NPE :)

@stoyanK7 stoyanK7 force-pushed the issue/11973-fix-NPE-empty-switch-VariableDeclarationUsageDistance branch from 83e8eb9 to 3fc7939 Compare September 9, 2022 12:34
Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Items

@stoyanK7 stoyanK7 force-pushed the issue/11973-fix-NPE-empty-switch-VariableDeclarationUsageDistance branch from 3fc7939 to 03490f0 Compare September 11, 2022 12:51
Copy link
Collaborator Author

@stoyanK7 stoyanK7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So pitest-coding-1 is going to fail with the following mutation here:

Unnecessary suppressed mutation(s) found and should be removed:

Source File: "VariableDeclarationUsageDistanceCheck.java"
Class: "com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck"
Method: "lambda$getFirstCaseGroupOrSwitchRule$2"
Line Contents: ".orElseGet(() -> block.findFirstToken(TokenTypes.SWITCH_RULE));"
Mutator: "org.pitest.mutationtest.engine.gregor.mutators.experimental.NakedReceiverMutator"
Description: "replaced call to com/puppycrawl/tools/checkstyle/api/DetailAST::findFirstToken with receiver"

Can anybody help me and explain what it does(how the code looks after mutation) since documentation is scarce(practically nonexistent).
This is what I could find

@rnveach
Copy link
Member

rnveach commented Sep 11, 2022

"replaced call to com/puppycrawl/tools/checkstyle/api/DetailAST::findFirstToken with receiver"

I believe the mutation is replacing block.findFirstToken(TokenTypes.SWITCH_RULE) with block.
This is clearly shown in the javadoc, https://github.com/hcoles/pitest/blob/master/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/mutators/experimental/NakedReceiverMutator.java#L31-L46 .

@stoyanK7
Copy link
Collaborator Author

@rnveach Already tried that and testVariableDeclarationUsageDistanceSwitchExpressions2() fails when block.findFirstToken(TokenTypes.SWITCH_RULE) is changed to just block:
Screenshot from 2022-09-11 15-07-04

@rnveach
Copy link
Member

rnveach commented Sep 11, 2022

@stoyanK7 Please read the top of the pitest report from CI.

Unnecessary suppressed mutation(s) found and should be removed:

It means you are now killing this mutation and can remove it from the suppressions.

@stoyanK7
Copy link
Collaborator Author

stoyanK7 commented Sep 11, 2022

@rnveach Oh my god, you are right, thanks! I should have actually read whats up. I assumed it is a surviving one since that was always the case previously, but it turns out pitest also fails when there are unnecessarily suppressed mutations. Removing it right now.

@stoyanK7 stoyanK7 force-pushed the issue/11973-fix-NPE-empty-switch-VariableDeclarationUsageDistance branch 2 times, most recently from 434d66d to 40df68e Compare September 11, 2022 18:43
@stoyanK7 stoyanK7 force-pushed the issue/11973-fix-NPE-empty-switch-VariableDeclarationUsageDistance branch 2 times, most recently from 4790a3e to cc48160 Compare September 11, 2022 19:04
Copy link
Member

@nrmancuso nrmancuso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update documentation for getFirstCaseGroupOrSwitchRule to mention that we will return null if switch block is empty.

@stoyanK7 stoyanK7 force-pushed the issue/11973-fix-NPE-empty-switch-VariableDeclarationUsageDistance branch from cc48160 to 5ae7d1a Compare September 11, 2022 20:08
Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please review drone CI falure:

src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceCheckSwitchExpressions2.java:28: error: yield outside of switch expression
yield 2;
^

item:

Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

items

@stoyanK7 stoyanK7 force-pushed the issue/11973-fix-NPE-empty-switch-VariableDeclarationUsageDistance branch 2 times, most recently from 8f631c4 to b99dd4c Compare September 12, 2022 20:00
@nrmancuso nrmancuso removed their assignment Sep 13, 2022
@stoyanK7 stoyanK7 force-pushed the issue/11973-fix-NPE-empty-switch-VariableDeclarationUsageDistance branch from b99dd4c to 2e7da62 Compare September 14, 2022 11:42
Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Items

@stoyanK7 stoyanK7 force-pushed the issue/11973-fix-NPE-empty-switch-VariableDeclarationUsageDistance branch from 2e7da62 to 3fcdd10 Compare September 15, 2022 08:10
Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Items

@stoyanK7 stoyanK7 force-pushed the issue/11973-fix-NPE-empty-switch-VariableDeclarationUsageDistance branch 2 times, most recently from 395b380 to ded47a1 Compare September 15, 2022 14:37
Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Items

final List<String> children = new ArrayList<>(3);
final List<Integer> tokensToMatch = List.of(TokenTypes.CLASS_DEF, TokenTypes.VARIABLE_DEF);

TokenUtil.forEachChild(astForTest, tokensToMatch, child -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might was no clear. I know it is very unusual and sattle.
We allow tests only by Inputs with embedded config.
All other forms of testing are well proven "way to hell" in our repo .

Please no updates in TokenUtilTest.

Let's come back to "map.or" solution , sorry that we confused you and lost valuable time.

Let me know if you interested in more details why we follow unusual philosophy

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inputs with embedded config.

This is not possible here since it is not a check/filter itself.


Please no updates in TokenUtilTest
Let's come back to "map.or" solution

Reverted back. Done.


It's alright. I would not call it wasted time. Dug around the codebase and learned a few things about the rules of this repository and how pull requests should be.

All other forms of testing are well proven "way to hell" in our repo .
Let me know if you interested in more details why we follow unusual philosophy

Yes, I'd like to know why. In my eyes, such a test does not seem harder to maintain than any other check's tests and input files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try our best to use tests only like verifyWithInlineConfigParser(getNonCompilablePath(filename).

It makes us confident that we always use compilable input files. And all code in Check can handle any possible-and-compilable combinations of java code and all code in Check are there for a reason (pitest) no extra code and no dead code. Removal/mutation of any part of it immediately detected by Junit tests.

As you might notice in above statement we do not mention about util methods at all , as potentially until method can be copied to class and be part of it. It is our details of implementation that we use util methods, we do not want to let anyone know it, and it subject to change at any time.

If we allow testing of utility methods separately, all logic of all Checks can be generalized and moved to utilities so whole test pressure will also move to utilities. And we will struggle with auto measure of quality of Check.
In result maintainers will win in short term, user will loose in long term, usage will drop, maintainers will not found way to invite new contributions, project is dead eventually.

Right now as you might noticed, new contributor does not need to know all ast and all utilities and all rules. All you need is to put a java file to test and place debug in Check visitToken.

Copy link
Member

@romani romani Sep 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We did not born smart, it took us 20 years to do all types of testing to end up where we are. So are of code is still old style

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get it now - all in the name of maintainability and keeping the project going and easy to contribute to. Which I have noticed btw.

What does that mean for the util tests though? Do you want them to move over to such testing verifyWithInlineConfigParser? Is it even possible? Or do you expect tests from checks that use util methods to cover them?

@stoyanK7 stoyanK7 force-pushed the issue/11973-fix-NPE-empty-switch-VariableDeclarationUsageDistance branch 2 times, most recently from 4fe5f6f to 4dab5af Compare September 15, 2022 19:34
Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok to merge

@romani romani assigned nrmancuso and unassigned romani Sep 15, 2022
@stoyanK7
Copy link
Collaborator Author

Github, generate report

@github-actions
Copy link
Contributor

https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/4dab5af_2022060049/reports/diff/index.html

@romani
Copy link
Member

romani commented Sep 16, 2022

OMG, we had exception on project that we in regression list. We might need to have CI script that runs updated in commit check against all such projects to expect no exceptions.

CIs are restarted

Copy link
Member

@nrmancuso nrmancuso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Item:

@rnveach
Copy link
Member

rnveach commented Sep 16, 2022

we had exception on project that we in regression list.

Regression config is missing turning on validateBetweenScopes as seen in regression report
https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/4dab5af_2022060049/reports/diff/openjdk17/index.html
https://github.com/checkstyle/contribution/blob/master/checkstyle-tester/checks-nonjavadoc-error.xml#L349

Also want to remind we are missing checkstyle/contribution#548 from regression config as well

@stoyanK7 stoyanK7 force-pushed the issue/11973-fix-NPE-empty-switch-VariableDeclarationUsageDistance branch from 9f80cda to c01689f Compare September 17, 2022 20:35
@romani
Copy link
Member

romani commented Sep 18, 2022

@Vyom-Yadav , please glance at failure
Is it capture#xxx problem ?
#12210

@Vyom-Yadav
Copy link
Member

@romani For nullness checker it is capture#, for methods-resource-fenum it is
Message: "@MustCall method close may not have been invoked on temp-var-20910 or any of its aliases."
temp-var-20910, this will also vary with runs.

@romani
Copy link
Member

romani commented Sep 18, 2022

@nrmancuso , lets merge this without Checker green state, in worse case we will add one suppression to our files.
While we are learning this Checker tool, we ok to grow tech debt

Copy link
Member

@nrmancuso nrmancuso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checker is already unstable, I am fine to merge on red.

@nrmancuso nrmancuso merged commit 1674de8 into checkstyle:master Sep 18, 2022
@stoyanK7 stoyanK7 deleted the issue/11973-fix-NPE-empty-switch-VariableDeclarationUsageDistance branch September 19, 2022 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VariableDeclarationUsageDistance throws NPE on empty switch statements
5 participants