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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ GRS_OBJS = \
rust/rust-const-checker.o \
rust/rust-lint-marklive.o \
rust/rust-lint-unused-var.o \
rust/rust-unused-var-checker.o \
rust/rust-unused-var-collector.o \
rust/rust-unused-var-context.o \
rust/rust-readonly-check.o \
rust/rust-readonly-check2.o \
rust/rust-hir-type-check-path.o \
Expand Down Expand Up @@ -441,6 +444,7 @@ RUST_INCLUDES = -I $(srcdir)/rust \
-I $(srcdir)/rust/typecheck \
-I $(srcdir)/rust/checks/lints \
-I $(srcdir)/rust/checks/errors \
-I $(srcdir)/rust/checks/lints/unused-var \
-I $(srcdir)/rust/checks/errors/privacy \
-I $(srcdir)/rust/checks/errors/borrowck \
-I $(srcdir)/rust/util \
Expand Down Expand Up @@ -510,6 +514,11 @@ rust/%.o: rust/checks/lints/%.cc
$(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
$(POSTCOMPILE)

# build unused variable checking pass files in rust folder
rust/%.o: rust/checks/lints/unused-var/%.cc
$(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
$(POSTCOMPILE)

# build rust/checks/errors files in rust folder
rust/%.o: rust/checks/errors/%.cc
$(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
Expand Down
128 changes: 128 additions & 0 deletions gcc/rust/checks/lints/unused-var/rust-unused-var-checker.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright (C) 2025 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "rust-unused-var-checker.h"
#include "rust-hir-item.h"

#include "options.h"

namespace Rust {
namespace Analysis {
UnusedVarChecker::UnusedVarChecker ()
: nr_context (
Resolver2_0::ImmutableNameResolutionContext::get ().resolver ()),
mappings (Analysis::Mappings::get ()),
unused_var_context (*UnusedVarContext::get ())
{}
void
UnusedVarChecker::go (HIR::Crate &crate)
{
UnusedVarCollector ().go (crate);
for (auto &item : crate.get_items ())
item->accept_vis (*this);
}
void
UnusedVarChecker::visit (HIR::LetStmt &stmt)
{
HIR::Pattern &pattern = stmt.get_pattern ();
check_variable (pattern);
walk (stmt);
}
void
UnusedVarChecker::visit_function_param (HIR::FunctionParam &param)
{
check_variable (param.get_param_name ());
}

void
UnusedVarChecker::visit (HIR::ConstantItem &item)
{
std::string var_name = item.get_identifier ().as_string ();
bool starts_with_under_score = var_name.compare (0, 1, "_") == 0;
auto id = item.get_mappings ().get_hirid ();
if (!unused_var_context.is_variable_used (id) && !starts_with_under_score)
rust_warning_at (item.get_locus (), OPT_Wunused_variable,
"unused name '%s'",
item.get_identifier ().as_string ().c_str ());
}

void
UnusedVarChecker::visit (HIR::StaticItem &item)
{
std::string var_name = item.get_identifier ().as_string ();
bool starts_with_under_score = var_name.compare (0, 1, "_") == 0;
auto id = item.get_mappings ().get_hirid ();
if (!unused_var_context.is_variable_used (id) && !starts_with_under_score)
rust_warning_at (item.get_locus (), OPT_Wunused_variable,
"unused name '%s'",
item.get_identifier ().as_string ().c_str ());
}

void
UnusedVarChecker::visit (HIR::TraitItemFunc &item)
{
// TODO: check trait item functions if they are not derived.
}

void
UnusedVarChecker::check_variable (HIR::Pattern &pattern)
{
switch (pattern.get_pattern_type ())
{
case HIR::Pattern::PatternType::IDENTIFIER:
check_variable_identifier (
static_cast<HIR::IdentifierPattern &> (pattern));
break;
case HIR::Pattern::PatternType::TUPLE:
check_variable_tuple (static_cast<HIR::TuplePattern &> (pattern));
break;
default:
break;
}
}
void
UnusedVarChecker::check_variable_identifier (HIR::IdentifierPattern &pattern)
{
std::string var_name = pattern.get_identifier ().as_string ();
bool starts_with_under_score = var_name.compare (0, 1, "_") == 0;
auto id = pattern.get_mappings ().get_hirid ();
if (!unused_var_context.is_variable_used (id) && var_name != "self"
&& !starts_with_under_score)
rust_warning_at (pattern.get_locus (), OPT_Wunused_variable,
"unused name '%s'",
pattern.get_identifier ().as_string ().c_str ());
}
void
UnusedVarChecker::check_variable_tuple (HIR::TuplePattern &pattern)
{
switch (pattern.get_items ().get_item_type ())
{
case HIR::TuplePatternItems::ItemType::NO_REST:
{
auto items
= static_cast<HIR::TuplePatternItemsNoRest &> (pattern.get_items ());
for (auto &item : items.get_patterns ())
check_variable (*item);
}
break;
default:
break;
}
}
} // namespace Analysis
} // namespace Rust
48 changes: 48 additions & 0 deletions gcc/rust/checks/lints/unused-var/rust-unused-var-checker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (C) 2025 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "rust-hir-item.h"
#include "rust-hir-visitor.h"
#include "rust-immutable-name-resolution-context.h"
#include "rust-unused-var-collector.h"

namespace Rust {
namespace Analysis {
class UnusedVarChecker : public HIR::DefaultHIRVisitor
{
public:
UnusedVarChecker ();
void go (HIR::Crate &crate);

private:
const Resolver2_0::NameResolutionContext &nr_context;
Analysis::Mappings &mappings;
UnusedVarContext &unused_var_context;

using HIR::DefaultHIRVisitor::visit;
virtual void visit (HIR::LetStmt &stmt) override;
virtual void visit_function_param (HIR::FunctionParam &param) override;
virtual void visit (HIR::TraitItemFunc &decl) override;
virtual void visit (HIR::ConstantItem &item) override;
virtual void visit (HIR::StaticItem &item) override;
void check_variable_identifier (HIR::IdentifierPattern &identifier);
void check_variable_tuple (HIR::TuplePattern &pattern);
void check_variable (HIR::Pattern &pattern);
};
} // namespace Analysis
} // namespace Rust
130 changes: 130 additions & 0 deletions gcc/rust/checks/lints/unused-var/rust-unused-var-collector.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright (C) 2025 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "rust-unused-var-collector.h"
#include "rust-hir-full-decls.h"
#include "rust-hir-item.h"
#include "rust-hir-path.h"
#include "rust-immutable-name-resolution-context.h"

namespace Rust {
namespace Analysis {
UnusedVarCollector::UnusedVarCollector ()
: nr_context (
Resolver2_0::ImmutableNameResolutionContext::get ().resolver ()),
mappings (Analysis::Mappings::get ()),
unused_var_context (*UnusedVarContext::get ())
{}
void
UnusedVarCollector::go (HIR::Crate &crate)
{
for (auto &item : crate.get_items ())
item->accept_vis (*this);
}
void
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

walk (stmt);
}

void
UnusedVarCollector::visit (HIR::ConstantItem &item)
{
unused_var_context.add_variable (item.get_mappings ().get_hirid ());
walk (item);
}

void
UnusedVarCollector::visit (HIR::StaticItem &item)
{
unused_var_context.add_variable (item.get_mappings ().get_hirid ());
walk (item);
}

void
UnusedVarCollector::visit_function_param (HIR::FunctionParam &param)
{
collect_variable (param.get_param_name ());
}

void
UnusedVarCollector::visit_closure_param (HIR::ClosureParam &param)
{
collect_variable (param.get_pattern ());
}

void
UnusedVarCollector::collect_variable (HIR::Pattern &pattern)
{
switch (pattern.get_pattern_type ())
{
case HIR::Pattern::PatternType::IDENTIFIER:
{
auto &identifier = static_cast<HIR::IdentifierPattern &> (pattern);
auto id = identifier.get_mappings ().get_hirid ();
unused_var_context.add_variable (id);
}
break;
case HIR::Pattern::PatternType::TUPLE:
{
collect_variable_tuple (static_cast<HIR::TuplePattern &> (pattern));
}
break;
default:
break;
}
}
void
UnusedVarCollector::collect_variable_tuple (HIR::TuplePattern &pattern)
{
switch (pattern.get_items ().get_item_type ())
{
case HIR::TuplePatternItems::ItemType::NO_REST:
{
auto &items
= static_cast<HIR::TuplePatternItemsNoRest &> (pattern.get_items ());
for (auto &sub : items.get_patterns ())
collect_variable (*sub);
}
break;
default:
break;
}
}

void
UnusedVarCollector::visit (HIR::PathInExpression &expr)
{
mark_path_used (expr);
}

void
UnusedVarCollector::visit (HIR::QualifiedPathInExpression &expr)
{
mark_path_used (expr);
}

void
UnusedVarCollector::visit (HIR::StructExprFieldIdentifier &ident)
{
mark_path_used (ident);
}
} // namespace Analysis
} // namespace Rust
Loading
Loading