Skip to content

Cannot detect check for null when assigning to a variable #3631

@Nyholm

Description

@Nyholm

I found this bug. Not sure if it is "too complex" or not. I just thought to report it.

function takesAString(string $name): void {
    echo "hello: ".$name;
}

function randomReturn(): ?string {
    return rand(1,2) === 1 ? 'foo' : null;
}

$name = randomReturn();

// Assignment in if does not work
if ($hasStringValue = $name !== null) {
    takesAString($name);
}

https://psalm.dev/r/c9ebe1f843

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughard problemsProblems without an obvious easy solution

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions