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

Replace libdparse with DMD in UnusedParameterCheck #116

Open
wants to merge 2 commits into
base: replace_libdparse
Choose a base branch
from

Conversation

Vladiwostok
Copy link
Collaborator

@Vladiwostok Vladiwostok commented Apr 16, 2024

This check warns for unused function parameters, with the following exceptions:

  • functions with no bodies are ignored;
  • function overrides are ignored;
  • parameters having only '_' chars in their name are ignored;
  • parameters passed as pointers are ignored;
  • parameters having 'ref' or 'out' are ignored;

This visitor inherits a common chain of visitors also used for UnusedVariableCheck (UnusedIdentifierCheck and UnusedStorageCheck. I've decided to move this check to DMD without moving it's parent classes, since most of the base class functionality is needed for the unused var check.

My approach for this visitor is:

  • create a "scope" (array of AA containing some var metadata) when visiting a function;
  • check the unused params in the scope only if the function should be checked;
  • when visiting a Parameter, add it's metadata to the current scope;
  • when visiting an identifier expression, mark the parameter as used, if it exists;
  • if visiting a string expression inside a mixin statement, try to look for any of the current scope params in the string literal;
  • visit '__traits' expression and mark as used the expression's arguments having the identifier type;
  • when checking if a param is used, if a param is not found in the current scope, the parent scopes are checked in order to cover local function cases.

I've also added extra tests for override functions and ref / out / pointer params.
Also, the check is not 100% correct. Given the following example:

void testNested(int a)
{
	int nested(int b)
	{
		int a = 5;
		return a + b;
	}

	nested(3);
}

The libdparse visitor fails to warn for unused param 'a' in 'testNested'. I've kept the same behavior in DMD version.

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 this pull request may close these issues.

Replace libdparse with DMD in UnusedParameterCheck
1 participant