Skip to content

Commit 145b26c

Browse files
committed
scan-deadcode: Factor warnings into more functions
gcc/rust/ChangeLog: * checks/lints/rust-lint-scan-deadcode.h: Add more functions that are reused throught the visitor and make `should_warn` perform all checks.
1 parent d475b75 commit 145b26c

1 file changed

Lines changed: 38 additions & 40 deletions

File tree

gcc/rust/checks/lints/rust-lint-scan-deadcode.h

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "rust-hir-full-decls.h"
2323
#include "rust-hir-map.h"
24+
#include "rust-hir-visibility.h"
2425
#include "rust-lint-marklive.h"
2526
#include "rust-name-resolver.h"
2627
#include "rust-diagnostics.h"
@@ -49,13 +50,16 @@ class ScanDeadcode : public MarkLiveBase
4950
it.get ()->accept_vis (sdc);
5051
};
5152

53+
bool name_starts_with_underscore (const Identifier &identifier)
54+
{
55+
return identifier.as_string ().rfind ('_', 0) == 0;
56+
}
57+
5258
void visit (HIR::Function &function) override
5359
{
5460
HirId hirId = function.get_mappings ().get_hirid ();
55-
auto starts_with_underscore
56-
= function.get_function_name ().as_string ().rfind ('_', 0) == 0;
57-
if (should_warn (hirId) && !function.get_visibility ().is_public ()
58-
&& !starts_with_underscore)
61+
if (should_warn (hirId, function.get_function_name (),
62+
function.get_visibility ()))
5963
{
6064
if (mappings.is_impl_item (hirId))
6165
{
@@ -78,59 +82,46 @@ class ScanDeadcode : public MarkLiveBase
7882
}
7983
}
8084

81-
void visit (HIR::StructStruct &stct) override
85+
void visit (HIR::StructStruct &strukt) override
8286
{
83-
HirId hirId = stct.get_mappings ().get_hirid ();
84-
if (should_warn (hirId) && !stct.get_visibility ().is_public ())
87+
HirId hirId = strukt.get_mappings ().get_hirid ();
88+
if (should_warn (hirId, strukt.get_identifier (), strukt.get_visibility ()))
8589
{
86-
bool name_starts_underscore
87-
= stct.get_identifier ().as_string ().at (0) == '_';
88-
if (!name_starts_underscore)
89-
rust_warning_at (stct.get_locus (), 0,
90-
"struct is never constructed: %qs",
91-
stct.get_identifier ().as_string ().c_str ());
90+
rust_warning_at (strukt.get_locus (), 0,
91+
"struct is never constructed: %qs",
92+
strukt.get_identifier ().as_string ().c_str ());
9293
}
9394
else
9495
{
9596
// only warn the unused fields when in unwarned struct.
96-
for (auto &field : stct.get_fields ())
97+
for (auto &field : strukt.get_fields ())
9798
{
9899
HirId field_hir_id = field.get_mappings ().get_hirid ();
99-
if (should_warn (field_hir_id)
100-
&& !field.get_visibility ().is_public ()
101-
&& field.get_field_name ().as_string ().at (0) != '_')
102-
{
103-
rust_warning_at (field.get_locus (), 0,
104-
"field is never read: %qs",
105-
field.get_field_name ().as_string ().c_str ());
106-
}
100+
if (should_warn (field_hir_id, field.get_field_name (),
101+
field.get_visibility ()))
102+
rust_warning_at (field.get_locus (), 0,
103+
"field is never read: %qs",
104+
field.get_field_name ().as_string ().c_str ());
107105
}
108106
}
109107
}
110108

111-
void visit (HIR::TupleStruct &stct) override
109+
void visit (HIR::TupleStruct &strukt) override
112110
{
113111
// only warn tuple struct unconstructed, and ignoring unused field
114-
HirId hirId = stct.get_mappings ().get_hirid ();
115-
if (should_warn (hirId) && !stct.get_visibility ().is_public ())
116-
{
117-
rust_warning_at (stct.get_locus (), 0,
118-
"struct is never constructed: %qs",
119-
stct.get_identifier ().as_string ().c_str ());
120-
}
112+
HirId hirId = strukt.get_mappings ().get_hirid ();
113+
if (should_warn (hirId, strukt.get_identifier (), strukt.get_visibility ()))
114+
rust_warning_at (strukt.get_locus (), 0,
115+
"struct is never constructed: %qs",
116+
strukt.get_identifier ().as_string ().c_str ());
121117
}
122118

123119
void visit (HIR::Enum &enm) override
124120
{
125121
HirId hirId = enm.get_mappings ().get_hirid ();
126-
if (should_warn (hirId) && !enm.get_visibility ().is_public ())
127-
{
128-
bool name_starts_underscore
129-
= enm.get_identifier ().as_string ().at (0) == '_';
130-
if (!name_starts_underscore)
131-
rust_warning_at (enm.get_locus (), 0, "enum is never used: %qs",
132-
enm.get_identifier ().as_string ().c_str ());
133-
}
122+
if (should_warn (hirId, enm.get_identifier (), enm.get_visibility ()))
123+
rust_warning_at (enm.get_locus (), 0, "enum is never used: %qs",
124+
enm.get_identifier ().as_string ().c_str ());
134125
}
135126

136127
void visit (HIR::ImplBlock &blc) override
@@ -159,11 +150,18 @@ class ScanDeadcode : public MarkLiveBase
159150
: live_symbols (live_symbols), resolver (Resolver::Resolver::get ()),
160151
mappings (Analysis::Mappings::get ()){};
161152

162-
bool should_warn (HirId hirId)
153+
bool should_warn (HirId hir_id, const Identifier &identifier,
154+
HIR::Visibility visibility)
163155
{
156+
if (name_starts_with_underscore (identifier))
157+
return false;
158+
159+
if (visibility.is_public ())
160+
return false;
161+
164162
// TODO: There are more condition to check if should warn, i.e visibility,
165163
// attributes.
166-
return live_symbols.find (hirId) == live_symbols.end ();
164+
return live_symbols.find (hir_id) == live_symbols.end ();
167165
}
168166
};
169167

0 commit comments

Comments
 (0)