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-unused-collector.h"
20 : #include "rust-hir-expr.h"
21 : #include "rust-hir-full-decls.h"
22 : #include "rust-hir-item.h"
23 : #include "rust-hir-path.h"
24 : #include "rust-hir-pattern.h"
25 : #include "rust-finalized-name-resolution-context.h"
26 : #include "rust-rib.h"
27 :
28 : namespace Rust {
29 : namespace Analysis {
30 13 : UnusedCollector::UnusedCollector (UnusedContext &context)
31 13 : : nr_context (Resolver2_0::FinalizedNameResolutionContext::get ()),
32 13 : mappings (Analysis::Mappings::get ()), unused_context (context)
33 13 : {}
34 : void
35 13 : UnusedCollector::go (HIR::Crate &crate)
36 : {
37 36 : for (auto &item : crate.get_items ())
38 23 : item->accept_vis (*this);
39 13 : }
40 :
41 : void
42 15 : UnusedCollector::visit (HIR::PathInExpression &expr)
43 : {
44 15 : mark_path_used (expr);
45 15 : walk (expr);
46 15 : }
47 :
48 : void
49 0 : UnusedCollector::visit (HIR::QualifiedPathInExpression &expr)
50 : {
51 0 : mark_path_used (expr);
52 0 : walk (expr);
53 0 : }
54 :
55 : void
56 0 : UnusedCollector::visit (HIR::StructExprFieldIdentifier &ident)
57 : {
58 0 : mark_path_used (ident);
59 0 : walk (ident);
60 0 : }
61 :
62 : void
63 9 : UnusedCollector::visit (HIR::AssignmentExpr &expr)
64 : {
65 9 : if (auto def_id
66 9 : = get_def_id (expr.get_lhs (), Resolver2_0::Namespace::Values))
67 : {
68 9 : HirId id = expr.get_lhs ().get_mappings ().get_hirid ();
69 9 : unused_context.remove_mut (*def_id);
70 9 : unused_context.add_assign (*def_id, id);
71 : }
72 9 : visit_outer_attrs (expr);
73 9 : expr.get_rhs ().accept_vis (*this);
74 9 : }
75 :
76 : void
77 13 : UnusedCollector::visit (HIR::IdentifierPattern &pattern)
78 : {
79 13 : if (pattern.is_mut ())
80 3 : unused_context.add_mut (pattern.get_mappings ().get_hirid ());
81 :
82 13 : walk (pattern);
83 13 : }
84 :
85 : void
86 2 : UnusedCollector::visit (HIR::StructPatternFieldIdent &pattern)
87 : {
88 2 : if (pattern.is_mut ())
89 2 : unused_context.add_mut (pattern.get_mappings ().get_hirid ());
90 :
91 2 : walk (pattern);
92 2 : }
93 :
94 : void
95 2 : UnusedCollector::visit (HIR::BreakExpr &expr)
96 : {
97 2 : if (!expr.has_label ())
98 : return;
99 1 : mark_label_used (expr.get_label ());
100 1 : walk (expr);
101 : }
102 :
103 : void
104 2 : UnusedCollector::visit (HIR::ContinueExpr &expr)
105 : {
106 2 : if (!expr.has_label ())
107 : return;
108 1 : mark_label_used (expr.get_label ());
109 1 : walk (expr);
110 : }
111 :
112 : } // namespace Analysis
113 : } // namespace Rust
|