Skip to content

Commit 4c9d68e

Browse files
committed
Make Clippy understand generic const items
1 parent e97a770 commit 4c9d68e

File tree

6 files changed

+15
-7
lines changed

6 files changed

+15
-7
lines changed

clippy_lints/src/large_const_arrays.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ impl<'tcx> LateLintPass<'tcx> for LargeConstArrays {
5050
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
5151
if_chain! {
5252
if !item.span.from_expansion();
53-
if let ItemKind::Const(hir_ty, _) = &item.kind;
53+
if let ItemKind::Const(hir_ty, generics, _) = &item.kind;
54+
// Since static items may not have generics, skip generic const items.
55+
// FIXME(generic_const_items): I don't think checking `generics.hwcp` suffices as it
56+
// doesn't account for empty where-clauses that only consist of keyword `where` IINM.
57+
if generics.params.is_empty() && !generics.has_where_clause_predicates;
5458
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
5559
if let ty::Array(element_type, cst) = ty.kind();
5660
if let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind();

clippy_lints/src/non_copy_const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ declare_lint_pass!(NonCopyConst => [DECLARE_INTERIOR_MUTABLE_CONST, BORROW_INTER
302302

303303
impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
304304
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx Item<'_>) {
305-
if let ItemKind::Const(hir_ty, body_id) = it.kind {
305+
if let ItemKind::Const(hir_ty, _generics, body_id) = it.kind {
306306
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
307307
if !ignored_macro(cx, it) && is_unfrozen(cx, ty) && is_value_unfrozen_poly(cx, body_id, ty) {
308308
lint(cx, Source::Item { item: it.span });

clippy_lints/src/types/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
349349
let is_exported = cx.effective_visibilities.is_exported(item.owner_id.def_id);
350350

351351
match item.kind {
352-
ItemKind::Static(ty, _, _) | ItemKind::Const(ty, _) => self.check_ty(
352+
ItemKind::Static(ty, _, _) | ItemKind::Const(ty, _, _) => self.check_ty(
353353
cx,
354354
ty,
355355
CheckTyContext {

clippy_utils/src/ast_utils.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,17 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
301301
(
302302
Const(box ast::ConstItem {
303303
defaultness: ld,
304+
generics: lg,
304305
ty: lt,
305306
expr: le,
306307
}),
307308
Const(box ast::ConstItem {
308309
defaultness: rd,
310+
generics: rg,
309311
ty: rt,
310312
expr: re,
311313
}),
312-
) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
314+
) => eq_defaultness(*ld, *rd) && eq_generics(lg, rg) && eq_ty(lt, rt) && eq_expr_opt(le, re),
313315
(
314316
Fn(box ast::Fn {
315317
defaultness: ld,
@@ -476,15 +478,17 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
476478
(
477479
Const(box ast::ConstItem {
478480
defaultness: ld,
481+
generics: lg,
479482
ty: lt,
480483
expr: le,
481484
}),
482485
Const(box ast::ConstItem {
483486
defaultness: rd,
487+
generics: rg,
484488
ty: rt,
485489
expr: re,
486490
}),
487-
) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
491+
) => eq_defaultness(*ld, *rd) && eq_generics(lg, rg) && eq_ty(lt, rt) && eq_expr_opt(le, re),
488492
(
489493
Fn(box ast::Fn {
490494
defaultness: ld,

clippy_utils/src/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
461461
// Check if this constant is based on `cfg!(..)`,
462462
// which is NOT constant for our purposes.
463463
if let Some(node) = self.lcx.tcx.hir().get_if_local(def_id)
464-
&& let Node::Item(Item { kind: ItemKind::Const(_, body_id), .. }) = node
464+
&& let Node::Item(Item { kind: ItemKind::Const(.., body_id), .. }) = node
465465
&& let Node::Expr(Expr { kind: ExprKind::Lit(_), span, .. }) = self.lcx
466466
.tcx
467467
.hir()

clippy_utils/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2380,7 +2380,7 @@ fn with_test_item_names(tcx: TyCtxt<'_>, module: LocalDefId, f: impl Fn(&[Symbol
23802380
for id in tcx.hir().module_items(module) {
23812381
if matches!(tcx.def_kind(id.owner_id), DefKind::Const)
23822382
&& let item = tcx.hir().item(id)
2383-
&& let ItemKind::Const(ty, _body) = item.kind {
2383+
&& let ItemKind::Const(ty, _generics, _body) = item.kind {
23842384
if let TyKind::Path(QPath::Resolved(_, path)) = ty.kind {
23852385
// We could also check for the type name `test::TestDescAndFn`
23862386
if let Res::Def(DefKind::Struct, _) = path.res {

0 commit comments

Comments
 (0)