Line data Source code
1 : // Copyright (C) 2025-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #include "rust-hir-expr.h"
20 : #include "rust-hir-path.h"
21 : #include "rust-hir-pattern.h"
22 : #include "rust-hir-visitor.h"
23 : #include "rust-mapping-common.h"
24 : #include "rust-name-resolution-context.h"
25 : #include "rust-unused-context.h"
26 :
27 : namespace Rust {
28 : namespace Analysis {
29 : class UnusedCollector : public HIR::DefaultHIRVisitor
30 : {
31 : public:
32 : UnusedCollector (UnusedContext &context);
33 : void go (HIR::Crate &crate);
34 :
35 : private:
36 : const Resolver2_0::NameResolutionContext &nr_context;
37 : Analysis::Mappings &mappings;
38 : UnusedContext &unused_context;
39 :
40 : using HIR::DefaultHIRVisitor::visit;
41 :
42 : // Unused var
43 : virtual void visit (HIR::PathInExpression &expr) override;
44 : virtual void visit (HIR::StructExprFieldIdentifier &ident) override;
45 : virtual void visit (HIR::QualifiedPathInExpression &expr) override;
46 :
47 : // Unused assignments
48 : virtual void visit (HIR::AssignmentExpr &expr) override;
49 :
50 : // Unused mut
51 : virtual void visit (HIR::IdentifierPattern &pattern) override;
52 : virtual void visit (HIR::StructPatternFieldIdent &pattern) override;
53 :
54 : // Unused label
55 : virtual void visit (HIR::BreakExpr &expr) override;
56 : virtual void visit (HIR::ContinueExpr &expr) override;
57 :
58 21 : template <typename T> HirId get_def_id (T &path_expr)
59 : {
60 21 : NodeId ast_node_id = path_expr.get_mappings ().get_nodeid ();
61 21 : NodeId id = nr_context.lookup (ast_node_id).value ();
62 21 : HirId def_id = mappings.lookup_node_to_hir (id).value ();
63 21 : return def_id;
64 : }
65 :
66 10 : template <typename T> void mark_path_used (T &path_expr)
67 : {
68 10 : auto def_id = get_def_id (path_expr);
69 10 : unused_context.add_variable (def_id);
70 10 : unused_context.remove_assign (def_id);
71 10 : }
72 :
73 2 : template <typename T> void mark_label_used (T &path_expr)
74 : {
75 2 : auto def_id = get_def_id (path_expr);
76 2 : unused_context.add_label (def_id);
77 2 : }
78 : };
79 : } // namespace Analysis
80 : } // namespace Rust
|