Branch data Line data Source code
1 : : // Copyright (C) 2021-2025 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 : 51458 : check_decl (tree *t)
27 : : {
28 : 51458 : rust_assert (TREE_CODE (*t) == VAR_DECL || TREE_CODE (*t) == PARM_DECL
29 : : || TREE_CODE (*t) == CONST_DECL);
30 : :
31 : 51458 : tree var_name = DECL_NAME (*t);
32 : 51458 : const char *var_name_ptr = IDENTIFIER_POINTER (var_name);
33 : 51458 : bool starts_with_under_score = strncmp (var_name_ptr, "_", 1) == 0;
34 : 51458 : bool is_self = strcmp (var_name_ptr, "self") == 0;
35 : :
36 : 51458 : 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 : 2764 : if (!TREE_USED (*t) && !DECL_ARTIFICIAL (*t) && !starts_with_under_score
48 : 54184 : && !is_self)
49 : : {
50 : 1948 : warning_at (DECL_SOURCE_LOCATION (*t),
51 : 3870 : is_constant ? OPT_Wunused_const_variable_
52 : : : OPT_Wunused_variable,
53 : : "unused name %qE", *t);
54 : : }
55 : 51458 : }
56 : :
57 : : static tree
58 : 369111 : unused_var_walk_fn (tree *t, int *, void *)
59 : : {
60 : 369111 : switch (TREE_CODE (*t))
61 : : {
62 : 36214 : case VAR_DECL:
63 : 36214 : case CONST_DECL:
64 : 36214 : check_decl (t);
65 : 36214 : break;
66 : :
67 : : default:
68 : : break;
69 : : }
70 : 369111 : return NULL_TREE;
71 : : }
72 : :
73 : : void
74 : 3866 : UnusedVariables::Lint (Compile::Context &ctx)
75 : : {
76 : 18421 : for (auto &fndecl : ctx.get_func_decls ())
77 : : {
78 : 29289 : for (tree p = DECL_ARGUMENTS (fndecl); p != NULL_TREE; p = DECL_CHAIN (p))
79 : : {
80 : 14734 : check_decl (&p);
81 : : }
82 : :
83 : 14555 : walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
84 : : &unused_var_walk_fn, &ctx);
85 : : }
86 : :
87 : 3912 : for (auto &var : ctx.get_var_decls ())
88 : : {
89 : 46 : tree decl = var->get_decl ();
90 : 46 : check_decl (&decl);
91 : : }
92 : :
93 : 4330 : for (auto &const_decl : ctx.get_const_decls ())
94 : : {
95 : 464 : check_decl (&const_decl);
96 : : }
97 : 3866 : }
98 : :
99 : : } // namespace Analysis
100 : : } // namespace Rust
|