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

[NETBEANS-7949] Fixing handling of 'case null'. #7980

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lahodaj
Copy link
Contributor

@lahodaj lahodaj commented Nov 22, 2024

This tries to handle various cases related to case null:

  • if the selector is nullable, it still won't throw an exception
  • if the selector is a variable, the variable will definitely be null inside case null and definitely not-null in other cases
  • proper handling of { yield ...; }

@lahodaj lahodaj added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) LSP [ci] enable Language Server Protocol tests VSCode Extension [ci] enable VSCode Extension tests labels Nov 22, 2024
@lahodaj lahodaj added this to the NB25 milestone Nov 22, 2024
@lahodaj lahodaj requested a review from mbien November 22, 2024 17:53
@mbien mbien added the hints label Nov 22, 2024
@mbien mbien added the ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) label Nov 22, 2024
Copy link
Member

@mbien mbien left a comment

Choose a reason for hiding this comment

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

makes sense to me

Comment on lines +256 to +262
boolean hasNullCase = cases.stream()
.flatMap(ct -> ct.getLabels().stream())
.filter(clt -> clt.getKind() == Kind.CONSTANT_CASE_LABEL)
.map(ctl -> ((ConstantCaseLabelTree) ctl).getConstantExpression())
.filter(expr -> expr.getKind() == Kind.NULL_LITERAL)
.findAny()
.isPresent();
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: one of my jackpot rules matched here, shorter version which combines the last three stages:

        boolean hasNullCase = cases.stream()
                                   .flatMap(ct -> ct.getLabels().stream())
                                   .filter(clt -> clt.getKind() == Kind.CONSTANT_CASE_LABEL)
                                   .map(ctl -> ((ConstantCaseLabelTree) ctl).getConstantExpression())
                                   .anyMatch(expr -> expr.getKind() == Kind.NULL_LITERAL);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) hints Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) LSP [ci] enable Language Server Protocol tests VSCode Extension [ci] enable VSCode Extension tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NPECheck (the “Null Pointer Dereference” hint) should support switches with case null
2 participants