Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions gcc/rust/checks/lints/unused/rust-unused-checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ UnusedChecker::visit_loop_label (HIR::LoopLabel &label)
rust_warning_at (lifetime.get_locus (), OPT_Wunused_variable,
"unused label %qs", lifetime.to_string ().c_str ());
}
void
UnusedChecker::visit (HIR::GroupedExpr &expr)
{
auto id = expr.get_mappings ().get_hirid ();
if (!unused_context.is_group_used (id))
rust_warning_at (expr.get_locus (), OPT_Wunused_variable,
"unnecessary parentheses");
}

} // namespace Analysis
} // namespace Rust
1 change: 1 addition & 0 deletions gcc/rust/checks/lints/unused/rust-unused-checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class UnusedChecker : public HIR::DefaultHIRVisitor
virtual void visit (HIR::StructPatternFieldIdent &identifier) override;
virtual void visit (HIR::EmptyStmt &stmt) override;
virtual void visit_loop_label (HIR::LoopLabel &label) override;
virtual void visit (HIR::GroupedExpr &expr) override;
};
} // namespace Analysis
} // namespace Rust
20 changes: 20 additions & 0 deletions gcc/rust/checks/lints/unused/rust-unused-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,25 @@ UnusedCollector::visit (HIR::ContinueExpr &expr)
walk (expr);
}

void
UnusedCollector::visit (HIR::LazyBooleanExpr &expr)
{
if (expr.get_lhs ().get_expression_type () == HIR::Expr::ExprType::Grouped)
mark_group_used (expr.get_lhs ());
if (expr.get_rhs ().get_expression_type () == HIR::Expr::ExprType::Grouped)
mark_group_used (expr.get_rhs ());
walk (expr);
}

void
UnusedCollector::visit (HIR::ArithmeticOrLogicalExpr &expr)
{
if (expr.get_lhs ().get_expression_type () == HIR::Expr::ExprType::Grouped)
mark_group_used (expr.get_lhs ());
if (expr.get_rhs ().get_expression_type () == HIR::Expr::ExprType::Grouped)
mark_group_used (expr.get_rhs ());
walk (expr);
}

} // namespace Analysis
} // namespace Rust
9 changes: 9 additions & 0 deletions gcc/rust/checks/lints/unused/rust-unused-collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class UnusedCollector : public HIR::DefaultHIRVisitor
virtual void visit (HIR::BreakExpr &expr) override;
virtual void visit (HIR::ContinueExpr &expr) override;

// Unused parens
virtual void visit (HIR::ArithmeticOrLogicalExpr &expr) override;
virtual void visit (HIR::LazyBooleanExpr &expr) override;

template <typename T> HirId get_def_id (T &path_expr)
{
NodeId ast_node_id = path_expr.get_mappings ().get_nodeid ();
Expand All @@ -75,6 +79,11 @@ class UnusedCollector : public HIR::DefaultHIRVisitor
auto def_id = get_def_id (path_expr);
unused_context.add_label (def_id);
}

template <typename T> void mark_group_used (T &path_expr)
{
unused_context.add_group (path_expr.get_mappings ().get_hirid ());
}
};
} // namespace Analysis
} // namespace Rust
13 changes: 13 additions & 0 deletions gcc/rust/checks/lints/unused/rust-unused-context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ UnusedContext::is_label_used (HirId id) const
return used_labels.find (id) != used_labels.end ();
}

void
UnusedContext::add_group (HirId id)

{
used_groups.emplace (id);
}

bool
UnusedContext::is_group_used (HirId id) const
{
return used_groups.find (id) != used_groups.end ();
}

std::string
UnusedContext::as_string () const
{
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/checks/lints/unused/rust-unused-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ class UnusedContext
void add_label (HirId id);
bool is_label_used (HirId id) const;

// Unused grup
void add_group (HirId id);
bool is_group_used (HirId id) const;

std::string as_string () const;

private:
std::unordered_set<HirId> used_vars;
std::unordered_set<HirId> mutable_vars;
std::map<HirId, std::vector<HirId>> assigned_vars;
std::unordered_set<HirId> used_labels;
std::unordered_set<HirId> used_groups;
};

} // namespace Analysis
Expand Down
10 changes: 10 additions & 0 deletions gcc/testsuite/rust/compile/unused-parens_0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// { dg-additional-options "-frust-unused-check-2.0" }
pub fn a() {
let _a = (true) && (false);
let _b = (((2) + 1));
// { dg-warning "unnecessary parentheses" "" { target *-*-* } .-1 }
let _c = ((((1))));
// { dg-warning "unnecessary parentheses" "" { target *-*-* } .-1 }
let _d = (3 << 2) + 1;
}