1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Mark return values in as throw argument as used (#5989)

Fixes vimeo/psalm#5975
This commit is contained in:
Bruce Weirdan 2021-06-25 16:11:27 +03:00 committed by GitHub
parent ee1c36c635
commit 6d4262edbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 1 deletions

View File

@ -940,6 +940,7 @@ class ArgumentAnalyzer
|| $context->inside_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
);
}
}

View File

@ -207,6 +207,7 @@ class AtomicMethodCallAnalyzer extends CallAnalyzer
|| $context->inside_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
);
$fake_method_exists = false;
@ -331,6 +332,7 @@ class AtomicMethodCallAnalyzer extends CallAnalyzer
|| $context->inside_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
)
) {
$new_call_context = MissingMethodCallHandler::handleMagicMethod(
@ -737,6 +739,7 @@ class AtomicMethodCallAnalyzer extends CallAnalyzer
|| $context->inside_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
)) {
$lhs_type_part = clone $lhs_type_part_new;
$class_storage = $mixin_class_storage;
@ -809,6 +812,7 @@ class AtomicMethodCallAnalyzer extends CallAnalyzer
|| $context->inside_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
)) {
$mixin_declaring_class_storage = $codebase->classlike_storage_provider->get(
$class_storage->mixin_declaring_fqcln

View File

@ -306,6 +306,7 @@ class AtomicStaticCallAnalyzer
|| $context->inside_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
);
$fake_method_exists = false;
@ -352,6 +353,7 @@ class AtomicStaticCallAnalyzer
|| $context->inside_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
)) {
$mixin_candidates = [];
foreach ($class_storage->templatedMixins as $mixin_candidate) {
@ -476,6 +478,7 @@ class AtomicStaticCallAnalyzer
|| $context->inside_use
|| $context->inside_assignment
|| $context->inside_conditional
|| $context->inside_throw
)) {
if ($codebase->methods->return_type_provider->has($fq_class_name)) {
$return_type_candidate = $codebase->methods->return_type_provider->getReturnType(

View File

@ -81,6 +81,9 @@ class Methods
/**
* Whether or not a given method exists
*
* If you pass true in $is_used argument the method return is considered used
*
* @param lowercase-string|null $calling_method_id
*/
public function methodExists(

View File

@ -1008,6 +1008,26 @@ class UnusedCodeTest extends TestCase
f(new Worker());
f(new AnotherWorker());',
],
'methodReturnValueUsedInThrow' => [
'<?php
class A {
public function foo() : Exception {
return new Exception;
}
}
throw (new A)->foo();
'
],
'staticMethodReturnValueUsedInThrow' => [
'<?php
class A {
public static function foo() : Exception {
return new Exception;
}
}
throw A::foo();
'
],
];
}