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

Report variables whose value is overwritten across multiple branches differently #4600

Open
arichard4 opened this issue Oct 24, 2021 · 0 comments

Comments

@arichard4
Copy link
Contributor

This is for upstreaming code that I've had running on a branch of Phan for a few years; the question is whether you want this (and also the code quality, since I'm utterly unfamiliar with Phan's internals). I'll submit a PR shortly for more concrete discussion.

Phan draws a distinction between variables which are never defined, PhanUnusedVariable, and variables which are undefined in some reachable branches so far as Phan can tell, PhanPossiblyUndeclaredVariable. Phan also reports variable assignments that are never used. However, this interacts imperfectly with PhanUnusedVariable when considering multiple branches:

<?php

function test_unused() {
	$var = 'default'; //PhanUnusedVariable Unused definition of variable $var
	if (rand(0,1)) {
		$var = '1';
	} else {
		$var = '0';
	}
	return $var;
}

function test_invalid() {
	if (rand(0,1)) {
		$var = '1';
	}
	return $var; //PhanPossiblyUndeclaredVariable Variable $var is possibly undeclared
}

print(test_unused());
print(test_invalid());

Some projects (e.g. mine) need to silence PhanPossiblyUndeclaredVariable due to a high rate of false positives and existing bad code; others are simply fine with the unused default value pattern. It would be nice if there was a way to silence the issue in test_unused without silencing all PhanUnusedVariables.

More specifically, if Phan reported a different issue type for when an assignment is unused because it is overwritten on multiple branches versus when it is either never accessed or overwritten without branches, projects that silence PhanPossiblyUndeclaredVariable could also silence the new issue type without suppressing other PhanUnusedVariable warnings.

arichard4 pushed a commit to arichard4/phan that referenced this issue Oct 24, 2021
…m those unused or overwritten in one branch

Closes phan#4600
arichard4 pushed a commit to arichard4/phan that referenced this issue Oct 24, 2021
…m those unused or overwritten in one branch

Closes phan#4600
arichard4 pushed a commit to arichard4/phan that referenced this issue Oct 24, 2021
…m those unused or overwritten in one branch

Closes phan#4600
arichard4 pushed a commit to arichard4/phan that referenced this issue Oct 24, 2021
…m those unused or overwritten in one branch

Closes phan#4600
arichard4 pushed a commit to arichard4/phan that referenced this issue Oct 24, 2021
…m those unused or overwritten in one branch

Closes phan#4600
arichard4 pushed a commit to arichard4/phan that referenced this issue Oct 25, 2021
…m those unused or overwritten in one branch

Closes phan#4600
arichard4 pushed a commit to arichard4/phan that referenced this issue Oct 26, 2021
…m those unused or overwritten in one branch

Closes phan#4600
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

No branches or pull requests

1 participant