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-finalized-name-resolution-context.h"
25 : #include "rust-rib.h"
26 : #include "rust-unused-context.h"
27 :
28 : namespace Rust {
29 : namespace Analysis {
30 : class UnusedCollector : public HIR::DefaultHIRVisitor
31 : {
32 : public:
33 : UnusedCollector (UnusedContext &context);
34 : void go (HIR::Crate &crate);
35 :
36 : private:
37 : const Resolver2_0::FinalizedNameResolutionContext &nr_context;
38 : Analysis::Mappings &mappings;
39 : UnusedContext &unused_context;
40 :
41 : using HIR::DefaultHIRVisitor::visit;
42 :
43 : // Unused var
44 : virtual void visit (HIR::PathInExpression &expr) override;
45 : virtual void visit (HIR::StructExprFieldIdentifier &ident) override;
46 : virtual void visit (HIR::QualifiedPathInExpression &expr) override;
47 :
48 : // Unused assignments
49 : virtual void visit (HIR::AssignmentExpr &expr) override;
50 :
51 : // Unused mut
52 : virtual void visit (HIR::IdentifierPattern &pattern) override;
53 : virtual void visit (HIR::StructPatternFieldIdent &pattern) override;
54 :
55 : // Unused label
56 : virtual void visit (HIR::BreakExpr &expr) override;
57 : virtual void visit (HIR::ContinueExpr &expr) override;
58 :
59 : template <typename T>
60 11 : tl::optional<HirId> get_def_id (T &path_expr, Resolver2_0::Namespace ns)
61 : {
62 11 : NodeId ast_node_id = path_expr.get_mappings ().get_nodeid ();
63 :
64 11 : if (auto id = nr_context.lookup (ast_node_id, ns))
65 : {
66 11 : if (auto def_id = mappings.lookup_node_to_hir (*id))
67 : {
68 11 : return *def_id;
69 : }
70 : }
71 0 : return tl::nullopt;
72 : }
73 :
74 : template <typename T>
75 15 : tl::optional<HirId> get_def_id (T &path_expr, Resolver2_0::Namespace ns1,
76 : Resolver2_0::Namespace ns2)
77 : {
78 15 : NodeId ast_node_id = path_expr.get_mappings ().get_nodeid ();
79 :
80 15 : if (auto nslookup = nr_context.lookup (ast_node_id, ns1, ns2))
81 : {
82 15 : NodeId id = nslookup->id;
83 :
84 15 : if (auto def_id = mappings.lookup_node_to_hir (id))
85 : {
86 15 : return *def_id;
87 : }
88 : }
89 :
90 0 : return tl::nullopt;
91 : }
92 :
93 15 : template <typename T> void mark_path_used (T &path_expr)
94 : {
95 15 : if (auto def_id = get_def_id (path_expr, Resolver2_0::Namespace::Values,
96 : Resolver2_0::Namespace::Types))
97 : {
98 15 : unused_context.add_variable (*def_id);
99 15 : unused_context.remove_assign (*def_id);
100 : }
101 15 : }
102 :
103 2 : template <typename T> void mark_label_used (T &path_expr)
104 : {
105 2 : if (auto def_id = get_def_id (path_expr, Resolver2_0::Namespace::Labels))
106 : {
107 2 : unused_context.add_label (*def_id);
108 : }
109 2 : }
110 : };
111 : } // namespace Analysis
112 : } // namespace Rust
|