diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php index fef2f892c..98e911a0e 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php @@ -321,11 +321,9 @@ class VariableFetchAnalyzer (bool) $statements_analyzer->getBranchPoint($var_name), ); } else { - if ($codebase->alter_code) { - if (!isset($project_analyzer->getIssuesToFix()['PossiblyUndefinedVariable'])) { - return true; - } - + if ($codebase->alter_code + && isset($project_analyzer->getIssuesToFix()['PossiblyUndefinedVariable']) + ) { $branch_point = $statements_analyzer->getBranchPoint($var_name); if ($branch_point) { diff --git a/tests/FileManipulation/UnusedVariableManipulationTest.php b/tests/FileManipulation/UnusedVariableManipulationTest.php index 91636f4cb..d41215421 100644 --- a/tests/FileManipulation/UnusedVariableManipulationTest.php +++ b/tests/FileManipulation/UnusedVariableManipulationTest.php @@ -27,6 +27,27 @@ class UnusedVariableManipulationTest extends FileManipulationTestCase 'issues_to_fix' => ['UnusedVariable'], 'safe_types' => true, ], + 'dontRemovePossiblyUndefined' => [ + 'input' => ' ' '7.1', + 'issues_to_fix' => ['UnusedVariable'], + 'safe_types' => true, + ], 'removeUnusedVariableFromTry' => [ 'input' => '