Fix nullsafe.neverNull false positive when the ?-> operand is nullable#6065
Closed
clarg18 wants to merge 1 commit into
Closed
Fix nullsafe.neverNull false positive when the ?-> operand is nullable#6065clarg18 wants to merge 1 commit into
nullsafe.neverNull false positive when the ?-> operand is nullable#6065clarg18 wants to merge 1 commit into
Conversation
…lable $a?->b on the left of ??, or inside isset()/empty(), was reported as redundant whenever $a?->b as a whole was nullable, without checking $a itself. When $a is nullable the ?-> is what yields the null those constructs handle, so it's required, and switching to -> as advised would fatal at runtime. Report only when the operand is never null, and recurse into a nullsafe operand so an inner never-null link ($nonNull?->nullable?->x) is still caught, matching NullsafePropertyFetchRule outside these contexts. Adds the same treatPhpDocTypesAsCertain tip, and fixes the bug-7109 expectations that had baked in the false positive. Fixes phpstan/phpstan#14311
Member
|
Don't fix what's not broken :) phpstan/phpstan#14311 (comment) |
Author
|
You're right, thanks. I hadn't clocked that |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes phpstan/phpstan#14311
$a?->bon the left of??, or insideisset()/empty(), was reported as an unnecessary nullsafe access whenever the whole$a?->bexpression came out nullable, without looking at whether$aitself could be null. When$ais nullable the?->is exactly what produces the null the surrounding construct then handles, so it is required. Following the advice and switching to->fatals at runtime when$ais null.Small repro:
The check in
IssetChecknow only reports when the operand is never null. When the operand is itself a nullsafe fetch with a never-null operand of its own ($nonNull?->nullable?->x), it recurses so that inner redundant link is still reported, which keeps parity withNullsafePropertyFetchRuleoutside these contexts. I also added thetreatPhpDocTypesAsCertaintip that the sibling rule emits.bug-7109had assertions that encoded the old behaviour (its$this->get()?->aaa ?? 6, whereget(): ?HelloWorld, was expected to be reported), so I corrected those and addedbug-14311for the fix.