Skip to content

Commit

Permalink
Suppressing NoValue should not treat subsequent code as unevaluated
Browse files Browse the repository at this point in the history
Fix #10302
  • Loading branch information
kkmuffme committed Oct 21, 2023
1 parent 915d801 commit 2da694a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -528,18 +528,17 @@ public static function analyze(
}

if ($context->vars_in_scope[$var_id]->isNever()) {
if (IssueBuffer::accepts(
if (!IssueBuffer::accepts(
new NoValue(
'All possible types for this assignment were invalidated - This may be dead code',
new CodeLocation($statements_analyzer->getSource(), $assign_var),
),
$statements_analyzer->getSuppressedIssues(),
)) {
return false;
// if the error is suppressed, do not treat it as never anymore
$context->vars_in_scope[$var_id] = Type::getMixed();
}

$context->vars_in_scope[$var_id] = Type::getNever();

$context->inside_assignment = $was_in_assignment;

return $context->vars_in_scope[$var_id];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,13 +806,16 @@ public static function verifyType(
}

if ($input_type->isNever()) {
IssueBuffer::maybeAdd(
if (!IssueBuffer::accepts(
new NoValue(
'All possible types for this argument were invalidated - This may be dead code',
$arg_location,
),
$statements_analyzer->getSuppressedIssues(),
);
)) {
// if the error is suppressed, do not treat it as exited anymore
$context->has_returned = false;
}

return null;
}
Expand Down
36 changes: 34 additions & 2 deletions tests/UnusedCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,13 @@ public static function get(): a {
return new a;
}
}
final class b {
public function test(): a {
return new a;
}
}
function process(b $handler): a {
if (\extension_loaded("fdsfdsfd")) {
return $handler->test();
Expand Down Expand Up @@ -1323,6 +1323,38 @@ public function b(): void {}
new A;
PHP,
],
'callNeverReturnsSuppressed' => [
'code' => '<?php
namespace Foo;
/**
* @psalm-suppress InvalidReturnType
* @return never
*/
function foo() : void {}
function bar(mixed $s) : string {
return is_string($s) ? "hello" : "world";
}
/** @psalm-suppress NoValue */
$a = foo();
print_r($a);',
],
'useNeverReturnsAsArgSuppressed' => [
'code' => '<?php
namespace Foo;
/**
* @psalm-suppress InvalidReturnType
* @return never
*/
function foo() : void {}
function bar($s) : void {}
/** @psalm-suppress NoValue */
bar(foo());
echo "hello";',
],
];
}

Expand Down

0 comments on commit 2da694a

Please sign in to comment.