mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Complain about redundant conditions on custom asserts
This commit is contained in:
parent
3fb0a2f336
commit
4904ffe8ea
@ -477,8 +477,9 @@ class FunctionCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expressio
|
||||
}
|
||||
|
||||
if ($function_storage) {
|
||||
if ($function_storage->assertions) {
|
||||
if ($function_storage->assertions && $stmt->name instanceof PhpParser\Node\Name) {
|
||||
self::applyAssertionsToContext(
|
||||
$stmt->name,
|
||||
$function_storage->assertions,
|
||||
$stmt->args,
|
||||
$function_storage->template_typeof_params ?: [],
|
||||
|
@ -706,6 +706,7 @@ class MethodCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\
|
||||
if ($method_storage) {
|
||||
if ($method_storage->assertions) {
|
||||
self::applyAssertionsToContext(
|
||||
$stmt->name,
|
||||
$method_storage->assertions,
|
||||
$args,
|
||||
$method_storage->template_typeof_params ?: [],
|
||||
|
@ -508,6 +508,7 @@ class StaticCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\
|
||||
if ($method_storage) {
|
||||
if ($method_storage->assertions) {
|
||||
self::applyAssertionsToContext(
|
||||
$stmt->name,
|
||||
$method_storage->assertions,
|
||||
$stmt->args,
|
||||
$method_storage->template_typeof_params ?: [],
|
||||
|
@ -2120,6 +2120,7 @@ class CallAnalyzer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PhpParser\Node\Identifier|PhpParser\Node\Name $expr
|
||||
* @param \Psalm\Storage\Assertion[] $assertions
|
||||
* @param array<int, PhpParser\Node\Arg> $args
|
||||
* @param Context $context
|
||||
@ -2129,6 +2130,7 @@ class CallAnalyzer
|
||||
* @return void
|
||||
*/
|
||||
protected static function applyAssertionsToContext(
|
||||
$expr,
|
||||
array $assertions,
|
||||
array $args,
|
||||
array $template_typeof_params,
|
||||
@ -2175,15 +2177,21 @@ class CallAnalyzer
|
||||
|
||||
$changed_vars = [];
|
||||
|
||||
$asserted_keys = [];
|
||||
|
||||
foreach ($type_assertions as $var_id => $_) {
|
||||
$asserted_keys[$var_id] = true;
|
||||
}
|
||||
|
||||
// while in an and, we allow scope to boil over to support
|
||||
// statements of the form if ($x && $x->foo())
|
||||
$op_vars_in_scope = \Psalm\Type\Reconciler::reconcileKeyedTypes(
|
||||
$type_assertions,
|
||||
$context->vars_in_scope,
|
||||
$changed_vars,
|
||||
[],
|
||||
$asserted_keys,
|
||||
$statements_analyzer,
|
||||
null
|
||||
new CodeLocation($statements_analyzer->getSource(), $expr)
|
||||
);
|
||||
|
||||
foreach ($changed_vars as $changed_var) {
|
||||
|
@ -373,6 +373,29 @@ class AssertTest extends TestCase
|
||||
$bar->sayHello();',
|
||||
'error_message' => 'UndefinedClass',
|
||||
],
|
||||
'detectRedundantCondition' => [
|
||||
'<?php
|
||||
class A {}
|
||||
|
||||
/**
|
||||
* @param class-string $expected
|
||||
* @param mixed $actual
|
||||
* @param string $message
|
||||
*
|
||||
* @template T
|
||||
* @template-typeof T $expected
|
||||
* @psalm-assert T $actual
|
||||
*/
|
||||
function assertInstanceOf($expected, $actual) : void {
|
||||
}
|
||||
|
||||
function takesA(A $a) : void {
|
||||
if (assertInstanceOf(A::class, $a)) {
|
||||
return;
|
||||
}
|
||||
}',
|
||||
'error_message' => 'RedundantCondition'
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user