Skip to content

Commit

Permalink
Fix #2752 - string casts can be implicit method calls, don’t remove a…
Browse files Browse the repository at this point in the history
…utomatically
  • Loading branch information
muglug committed Feb 7, 2020
1 parent 4cd4e17 commit e567f8c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Psalm/Internal/Visitor/CheckTrivialExprVisitor.php
Expand Up @@ -37,6 +37,7 @@ private function checkNonTrivialExpr(PhpParser\Node\Expr $node)
|| $node instanceof PhpParser\Node\Expr\Yield_
|| $node instanceof PhpParser\Node\Expr\YieldFrom
|| $node instanceof PhpParser\Node\Expr\New_
|| $node instanceof PhpParser\Node\Expr\Cast\String_
) {
if (($node instanceof PhpParser\Node\Expr\FuncCall
|| $node instanceof PhpParser\Node\Expr\MethodCall
Expand Down
45 changes: 45 additions & 0 deletions tests/FileManipulation/UnusedVariableManipulationTest.php
Expand Up @@ -587,6 +587,51 @@ function foo() : void {
['UnusedVariable'],
true,
],
'dontRemoveUsedToStringCall' => [
'<?php
class S {
/**
* @throws Exception
*/
public function __toString() {
if(rand(0,1)){

This comment has been minimized.

Copy link
@orklah

orklah Feb 7, 2020

Collaborator

Not sure it's a good idea to have rand() in tests. I used it to show exception was not always thrown but tests should behave consistently between runs...

This comment has been minimized.

Copy link
@muglug

muglug Feb 7, 2020

Author Collaborator

ha check again, it's inside a string

This comment has been minimized.

Copy link
@orklah

orklah Feb 7, 2020

Collaborator

facepalm Forgot the whole "static" deal in "static analysis" :D

throw new exception();
}
return "";
}
}
function foo(S $a) {
try {
$b = (string) $a;
} catch(Exception $e){
// this class is not stringable
}
}',
'<?php
class S {
/**
* @throws Exception
*/
public function __toString() {
if(rand(0,1)){
throw new exception();
}
return "";
}
}
function foo(S $a) {
try {
(string) $a;
} catch(Exception $e){
// this class is not stringable
}
}',
'7.1',
['UnusedVariable'],
true,
],
];
}
}

0 comments on commit e567f8c

Please sign in to comment.