Skip to content

Commit 0f0556b

Browse files
committed
fix clippy
1 parent 77733ba commit 0f0556b

File tree

6 files changed

+50
-20
lines changed

6 files changed

+50
-20
lines changed

src/tools/clippy/clippy_lints/src/trailing_empty_array.rs

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'tcx>, item: &'tcx
5959
if let ItemKind::Struct(data, _) = &item.kind;
6060
if let Some(last_field) = data.fields().last();
6161
if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind;
62+
if let rustc_hir::ArrayLen::Body(length) = length;
6263

6364
// Then check if that that array zero-sized
6465
let length_ldid = cx.tcx.hir().local_def_id(length.hir_id);

src/tools/clippy/clippy_lints/src/utils/author.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_ast::ast::{LitFloatType, LitKind};
66
use rustc_ast::LitIntType;
77
use rustc_data_structures::fx::FxHashMap;
88
use rustc_hir as hir;
9-
use rustc_hir::{ExprKind, FnRetTy, HirId, Lit, PatKind, QPath, StmtKind, TyKind};
9+
use rustc_hir::{ArrayLen, ExprKind, FnRetTy, HirId, Lit, PatKind, QPath, StmtKind, TyKind};
1010
use rustc_lint::{LateContext, LateLintPass, LintContext};
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
1212
use rustc_span::symbol::{Ident, Symbol};
@@ -567,7 +567,14 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
567567
bind!(self, value, length);
568568
kind!("Repeat({value}, {length})");
569569
self.expr(value);
570-
self.body(field!(length.body));
570+
match length.value {
571+
ArrayLen::Infer(..) => out!("if let ArrayLen::Infer(..) = length;"),
572+
ArrayLen::Body(anon_const) => {
573+
bind!(self, anon_const);
574+
out!("if let ArrayLen::Body({anon_const}) = {length};");
575+
self.body(field!(anon_const.body));
576+
}
577+
}
571578
},
572579
ExprKind::Err => kind!("Err"),
573580
ExprKind::DropTemps(expr) => {

src/tools/clippy/clippy_lints/src/utils/inspector.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,17 @@ fn print_expr(cx: &LateContext<'_>, expr: &hir::Expr<'_>, indent: usize) {
334334
println!("{}anon_const:", ind);
335335
print_expr(cx, &cx.tcx.hir().body(anon_const.body).value, indent + 1);
336336
},
337-
hir::ExprKind::Repeat(val, ref anon_const) => {
337+
hir::ExprKind::Repeat(val, length) => {
338338
println!("{}Repeat", ind);
339339
println!("{}value:", ind);
340340
print_expr(cx, val, indent + 1);
341341
println!("{}repeat count:", ind);
342-
print_expr(cx, &cx.tcx.hir().body(anon_const.body).value, indent + 1);
342+
match length {
343+
hir::ArrayLen::Infer(_, _) => println!("{}repeat count: _", ind),
344+
hir::ArrayLen::Body(anon_const) => {
345+
print_expr(cx, &cx.tcx.hir().body(anon_const.body).value, indent + 1)
346+
}
347+
}
343348
},
344349
hir::ExprKind::Err => {
345350
println!("{}Err", ind);

src/tools/clippy/clippy_utils/src/hir_utils.rs

+27-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::HirIdMap;
88
use rustc_hir::{
99
BinOpKind, Block, BodyId, Expr, ExprField, ExprKind, FnRetTy, GenericArg, GenericArgs, Guard, HirId,
1010
InlineAsmOperand, Let, Lifetime, LifetimeName, ParamName, Pat, PatField, PatKind, Path, PathSegment, QPath, Stmt,
11-
StmtKind, Ty, TyKind, TypeBinding,
11+
StmtKind, Ty, TyKind, TypeBinding, ArrayLen
1212
};
1313
use rustc_lexer::{tokenize, TokenKind};
1414
use rustc_lint::LateContext;
@@ -170,6 +170,14 @@ impl HirEqInterExpr<'_, '_, '_> {
170170
}
171171
}
172172

173+
pub fn eq_array_length(&mut self, left: ArrayLen, right: ArrayLen) -> bool {
174+
match (left, right) {
175+
(ArrayLen::Infer(..), ArrayLen::Infer(..)) => true,
176+
(ArrayLen::Body(l_ct), ArrayLen::Body(r_ct)) => self.eq_body(l_ct.body, r_ct.body),
177+
(_, _) => false,
178+
}
179+
}
180+
173181
pub fn eq_body(&mut self, left: BodyId, right: BodyId) -> bool {
174182
let cx = self.inner.cx;
175183
let eval_const = |body| constant_context(cx, cx.tcx.typeck_body(body)).expr(&cx.tcx.hir().body(body).value);
@@ -194,8 +202,8 @@ impl HirEqInterExpr<'_, '_, '_> {
194202
}
195203

196204
let is_eq = match (
197-
&reduce_exprkind(self.inner.cx, &left.kind),
198-
&reduce_exprkind(self.inner.cx, &right.kind),
205+
reduce_exprkind(self.inner.cx, &left.kind),
206+
reduce_exprkind(self.inner.cx, &right.kind),
199207
) {
200208
(&ExprKind::AddrOf(lb, l_mut, le), &ExprKind::AddrOf(rb, r_mut, re)) => {
201209
lb == rb && l_mut == r_mut && self.eq_expr(le, re)
@@ -232,7 +240,7 @@ impl HirEqInterExpr<'_, '_, '_> {
232240
},
233241
(&ExprKind::Index(la, li), &ExprKind::Index(ra, ri)) => self.eq_expr(la, ra) && self.eq_expr(li, ri),
234242
(&ExprKind::If(lc, lt, ref le), &ExprKind::If(rc, rt, ref re)) => {
235-
self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r))
243+
self.eq_expr(lc, rc) && self.eq_expr(lt, rt) && both(le, re, |l, r| self.eq_expr(l, r))
236244
},
237245
(&ExprKind::Let(l), &ExprKind::Let(r)) => {
238246
self.eq_pat(l.pat, r.pat) && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && self.eq_expr(l.init, r.init)
@@ -253,8 +261,8 @@ impl HirEqInterExpr<'_, '_, '_> {
253261
(&ExprKind::MethodCall(l_path, _, l_args, _), &ExprKind::MethodCall(r_path, _, r_args, _)) => {
254262
self.inner.allow_side_effects && self.eq_path_segment(l_path, r_path) && self.eq_exprs(l_args, r_args)
255263
},
256-
(&ExprKind::Repeat(le, ref ll_id), &ExprKind::Repeat(re, ref rl_id)) => {
257-
self.eq_expr(le, re) && self.eq_body(ll_id.body, rl_id.body)
264+
(&ExprKind::Repeat(le, ll), &ExprKind::Repeat(re, rl)) => {
265+
self.eq_expr(le, re) && self.eq_array_length(ll, rl)
258266
},
259267
(&ExprKind::Ret(ref l), &ExprKind::Ret(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)),
260268
(&ExprKind::Path(ref l), &ExprKind::Path(ref r)) => self.eq_qpath(l, r),
@@ -391,8 +399,8 @@ impl HirEqInterExpr<'_, '_, '_> {
391399
fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
392400
match (&left.kind, &right.kind) {
393401
(&TyKind::Slice(l_vec), &TyKind::Slice(r_vec)) => self.eq_ty(l_vec, r_vec),
394-
(&TyKind::Array(lt, ref ll_id), &TyKind::Array(rt, ref rl_id)) => {
395-
self.eq_ty(lt, rt) && self.eq_body(ll_id.body, rl_id.body)
402+
(&TyKind::Array(lt, ll), &TyKind::Array(rt, rl)) => {
403+
self.eq_ty(lt, rt) && self.eq_array_length(ll, rl)
396404
},
397405
(&TyKind::Ptr(ref l_mut), &TyKind::Ptr(ref r_mut)) => {
398406
l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty)
@@ -714,9 +722,9 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
714722
ExprKind::ConstBlock(ref l_id) => {
715723
self.hash_body(l_id.body);
716724
},
717-
ExprKind::Repeat(e, ref l_id) => {
725+
ExprKind::Repeat(e, len) => {
718726
self.hash_expr(e);
719-
self.hash_body(l_id.body);
727+
self.hash_array_length(len);
720728
},
721729
ExprKind::Ret(ref e) => {
722730
if let Some(e) = *e {
@@ -906,9 +914,9 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
906914
TyKind::Slice(ty) => {
907915
self.hash_ty(ty);
908916
},
909-
TyKind::Array(ty, anon_const) => {
917+
&TyKind::Array(ty, len) => {
910918
self.hash_ty(ty);
911-
self.hash_body(anon_const.body);
919+
self.hash_array_length(len);
912920
},
913921
TyKind::Ptr(ref mut_ty) => {
914922
self.hash_ty(mut_ty.ty);
@@ -953,6 +961,13 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
953961
}
954962
}
955963

964+
pub fn hash_array_length(&mut self, length: ArrayLen) {
965+
match length {
966+
ArrayLen::Infer(..) => {}
967+
ArrayLen::Body(anon_const) => self.hash_body(anon_const.body),
968+
}
969+
}
970+
956971
pub fn hash_body(&mut self, body_id: BodyId) {
957972
// swap out TypeckResults when hashing a body
958973
let old_maybe_typeck_results = self.maybe_typeck_results.replace(self.cx.tcx.typeck_body(body_id));

src/tools/clippy/clippy_utils/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ use rustc_hir::{
7575
def, Arm, BindingAnnotation, Block, BlockCheckMode, Body, Constness, Destination, Expr, ExprKind, FnDecl,
7676
ForeignItem, GenericArgs, HirId, Impl, ImplItem, ImplItemKind, IsAsync, Item, ItemKind, LangItem, Local,
7777
MatchSource, Mutability, Node, Param, Pat, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, TraitItem,
78-
TraitItemKind, TraitRef, TyKind, UnOp,
78+
TraitItemKind, TraitRef, TyKind, UnOp, ArrayLen
7979
};
8080
use rustc_lint::{LateContext, Level, Lint, LintContext};
8181
use rustc_middle::hir::exports::Export;
@@ -675,8 +675,9 @@ pub fn is_default_equivalent(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
675675
_ => false,
676676
},
677677
ExprKind::Tup(items) | ExprKind::Array(items) => items.iter().all(|x| is_default_equivalent(cx, x)),
678-
ExprKind::Repeat(x, y) => if_chain! {
679-
if let ExprKind::Lit(ref const_lit) = cx.tcx.hir().body(y.body).value.kind;
678+
ExprKind::Repeat(x, len) => if_chain! {
679+
if let ArrayLen::Body(len) = len;
680+
if let ExprKind::Lit(ref const_lit) = cx.tcx.hir().body(len.body).value.kind;
680681
if let LitKind::Int(v, _) = const_lit.node;
681682
if v <= 32 && is_default_equivalent(cx, x);
682683
then {

src/tools/clippy/tests/ui/author/repeat.stdout

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ if_chain! {
22
if let ExprKind::Repeat(value, length) = expr.kind;
33
if let ExprKind::Lit(ref lit) = value.kind;
44
if let LitKind::Int(1, LitIntType::Unsigned(UintTy::U8)) = lit.node;
5-
let expr1 = &cx.tcx.hir().body(length.body).value;
5+
if let ArrayLen::Body(anon_const) = length;
6+
let expr1 = &cx.tcx.hir().body(anon_const.body).value;
67
if let ExprKind::Lit(ref lit1) = expr1.kind;
78
if let LitKind::Int(5, LitIntType::Unsuffixed) = lit1.node;
89
then {

0 commit comments

Comments
 (0)