From d1740394aa2e4313900c0d6a2be9f6da7b03c3c7 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Sat, 20 Mar 2021 20:53:51 -0400 Subject: [PATCH] Detect mixed echo argument when unused variable detection is turned on --- .../Analyzer/Statements/EchoAnalyzer.php | 22 +++++++++---------- tests/UnusedVariableTest.php | 8 ++++++- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/EchoAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/EchoAnalyzer.php index 7a9e3fce57e..b63ee750f8e 100644 --- a/src/Psalm/Internal/Analyzer/Statements/EchoAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/EchoAnalyzer.php @@ -37,19 +37,17 @@ public static function analyze( $expr_type = $statements_analyzer->node_data->getType($expr); - if ($statements_analyzer->data_flow_graph - && $expr_type - ) { - $expr_type = CastAnalyzer::castStringAttempt( - $statements_analyzer, - $context, - $expr_type, - $expr, - false - ); - } - if ($statements_analyzer->data_flow_graph instanceof TaintFlowGraph) { + if ($expr_type) { + $expr_type = CastAnalyzer::castStringAttempt( + $statements_analyzer, + $context, + $expr_type, + $expr, + false + ); + } + $call_location = new CodeLocation($statements_analyzer->getSource(), $stmt); $echo_param_sink = TaintSink::getForMethodArgument( diff --git a/tests/UnusedVariableTest.php b/tests/UnusedVariableTest.php index 794722cda11..9681871b3d8 100644 --- a/tests/UnusedVariableTest.php +++ b/tests/UnusedVariableTest.php @@ -932,6 +932,7 @@ function bar(string $type) : ArrayObject { $i = 1; }; $a(); + /** @psalm-suppress MixedArgument */ echo $i;', ], 'regularVariableClosureUseInAddition' => [ @@ -1853,7 +1854,10 @@ function foo(string $a): void { $hue = "goodbye"; } - /** @psalm-suppress PossiblyUndefinedVariable */ + /** + * @psalm-suppress PossiblyUndefinedVariable + * @psalm-suppress MixedArgument + */ echo $hue; }' ], @@ -1928,6 +1932,7 @@ function foo(int $i): void { } if (isset($j)) { + /** @psalm-suppress MixedArgument */ echo $j; } }' @@ -2012,6 +2017,7 @@ function verifyLoad(bool $b) : bool { ' $keys */ function foo(iterable $keys, int $colno) : void {