Line data Source code
1 : // Copyright (C) 2021-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 : #ifndef RUST_HIR_SCAN_DEADCODE
20 : #define RUST_HIR_SCAN_DEADCODE
21 :
22 : #include "options.h"
23 : #include "rust-hir-full-decls.h"
24 : #include "rust-hir-map.h"
25 : #include "rust-lint-marklive.h"
26 : #include "rust-name-resolver.h"
27 : #include "rust-diagnostics.h"
28 :
29 : namespace Rust {
30 : namespace Analysis {
31 :
32 : // Scan item symbols and warn the symbol if it is not in the live_symbols set.
33 : // There are three kinds of item we should handle in this pass.
34 : // 1. Function item
35 : // 2. The function item in the impl block without trait
36 : // 3. StructStruct, e.g., `Struct Foo{one: 1, two: 2}`. Furthermore, the unused
37 : // struct fields will be warned too.
38 : // 4. TupleStruct, e.g., `Struct Foo(i32, i32)`
39 4628 : class ScanDeadcode : public MarkLiveBase
40 : {
41 : using Rust::Analysis::MarkLiveBase::visit;
42 :
43 : public:
44 4628 : static void Scan (HIR::Crate &crate)
45 : {
46 4628 : std::set<HirId> live_symbols = Analysis::MarkLive::Analysis (crate);
47 4628 : ScanDeadcode sdc (live_symbols);
48 24578 : for (auto &it : crate.get_items ())
49 19950 : it.get ()->accept_vis (sdc);
50 4628 : };
51 :
52 15256 : void visit (HIR::Function &function) override
53 : {
54 15256 : HirId hirId = function.get_mappings ().get_hirid ();
55 15256 : auto starts_with_underscore
56 15256 : = function.get_function_name ().as_string ().rfind ('_', 0) == 0;
57 8145 : if (should_warn (hirId) && !function.get_visibility ().is_public ()
58 20621 : && !starts_with_underscore)
59 : {
60 5343 : if (mappings.is_impl_item (hirId))
61 : {
62 5160 : HIR::ImplBlock *implBlock = mappings.lookup_associated_impl (hirId);
63 5160 : if (!implBlock->has_trait_ref ())
64 : {
65 90 : rust_warning_at (
66 180 : function.get_function_name ().get_locus (), 0,
67 : "associated function is never used: %qs",
68 180 : function.get_function_name ().as_string ().c_str ());
69 : }
70 : }
71 : else
72 : {
73 183 : rust_warning_at (
74 366 : function.get_function_name ().get_locus (), 0,
75 : "function is never used: %qs",
76 366 : function.get_function_name ().as_string ().c_str ());
77 : }
78 : }
79 15256 : }
80 :
81 1517 : void visit (HIR::StructStruct &stct) override
82 : {
83 1517 : HirId hirId = stct.get_mappings ().get_hirid ();
84 556 : if (should_warn (hirId) && !stct.get_visibility ().is_public ())
85 : {
86 245 : bool name_starts_underscore
87 245 : = stct.get_identifier ().as_string ().at (0) == '_';
88 245 : if (!name_starts_underscore)
89 222 : rust_warning_at (stct.get_locus (), 0,
90 : "struct is never constructed: %qs",
91 444 : stct.get_identifier ().as_string ().c_str ());
92 : }
93 : else
94 : {
95 : // only warn the unused fields when in unwarned struct.
96 2843 : for (auto &field : stct.get_fields ())
97 : {
98 1571 : HirId field_hir_id = field.get_mappings ().get_hirid ();
99 3142 : if (should_warn (field_hir_id)
100 749 : && !field.get_visibility ().is_public ()
101 2085 : && field.get_field_name ().as_string ().at (0) != '_')
102 : {
103 478 : rust_warning_at (field.get_locus (), 0,
104 : "field is never read: %qs",
105 956 : field.get_field_name ().as_string ().c_str ());
106 : }
107 : }
108 : }
109 1517 : }
110 :
111 856 : void visit (HIR::TupleStruct &stct) override
112 : {
113 : // only warn tuple struct unconstructed, and ignoring unused field
114 856 : HirId hirId = stct.get_mappings ().get_hirid ();
115 110 : if (should_warn (hirId) && !stct.get_visibility ().is_public ())
116 : {
117 57 : rust_warning_at (stct.get_locus (), 0,
118 : "struct is never constructed: %qs",
119 114 : stct.get_identifier ().as_string ().c_str ());
120 : }
121 856 : }
122 :
123 7661 : void visit (HIR::ImplBlock &blc) override
124 : {
125 7661 : if (blc.has_impl_items ())
126 : {
127 15539 : for (auto &implItem : blc.get_impl_items ())
128 : {
129 9903 : implItem->accept_vis (*this);
130 : }
131 : }
132 7661 : }
133 :
134 1328 : void visit (HIR::Module &mod) override
135 : {
136 6816 : for (auto &item : mod.get_items ())
137 5488 : item->accept_vis (*this);
138 1328 : }
139 :
140 479 : void visit (HIR::ConstantItem &item) override
141 : {
142 479 : if (!flag_unused_check_2_0)
143 477 : return;
144 4 : std::string var_name = item.get_identifier ().as_string ();
145 2 : bool starts_with_under_score = var_name.at (0) == '_';
146 2 : HirId hirId = item.get_mappings ().get_hirid ();
147 2 : if (should_warn (hirId) && !item.get_visibility ().is_public ()
148 3 : && !starts_with_under_score)
149 1 : rust_warning_at (item.get_locus (), OPT_Wunused_variable,
150 : "deadcode const item %qs",
151 2 : item.get_identifier ().as_string ().c_str ());
152 2 : }
153 :
154 69 : void visit (HIR::StaticItem &item) override
155 : {
156 69 : if (!flag_unused_check_2_0)
157 66 : return;
158 6 : std::string var_name = item.get_identifier ().as_string ();
159 3 : bool starts_with_under_score = var_name.at (0) == '_';
160 3 : HirId hirId = item.get_mappings ().get_hirid ();
161 3 : if (should_warn (hirId) && !item.get_visibility ().is_public ()
162 5 : && !starts_with_under_score)
163 1 : rust_warning_at (item.get_locus (), OPT_Wunused_variable,
164 : "deadcode static item %qs",
165 2 : item.get_identifier ().as_string ().c_str ());
166 3 : }
167 :
168 : private:
169 : std::set<HirId> live_symbols;
170 : Resolver::Resolver *resolver;
171 : Analysis::Mappings &mappings;
172 :
173 4628 : ScanDeadcode (std::set<HirId> &live_symbols)
174 4628 : : live_symbols (live_symbols), resolver (Resolver::Resolver::get ()),
175 9256 : mappings (Analysis::Mappings::get ()){};
176 :
177 19205 : bool should_warn (HirId hirId)
178 : {
179 : // TODO: There are more condition to check if should warn, i.e visibility,
180 : // attributes.
181 19205 : return live_symbols.find (hirId) == live_symbols.end ();
182 : }
183 : };
184 :
185 : } // namespace Analysis
186 : } // namespace Rust
187 :
188 : #endif
|