Skip to content

Commit 5ceb339

Browse files
committed
gccrs: add unused parens lint
gcc/rust/ChangeLog: * checks/lints/unused/rust-unused-checker.cc (UnusedChecker::visit): Add warning. * checks/lints/unused/rust-unused-checker.h: Likewise. * checks/lints/unused/rust-unused-collector.cc (UnusedCollector::visit): Check if a grouped expr in arithmetical or boolean expr is used. * checks/lints/unused/rust-unused-collector.h: Likewise. * checks/lints/unused/rust-unused-context.cc (UnusedContext::add_group): (UnusedContext::is_group_used): Methods helper. * checks/lints/unused/rust-unused-context.h: Likewise. Signed-off-by: Lucas Ly Ba <[email protected]>
1 parent 32622b7 commit 5ceb339

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

gcc/rust/checks/lints/unused/rust-unused-checker.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ UnusedChecker::visit_loop_label (HIR::LoopLabel &label)
134134
rust_warning_at (lifetime.get_locus (), OPT_Wunused_variable,
135135
"unused label %qs", lifetime.to_string ().c_str ());
136136
}
137+
void
138+
UnusedChecker::visit (HIR::GroupedExpr &expr)
139+
{
140+
auto id = expr.get_mappings ().get_hirid ();
141+
if (!unused_context.is_group_used (id))
142+
rust_warning_at (expr.get_locus (), OPT_Wunused_variable,
143+
"unnecessary parentheses");
144+
}
137145

138146
} // namespace Analysis
139147
} // namespace Rust

gcc/rust/checks/lints/unused/rust-unused-checker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class UnusedChecker : public HIR::DefaultHIRVisitor
4545
virtual void visit (HIR::StructPatternFieldIdent &identifier) override;
4646
virtual void visit (HIR::EmptyStmt &stmt) override;
4747
virtual void visit_loop_label (HIR::LoopLabel &label) override;
48+
virtual void visit (HIR::GroupedExpr &expr) override;
4849
};
4950
} // namespace Analysis
5051
} // namespace Rust

gcc/rust/checks/lints/unused/rust-unused-collector.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,27 @@ UnusedCollector::visit (HIR::ContinueExpr &expr)
106106
walk (expr);
107107
}
108108

109+
void
110+
UnusedCollector::visit (HIR::LazyBooleanExpr &expr)
111+
{
112+
if (expr.get_lhs ().get_expression_type () == HIR::Expr::ExprType::Grouped)
113+
mark_group_used (expr.get_lhs ());
114+
if (expr.get_rhs ().get_expression_type () == HIR::Expr::ExprType::Grouped)
115+
mark_group_used (expr.get_rhs ());
116+
walk (expr);
117+
}
118+
119+
void
120+
UnusedCollector::visit (HIR::ArithmeticOrLogicalExpr &expr)
121+
{
122+
std::cout << expr.get_lhs ().to_string () << "\n";
123+
std::cout << expr.get_rhs ().to_string () << "\n";
124+
if (expr.get_lhs ().get_expression_type () == HIR::Expr::ExprType::Grouped)
125+
mark_group_used (expr.get_lhs ());
126+
if (expr.get_rhs ().get_expression_type () == HIR::Expr::ExprType::Grouped)
127+
mark_group_used (expr.get_rhs ());
128+
walk (expr);
129+
}
130+
109131
} // namespace Analysis
110132
} // namespace Rust

gcc/rust/checks/lints/unused/rust-unused-collector.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class UnusedCollector : public HIR::DefaultHIRVisitor
5555
virtual void visit (HIR::BreakExpr &expr) override;
5656
virtual void visit (HIR::ContinueExpr &expr) override;
5757

58+
// Unused parens
59+
virtual void visit (HIR::ArithmeticOrLogicalExpr &expr) override;
60+
virtual void visit (HIR::LazyBooleanExpr &expr) override;
61+
5862
template <typename T> HirId get_def_id (T &path_expr)
5963
{
6064
NodeId ast_node_id = path_expr.get_mappings ().get_nodeid ();
@@ -75,6 +79,12 @@ class UnusedCollector : public HIR::DefaultHIRVisitor
7579
auto def_id = get_def_id (path_expr);
7680
unused_context.add_label (def_id);
7781
}
82+
83+
template <typename T> void mark_group_used (T &path_expr)
84+
{
85+
auto def_id = get_def_id (path_expr);
86+
unused_context.add_group (def_id);
87+
}
7888
};
7989
} // namespace Analysis
8090
} // namespace Rust

gcc/rust/checks/lints/unused/rust-unused-context.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ UnusedContext::is_label_used (HirId id) const
8686
return used_labels.find (id) != used_labels.end ();
8787
}
8888

89+
void
90+
UnusedContext::add_group (HirId id)
91+
92+
{
93+
used_groups.emplace (id);
94+
}
95+
96+
bool
97+
UnusedContext::is_group_used (HirId id) const
98+
{
99+
return used_groups.find (id) != used_groups.end ();
100+
}
101+
89102
std::string
90103
UnusedContext::as_string () const
91104
{

gcc/rust/checks/lints/unused/rust-unused-context.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,18 @@ class UnusedContext
4242
void add_label (HirId id);
4343
bool is_label_used (HirId id) const;
4444

45+
// Unused grup
46+
void add_group (HirId id);
47+
bool is_group_used (HirId id) const;
48+
4549
std::string as_string () const;
4650

4751
private:
4852
std::unordered_set<HirId> used_vars;
4953
std::unordered_set<HirId> mutable_vars;
5054
std::map<HirId, std::vector<HirId>> assigned_vars;
5155
std::unordered_set<HirId> used_labels;
56+
std::unordered_set<HirId> used_groups;
5257
};
5358

5459
} // namespace Analysis

0 commit comments

Comments
 (0)