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

Unexpected RedundantConditionGivenDocblockType #3790

Closed
andyg0808 opened this issue Jul 10, 2020 · 3 comments
Closed

Unexpected RedundantConditionGivenDocblockType #3790

andyg0808 opened this issue Jul 10, 2020 · 3 comments
Labels

Comments

@andyg0808
Copy link

It seems like the conditional here is a reasonable thing to do:
https://psalm.dev/r/b4f8169f97

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/b4f8169f97
<?php

/**
 * @template T of mixed|false|null
 * @param T $i
 * @return (T is false ? no-return : T is null ? no-return : T)
 */
function orThrow($i) {
    if ($i === false || $i === null) {
        throw new RuntimeException("Example");
    }
    return $i;
}

/** @psalm-trace $var */
$var = orThrow(false);
Psalm output (using commit d1ca68e):

ERROR: RedundantConditionGivenDocblockType - 9:9 - Found a redundant condition when evaluating docblock-defined type $i and trying to reconcile type 'T:fn-orthrow as false|mixed|null' to !false

INFO: LessSpecificReturnType - 6:12 - The inferred return type 'T' for orThrow is more specific than the declared return type 'T|never-return'

ERROR: NoValue - 16:1 - This function or method call never returns output

@weirdan
Copy link
Collaborator

weirdan commented Jul 10, 2020

Also, types inferred in ifs here look incorrect: https://psalm.dev/r/eca2491cf6

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/eca2491cf6
<?php

/**
 * @template T of mixed|false|null
 * @param T $i
 * @return (T is false ? no-return : (T is null ? no-return : T))
 */
function orThrow($i) {
    /** @psalm-trace $i */
    $i;
    
    if ($i === false) {
        /** @psalm-trace $i */
        $i;
        throw new RuntimeException("Example");
    }
    if ($i === null) {
        /** @psalm-trace $i */
        $i;
        throw new RuntimeException("Example");
    }
    /** @psalm-trace $i */
    $i;
    return $i;
}

/** @psalm-trace $var */
$var = orThrow(false);
Psalm output (using commit d1ca68e):

INFO: Trace - 10:5 - $i: T:fn-orthrow as false|mixed|null

INFO: Trace - 14:9 - $i: T:fn-orthrow as false|mixed|null

INFO: Trace - 19:9 - $i: T:fn-orthrow as mixed|null

INFO: Trace - 23:5 - $i: T:fn-orthrow as mixed

INFO: LessSpecificReturnType - 6:12 - The inferred return type 'T' for orThrow is more specific than the declared return type 'T|never-return'

ERROR: NoValue - 28:1 - This function or method call never returns output

@muglug muglug added the bug label Jul 10, 2020
@muglug muglug closed this as completed in 8349564 Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants