Skip to content

Commit 6303d2d

Browse files
committed
Revert "!: this is the commit that demonstrates the ICE"
This reverts commit d85f903.
1 parent d85f903 commit 6303d2d

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

clippy_lints/src/trailing_zero_sized_array_without_repr.rs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
use clippy_utils::{consts::miri_to_const, consts::Constant, diagnostics::span_lint_and_help};
1+
use clippy_utils::{
2+
diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then},
3+
source::{indent_of, snippet},
4+
};
5+
use rustc_errors::Applicability;
26
use rustc_hir::{HirId, Item, ItemKind};
37
use rustc_lint::{LateContext, LateLintPass};
8+
use rustc_middle::ty::{Const, TyS};
49
use rustc_session::{declare_lint_pass, declare_tool_lint};
510
use rustc_span::sym;
611

@@ -37,15 +42,27 @@ impl<'tcx> LateLintPass<'tcx> for TrailingZeroSizedArrayWithoutRepr {
3742
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
3843
dbg!(item.ident);
3944
if is_struct_with_trailing_zero_sized_array(cx, item) && !has_repr_attr(cx, item.hir_id()) {
40-
// span_lint_and_help(
41-
// cx,
42-
// TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR,
43-
// item.span,
44-
// "trailing zero-sized array in a struct which is not marked with a `repr` attribute",
45-
// None,
46-
// "",
47-
// );
48-
eprintln!("consider yourself linted 😎");
45+
let help_msg = format!(
46+
"consider annotating {} with `#[repr(C)]` or another `repr` attribute",
47+
cx.tcx
48+
.type_of(item.def_id)
49+
.ty_adt_def()
50+
.map(|adt_def| cx.tcx.def_path_str(adt_def.did))
51+
.unwrap_or_else(
52+
// I don't think this will ever be the case, since we made it through
53+
// `is_struct_with_trailing_zero_sized_array`, but I don't feel comfortable putting an `unwrap`
54+
|| "the struct definition".to_string()
55+
)
56+
);
57+
58+
span_lint_and_help(
59+
cx,
60+
TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR,
61+
item.span,
62+
"trailing zero-sized array in a struct which is not marked with a `repr` attribute",
63+
None,
64+
&help_msg,
65+
);
4966
}
5067
}
5168
}
@@ -58,19 +75,11 @@ fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'tcx>, item: &'tcx
5875
if let ItemKind::Struct(data, _) = &item.kind {
5976
if let Some(last_field) = data.fields().last() {
6077
if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind {
61-
let length_did = cx.tcx.hir().body_owner_def_id(length.body).to_def_id();
62-
let ty = cx.tcx.type_of(length_did);
63-
let length = cx
64-
.tcx
65-
// ICE happens in `const_eval_poly` according to my backtrace
66-
.const_eval_poly(length_did)
67-
.ok()
68-
.map(|val| rustc_middle::ty::Const::from_value(cx.tcx, val, ty));
69-
if let Some(Constant::Int(length)) = length.and_then(miri_to_const){
70-
length == 0
71-
} else {
72-
false
73-
}
78+
// Then check if that that array zero-sized
79+
let length_ldid = cx.tcx.hir().local_def_id(length.hir_id);
80+
let length = Const::from_anon_const(cx.tcx, length_ldid);
81+
let length = length.try_eval_usize(cx.tcx, cx.param_env);
82+
length == Some(0)
7483
} else {
7584
false
7685
}

tests/ui/trailing_zero_sized_array_without_repr.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#![warn(clippy::trailing_zero_sized_array_without_repr)]
22
#![feature(const_generics_defaults)]
33

4-
// ICE note: All of these are fine
5-
64
// Do lint:
75

86
struct RarelyUseful {
@@ -25,6 +23,8 @@ struct OnlyAnotherAttribute {
2523
last: [usize; 0],
2624
}
2725

26+
// NOTE: Unfortunately the attribute isn't included in the lint output. I'm not sure how to make it
27+
// show up.
2828
#[derive(Debug)]
2929
struct OnlyADeriveAttribute {
3030
field: i32,
@@ -150,16 +150,12 @@ struct TupleStructReprC(i32, [usize; 0]);
150150

151151
type NamedTuple = (i32, [usize; 0]);
152152

153-
// ICE note: and then this one crashes
154-
155153
#[rustfmt::skip] // [rustfmt#4995](https://github.com/rust-lang/rustfmt/issues/4995)
156154
struct ConstParamZeroDefault<const N: usize = 0> {
157155
field: i32,
158156
last: [usize; N],
159157
}
160158

161-
// ICE notes: presumably these as well but I'm not sure
162-
163159
struct ConstParamNoDefault<const N: usize> {
164160
field: i32,
165161
last: [usize; N],

0 commit comments

Comments
 (0)