Skip to content

Assert can improperly cause "unreachable code" warnings #3717

Description

@kevinrsoursib

Describe the bug
A line like:
assert(false, "This shouldn't happen');
appears to mark the rest of the function as unreachable. However asserts can be disabled (and should be in production environments) so the code in question is very reachable. The use case in question is a __get() magic method where it's useful to flag cases where an invalid field is referenced but you don't necessarily want to make that a fatal error in production.

To Reproduce
A code snippet (NOT a screenshot of a code snippet) or steps to reproduce the issue

class foo
{
private int $bar = 5;
public function __get($name)
{
if ($name == 'bar')
{
return $this->bar;
}
assert(false, 'invalid field passed');

return null; // this line is improperly displayed as unreachable.

}
}

Expected behavior
Should not flag the line as unreachable.

Platform and version
Windows 11, 1.18.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions