Skip to content

Enable unused_parens for match arms #100093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2022
Merged
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
2 changes: 1 addition & 1 deletion compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere

fn print_string(&mut self, st: &str, style: ast::StrStyle) {
let st = match style {
ast::StrStyle::Cooked => (format!("\"{}\"", st.escape_debug())),
ast::StrStyle::Cooked => format!("\"{}\"", st.escape_debug()),
ast::StrStyle::Raw(n) => {
format!("r{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = st)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }),
..
}) => (tcx.sess.source_map().end_point(fn_decl_span)),
}) => tcx.sess.source_map().end_point(fn_decl_span),
_ => self.body.span,
};

Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ enum UnusedDelimsCtx {
LetScrutineeExpr,
ArrayLenExpr,
AnonConst,
MatchArmExpr,
}

impl From<UnusedDelimsCtx> for &'static str {
Expand All @@ -414,6 +415,7 @@ impl From<UnusedDelimsCtx> for &'static str {
UnusedDelimsCtx::BlockRetValue => "block return value",
UnusedDelimsCtx::LetScrutineeExpr => "`let` scrutinee expression",
UnusedDelimsCtx::ArrayLenExpr | UnusedDelimsCtx::AnonConst => "const expression",
UnusedDelimsCtx::MatchArmExpr => "match arm expression",
}
}
}
Expand Down Expand Up @@ -805,6 +807,18 @@ impl EarlyLintPass for UnusedParens {
}
return;
}
ExprKind::Match(ref _expr, ref arm) => {
for a in arm {
self.check_unused_delims_expr(
cx,
&a.body,
UnusedDelimsCtx::MatchArmExpr,
false,
None,
None,
);
}
}
_ => {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ impl SplitVarLenSlice {
// The only admissible fixed-length slice is one of the array size. Whether `max_slice`
// is fixed-length or variable-length, it will be the only relevant slice to output
// here.
Some(_) => (0..0), // empty range
Some(_) => 0..0, // empty range
// We cover all arities in the range `(self.arity..infinity)`. We split that range into
// two: lengths smaller than `max_slice.arity()` are treated independently as
// fixed-lengths slices, and lengths above are captured by `max_slice`.
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/lint/unused/issue-92751.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[deny(unused)]
pub fn broken(x: Option<()>) -> i32 {
match x {
Some(()) => (1), //~ ERROR unnecessary parentheses around match arm expression
None => (2), //~ ERROR unnecessary parentheses around match arm expression
}
}

fn main() { }
32 changes: 32 additions & 0 deletions src/test/ui/lint/unused/issue-92751.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
error: unnecessary parentheses around match arm expression
--> $DIR/issue-92751.rs:4:21
|
LL | Some(()) => (1),
| ^ ^
|
note: the lint level is defined here
--> $DIR/issue-92751.rs:1:8
|
LL | #[deny(unused)]
| ^^^^^^
= note: `#[deny(unused_parens)]` implied by `#[deny(unused)]`
help: remove these parentheses
|
LL - Some(()) => (1),
LL + Some(()) => 1,
|

error: unnecessary parentheses around match arm expression
--> $DIR/issue-92751.rs:5:17
|
LL | None => (2),
| ^ ^
|
help: remove these parentheses
|
LL - None => (2),
LL + None => 2,
|

error: aborting due to 2 previous errors