aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/checks/lints/rust-lint-scan-deadcode.h
blob: 672c7611f5394ee7f2c4745cfda041ea47d8f292 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (C) 2021-2024 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#ifndef RUST_HIR_SCAN_DEADCODE
#define RUST_HIR_SCAN_DEADCODE

#include "rust-hir-full-decls.h"
#include "rust-hir-map.h"
#include "rust-lint-marklive.h"
#include "rust-name-resolver.h"
#include "rust-diagnostics.h"

namespace Rust {
namespace Analysis {

// Scan item symbols and warn the symbol if it is not in the live_symbols set.
// There are three kinds of item we should handle in this pass.
// 1. Function item
// 2. The function item in the impl block without trait
// 3. StructStruct, e.g., `Struct Foo{one: 1, two: 2}`. Furthermore, the unused
//    struct fields will be warned too.
// 4. TupleStruct, e.g., `Struct Foo(i32, i32)`
class ScanDeadcode : public MarkLiveBase
{
  using Rust::Analysis::MarkLiveBase::visit;

public:
  static void Scan (HIR::Crate &crate)
  {
    std::set<HirId> live_symbols = Analysis::MarkLive::Analysis (crate);
    ScanDeadcode sdc (live_symbols);
    for (auto &it : crate.get_items ())
      it.get ()->accept_vis (sdc);
  };

  void visit (HIR::Function &function) override
  {
    HirId hirId = function.get_mappings ().get_hirid ();
    if (should_warn (hirId) && !function.get_visibility ().is_public ())
      {
	if (mappings.is_impl_item (hirId))
	  {
	    HIR::ImplBlock *implBlock = mappings.lookup_associated_impl (hirId);
	    if (!implBlock->has_trait_ref ())
	      {
		rust_warning_at (
		  function.get_function_name ().get_locus (), 0,
		  "associated function is never used: %<%s%>",
		  function.get_function_name ().as_string ().c_str ());
	      }
	  }
	else
	  {
	    rust_warning_at (
	      function.get_function_name ().get_locus (), 0,
	      "function is never used: %<%s%>",
	      function.get_function_name ().as_string ().c_str ());
	  }
      }
  }

  void visit (HIR::StructStruct &stct) override
  {
    HirId hirId = stct.get_mappings ().get_hirid ();
    if (should_warn (hirId) && !stct.get_visibility ().is_public ())
      {
	bool name_starts_underscore
	  = stct.get_identifier ().as_string ().at (0) == '_';
	if (!name_starts_underscore)
	  rust_warning_at (stct.get_locus (), 0,
			   "struct is never constructed: %<%s%>",
			   stct.get_identifier ().as_string ().c_str ());
      }
    else
      {
	// only warn the unused fields when in unwarned struct.
	for (auto &field : stct.get_fields ())
	  {
	    HirId field_hir_id = field.get_mappings ().get_hirid ();
	    if (should_warn (field_hir_id)
		&& !field.get_visibility ().is_public ())
	      {
		rust_warning_at (field.get_locus (), 0,
				 "field is never read: %<%s%>",
				 field.get_field_name ().as_string ().c_str ());
	      }
	  }
      }
  }

  void visit (HIR::TupleStruct &stct) override
  {
    // only warn tuple struct unconstructed, and ignoring unused field
    HirId hirId = stct.get_mappings ().get_hirid ();
    if (should_warn (hirId) && !stct.get_visibility ().is_public ())
      {
	rust_warning_at (stct.get_locus (), 0,
			 "struct is never constructed: %<%s%>",
			 stct.get_identifier ().as_string ().c_str ());
      }
  }

  void visit (HIR::ImplBlock &blc) override
  {
    if (blc.has_impl_items ())
      {
	for (auto &implItem : blc.get_impl_items ())
	  {
	    implItem->accept_vis (*this);
	  }
      }
  }

  void visit (HIR::Module &mod) override
  {
    for (auto &item : mod.get_items ())
      item->accept_vis (*this);
  }

private:
  std::set<HirId> live_symbols;
  Resolver::Resolver *resolver;
  Analysis::Mappings &mappings;

  ScanDeadcode (std::set<HirId> &live_symbols)
    : live_symbols (live_symbols), resolver (Resolver::Resolver::get ()),
      mappings (Analysis::Mappings::get ()){};

  bool should_warn (HirId hirId)
  {
    // TODO: There are more condition to check if should warn, i.e visibility,
    // attributes.
    return live_symbols.find (hirId) == live_symbols.end ();
  }
};

} // namespace Analysis
} // namespace Rust

#endif