Skip to content

Implement unused variable checker on HIR #4055

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sakupan102
Copy link
Contributor

@sakupan102 sakupan102 commented Aug 10, 2025

@sakupan102 sakupan102 changed the title Implement unused variable checker on HIR. Implement unused variable checker on HIR Aug 10, 2025
gcc/rust/ChangeLog:

	* Make-lang.in: New file.
	* checks/lints/unused-var/rust-unused-var-checker.cc (UnusedVarChecker): Implement unused
	variable checker.
	* checks/lints/unused-var/rust-unused-var-checker.h (UnusedVarChecker): Implement unused
	variable checker.
	* checks/lints/unused-var/rust-unused-var-collector.cc (UnusedVarCollector): Implement
	unused variable collector.
	* checks/lints/unused-var/rust-unused-var-collector.h (UnusedVarCollector): Implement
	unused variable collector.
	* checks/lints/unused-var/rust-unused-var-context.cc (UnusedVarContext): Implement
	unused variable context.
	* checks/lints/unused-var/rust-unused-var-context.h (UnusedVarContext): Implement unused
	variable context.

Signed-off-by: Ryutaro Okada <[email protected]>
Copy link
Member

@CohenArthur CohenArthur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a really good base and the code looks good to me! I think it is important to add testcases for features like these, so it would be good if you could add a flag to enable the lints you've written and then add testcases to the testsuite

Comment on lines +24 to +31
UnusedVarContext *
UnusedVarContext::get ()
{
static UnusedVarContext *instance;
if (instance == nullptr)
instance = new UnusedVarContext ();
return instance;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine but we should also check if we can do without a global variable

UnusedVarCollector::visit (HIR::LetStmt &stmt)
{
HIR::Pattern &pattern = stmt.get_pattern ();
collect_variable (pattern);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we visit instead here? mabye I'm missing something but I think all patterns should create variables to check for, so we could just do visit(pattern) and then create visitors for all our kinds of patterns instead of manually dispatching like in collect_variable

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.

2 participants