Enable unused_parens for match arms

This commit is contained in:
wcampbell 2022-08-03 00:00:04 -04:00
parent c9e134e1b6
commit 8dd44f1af4
6 changed files with 58 additions and 3 deletions

View File

@ -377,7 +377,7 @@ fn print_literal(&mut self, lit: &ast::Lit) {
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)
}

View File

@ -839,7 +839,7 @@ fn give_name_if_anonymous_region_appears_in_yield_ty(
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,
};

View File

@ -396,6 +396,7 @@ enum UnusedDelimsCtx {
LetScrutineeExpr,
ArrayLenExpr,
AnonConst,
MatchArmExpr,
}
impl From<UnusedDelimsCtx> for &'static str {
@ -414,6 +415,7 @@ fn from(ctx: UnusedDelimsCtx) -> &'static str {
UnusedDelimsCtx::BlockRetValue => "block return value",
UnusedDelimsCtx::LetScrutineeExpr => "`let` scrutinee expression",
UnusedDelimsCtx::ArrayLenExpr | UnusedDelimsCtx::AnonConst => "const expression",
UnusedDelimsCtx::MatchArmExpr => "match arm expression",
}
}
}
@ -805,6 +807,18 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
}
return;
}
ExprKind::Match(ref _expr, ref arm) => {
for a in arm {
self.check_unused_delims_expr(
cx,
&a.body,
UnusedDelimsCtx::MatchArmExpr,
false,
None,
None,
);
}
}
_ => {}
}

View File

@ -617,7 +617,7 @@ fn iter<'a>(&'a self) -> impl Iterator<Item = Slice> + Captures<'a> {
// 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`.

View File

@ -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() { }

View File

@ -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