Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline returned variables aren't considered used #4570

Closed
arichard4 opened this issue Oct 8, 2021 · 2 comments · Fixed by #4571
Closed

Inline returned variables aren't considered used #4570

arichard4 opened this issue Oct 8, 2021 · 2 comments · Fixed by #4571

Comments

@arichard4
Copy link
Contributor

arichard4 commented Oct 8, 2021

In some cases, a return statement isn't considered as using a variable, property, or closure.

<?php

class A {
	private static $cache;
	private static function ExpensiveFunction() {
		return 1;
	}

	public static function CachedCall() {
		return self::$cache ??= static::ExpensiveFunction();
	}

	public static function ReturnsVar() {
		return $x = 1;
	}

	public static function ReturnsFunc() {
		return function() { return 1; };
	}
}

A::CachedCall();
A::ReturnsVar();
A::ReturnsFunc();
../phan/build/phan.phar --dead-code-detection return.php
 dead code ████████████████████████████████████████████████████ 100% 31MB/33MB
cache.php:4 PhanWriteOnlyPrivateProperty Possibly zero read references to private property \A::$cache
cache.php:14 PhanUnusedVariable Unused definition of variable $x
cache.php:18 PhanUnreferencedClosure Possibly zero references to Closure()

This is true both for 5.2.1 and for tip of v5.

TysonAndre added a commit to TysonAndre/phan that referenced this issue Oct 8, 2021
Make it more accurate for assignment operations (e.g. `+=`, `??=`) and
increment/decrement operations.

Closes phan#4570
@TysonAndre
Copy link
Member

cache.php:14 PhanUnusedVariable Unused definition of variable $x is by design.

PhanUnreferencedClosure is a different type of false positive - that issue type predates unused variable detection, and just checks if the closure is called and has a high false positive rate in general.

@arichard4
Copy link
Contributor Author

Thanks so much, sorry for the incorrect report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants