Skip to content

Commit a18f230

Browse files
committed
gccrs: add unused parens lint
gcc/rust/ChangeLog: * checks/lints/unused/rust-unused-checker.cc (UnusedChecker::visit): Visit GroupExpr and add unused parens warning if not necessary. * checks/lints/unused/rust-unused-checker.h: New. * 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: New. * checks/lints/unused/rust-unused-context.cc (UnusedContext::add_group): Method helper. (UnusedContext::is_group_used): Likewise. * checks/lints/unused/rust-unused-context.h: Likewise. gcc/testsuite/ChangeLog: * rust/compile/unused-parens_0.rs: New test. Signed-off-by: Lucas Ly Ba <[email protected]>
1 parent 17231c8 commit a18f230

File tree

7 files changed

+66
-0
lines changed

7 files changed

+66
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,25 @@ 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+
if (expr.get_lhs ().get_expression_type () == HIR::Expr::ExprType::Grouped)
123+
mark_group_used (expr.get_lhs ());
124+
if (expr.get_rhs ().get_expression_type () == HIR::Expr::ExprType::Grouped)
125+
mark_group_used (expr.get_rhs ());
126+
walk (expr);
127+
}
128+
109129
} // namespace Analysis
110130
} // namespace Rust

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

Lines changed: 9 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,11 @@ 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+
unused_context.add_group (path_expr.get_mappings ().get_hirid ());
86+
}
7887
};
7988
} // namespace Analysis
8089
} // 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
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// { dg-additional-options "-frust-unused-check-2.0" }
2+
pub fn a() {
3+
let _a = (true) && (false);
4+
let _b = (((2) + 1));
5+
// { dg-warning "unnecessary parentheses" "" { target *-*-* } .-1 }
6+
let _c = ((((1))));
7+
// { dg-warning "unnecessary parentheses" "" { target *-*-* } .-1 }
8+
let _d = (3 << 2) + 1;
9+
}
10+

0 commit comments

Comments
 (0)