Skip to content

Commit 547e5eb

Browse files
committed
Do not check unused braces on inline consts
1 parent 03defb6 commit 547e5eb

File tree

1 file changed

+53
-18
lines changed

1 file changed

+53
-18
lines changed

compiler/rustc_lint/src/unused.rs

+53-18
Original file line numberDiff line numberDiff line change
@@ -839,10 +839,6 @@ impl EarlyLintPass for UnusedParens {
839839
}
840840
}
841841

842-
fn check_anon_const(&mut self, cx: &EarlyContext<'_>, c: &ast::AnonConst) {
843-
self.check_unused_delims_expr(cx, &c.value, UnusedDelimsCtx::AnonConst, false, None, None);
844-
}
845-
846842
fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
847843
if let StmtKind::Local(ref local) = s.kind {
848844
self.check_unused_parens_pat(cx, &local.pat, false, false);
@@ -965,13 +961,6 @@ impl UnusedDelimLint for UnusedBraces {
965961
if !Self::is_expr_delims_necessary(expr, followed_by_block)
966962
&& (ctx != UnusedDelimsCtx::AnonConst
967963
|| matches!(expr.kind, ast::ExprKind::Lit(_)))
968-
// array length expressions are checked during `check_anon_const` and `check_ty`,
969-
// once as `ArrayLenExpr` and once as `AnonConst`.
970-
//
971-
// As we do not want to lint this twice, we do not emit an error for
972-
// `ArrayLenExpr` if `AnonConst` would do the same.
973-
&& (ctx != UnusedDelimsCtx::ArrayLenExpr
974-
|| !matches!(expr.kind, ast::ExprKind::Lit(_)))
975964
&& !cx.sess().source_map().is_multiline(value.span)
976965
&& value.attrs.is_empty()
977966
&& !value.span.from_expansion()
@@ -999,21 +988,54 @@ impl UnusedDelimLint for UnusedBraces {
999988
}
1000989

1001990
impl EarlyLintPass for UnusedBraces {
991+
fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
992+
<Self as UnusedDelimLint>::check_stmt(self, cx, s)
993+
}
994+
1002995
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
1003-
<Self as UnusedDelimLint>::check_expr(self, cx, e)
996+
<Self as UnusedDelimLint>::check_expr(self, cx, e);
997+
998+
if let ExprKind::Repeat(_, ref anon_const) = e.kind {
999+
self.check_unused_delims_expr(
1000+
cx,
1001+
&anon_const.value,
1002+
UnusedDelimsCtx::AnonConst,
1003+
false,
1004+
None,
1005+
None,
1006+
);
1007+
}
10041008
}
10051009

1006-
fn check_anon_const(&mut self, cx: &EarlyContext<'_>, c: &ast::AnonConst) {
1007-
self.check_unused_delims_expr(cx, &c.value, UnusedDelimsCtx::AnonConst, false, None, None);
1010+
fn check_generic_arg(&mut self, cx: &EarlyContext<'_>, arg: &ast::GenericArg) {
1011+
if let ast::GenericArg::Const(ct) = arg {
1012+
self.check_unused_delims_expr(
1013+
cx,
1014+
&ct.value,
1015+
UnusedDelimsCtx::AnonConst,
1016+
false,
1017+
None,
1018+
None,
1019+
);
1020+
}
10081021
}
10091022

1010-
fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
1011-
<Self as UnusedDelimLint>::check_stmt(self, cx, s)
1023+
fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &ast::Variant) {
1024+
if let Some(anon_const) = &v.disr_expr {
1025+
self.check_unused_delims_expr(
1026+
cx,
1027+
&anon_const.value,
1028+
UnusedDelimsCtx::AnonConst,
1029+
false,
1030+
None,
1031+
None,
1032+
);
1033+
}
10121034
}
10131035

10141036
fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
1015-
if let &ast::TyKind::Paren(ref r) = &ty.kind {
1016-
if let ast::TyKind::Array(_, ref len) = r.kind {
1037+
match ty.kind {
1038+
ast::TyKind::Array(_, ref len) => {
10171039
self.check_unused_delims_expr(
10181040
cx,
10191041
&len.value,
@@ -1023,6 +1045,19 @@ impl EarlyLintPass for UnusedBraces {
10231045
None,
10241046
);
10251047
}
1048+
1049+
ast::TyKind::Typeof(ref anon_const) => {
1050+
self.check_unused_delims_expr(
1051+
cx,
1052+
&anon_const.value,
1053+
UnusedDelimsCtx::AnonConst,
1054+
false,
1055+
None,
1056+
None,
1057+
);
1058+
}
1059+
1060+
_ => {}
10261061
}
10271062
}
10281063

0 commit comments

Comments
 (0)