1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

don't consider calls to methods with assertions as Unused

This commit is contained in:
orklah 2021-11-06 19:00:36 +01:00
parent f327c986d0
commit 9c9669ea44
2 changed files with 31 additions and 1 deletions

View File

@ -106,7 +106,13 @@ class MethodCallPurityAnalyzer
&& !$context->inside_general_use
&& !$context->inside_throw
) {
if (!$context->inside_assignment && !$context->inside_call && !$context->inside_return) {
if (!$context->inside_assignment
&& !$context->inside_call
&& !$context->inside_return
&& !$method_storage->assertions
&& !$method_storage->if_true_assertions
&& !$method_storage->if_false_assertions
) {
if (IssueBuffer::accepts(
new \Psalm\Issue\UnusedMethodCall(
'The call to ' . $cased_method_id . ' is not used',

View File

@ -1069,6 +1069,30 @@ class UnusedCodeTest extends TestCase
echo "hello";
}',
],
'NotUnusedWhenAssert' => [
'<?php
class A {
public function getVal(?string $val): string {
$this->assert($val);
return $val;
}
/**
* @psalm-assert string $val
* @psalm-mutation-free
*/
private function assert(?string $val): void {
if (null === $val) {
throw new Exception();
}
}
}
$a = new A();
$a->getVal(null);',
],
];
}