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 : #include "rust-lint-unused-var.h"
20 : #include "rust-gcc.h"
21 :
22 : namespace Rust {
23 : namespace Analysis {
24 :
25 : static void
26 52525 : check_decl (tree *t)
27 : {
28 52525 : rust_assert (TREE_CODE (*t) == VAR_DECL || TREE_CODE (*t) == PARM_DECL
29 : || TREE_CODE (*t) == CONST_DECL);
30 :
31 52525 : tree var_name = DECL_NAME (*t);
32 52525 : const char *var_name_ptr = IDENTIFIER_POINTER (var_name);
33 52525 : bool starts_with_under_score = strncmp (var_name_ptr, "_", 1) == 0;
34 52525 : bool is_self = strcmp (var_name_ptr, "self") == 0;
35 :
36 52525 : bool is_constant = TREE_CODE (*t) == CONST_DECL;
37 : // if (!is_constant)
38 : // {
39 : // debug_tree (*t);
40 : // rust_debug ("found var-decl: used %s artifical %s underscore %s name
41 : // %s",
42 : // TREE_USED (*t) ? "true" : "false",
43 : // DECL_ARTIFICIAL (*t) ? "true" : "false",
44 : // starts_with_under_score ? "true" : "false", var_name_ptr);
45 : // }
46 :
47 2816 : if (!TREE_USED (*t) && !DECL_ARTIFICIAL (*t) && !starts_with_under_score
48 55302 : && !is_self)
49 : {
50 1959 : warning_at (DECL_SOURCE_LOCATION (*t),
51 3891 : is_constant ? OPT_Wunused_const_variable_
52 : : OPT_Wunused_variable,
53 : "unused name %qE", *t);
54 : }
55 52525 : }
56 :
57 : static tree
58 378003 : unused_var_walk_fn (tree *t, int *, void *)
59 : {
60 378003 : switch (TREE_CODE (*t))
61 : {
62 37189 : case VAR_DECL:
63 37189 : case CONST_DECL:
64 37189 : check_decl (t);
65 37189 : break;
66 :
67 : default:
68 : break;
69 : }
70 378003 : return NULL_TREE;
71 : }
72 :
73 : void
74 4019 : UnusedVariables::Lint (Compile::Context &ctx)
75 : {
76 18778 : for (auto &fndecl : ctx.get_func_decls ())
77 : {
78 29555 : for (tree p = DECL_ARGUMENTS (fndecl); p != NULL_TREE; p = DECL_CHAIN (p))
79 : {
80 14796 : check_decl (&p);
81 : }
82 :
83 14759 : walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
84 : &unused_var_walk_fn, &ctx);
85 : }
86 :
87 4065 : for (auto &var : ctx.get_var_decls ())
88 : {
89 46 : tree decl = var->get_decl ();
90 46 : check_decl (&decl);
91 : }
92 :
93 4513 : for (auto &const_decl : ctx.get_const_decls ())
94 : {
95 494 : check_decl (&const_decl);
96 : }
97 4019 : }
98 :
99 : } // namespace Analysis
100 : } // namespace Rust
|