1 // Copyright (C) 2021-2024 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #include "rust-lint-unused-var.h"
20 #include "print-tree.h"
28 rust_assert (TREE_CODE (*t
) == VAR_DECL
|| TREE_CODE (*t
) == PARM_DECL
29 || TREE_CODE (*t
) == CONST_DECL
);
31 tree var_name
= DECL_NAME (*t
);
32 const char *var_name_ptr
= IDENTIFIER_POINTER (var_name
);
33 bool starts_with_under_score
= strncmp (var_name_ptr
, "_", 1) == 0;
35 bool is_constant
= TREE_CODE (*t
) == CONST_DECL
;
39 // rust_debug ("found var-decl: used %s artifical %s underscore %s name
41 // TREE_USED (*t) ? "true" : "false",
42 // DECL_ARTIFICIAL (*t) ? "true" : "false",
43 // starts_with_under_score ? "true" : "false", var_name_ptr);
46 if (!TREE_USED (*t
) && !DECL_ARTIFICIAL (*t
) && !starts_with_under_score
)
48 warning_at (DECL_SOURCE_LOCATION (*t
),
49 is_constant
? OPT_Wunused_const_variable_
50 : OPT_Wunused_variable
,
51 "unused name %qE", *t
);
56 unused_var_walk_fn (tree
*t
, int *, void *)
58 switch (TREE_CODE (*t
))
72 UnusedVariables::Lint (Compile::Context
&ctx
)
74 for (auto &fndecl
: ctx
.get_func_decls ())
76 for (tree p
= DECL_ARGUMENTS (fndecl
); p
!= NULL_TREE
; p
= DECL_CHAIN (p
))
81 walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl
),
82 &unused_var_walk_fn
, &ctx
);
85 for (auto &var
: ctx
.get_var_decls ())
87 tree t
= ctx
.get_backend ()->var_expression (var
, Location ());
91 for (auto &const_decl
: ctx
.get_const_decls ())
93 check_decl (&const_decl
);
97 } // namespace Analysis