Skip to content

Commit c654cc5

Browse files
committed
One more test + final tidying
1 parent 6303d2d commit c654cc5

File tree

3 files changed

+53
-54
lines changed

3 files changed

+53
-54
lines changed

clippy_lints/src/trailing_zero_sized_array_without_repr.rs

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
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;
1+
use clippy_utils::diagnostics::span_lint_and_help;
62
use rustc_hir::{HirId, Item, ItemKind};
73
use rustc_lint::{LateContext, LateLintPass};
8-
use rustc_middle::ty::{Const, TyS};
4+
use rustc_middle::ty::Const;
95
use rustc_session::{declare_lint_pass, declare_tool_lint};
106
use rustc_span::sym;
117

@@ -40,54 +36,39 @@ declare_lint_pass!(TrailingZeroSizedArrayWithoutRepr => [TRAILING_ZERO_SIZED_ARR
4036

4137
impl<'tcx> LateLintPass<'tcx> for TrailingZeroSizedArrayWithoutRepr {
4238
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
43-
dbg!(item.ident);
4439
if is_struct_with_trailing_zero_sized_array(cx, item) && !has_repr_attr(cx, item.hir_id()) {
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-
5840
span_lint_and_help(
5941
cx,
6042
TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR,
6143
item.span,
6244
"trailing zero-sized array in a struct which is not marked with a `repr` attribute",
6345
None,
64-
&help_msg,
46+
&format!(
47+
"consider annotating `{}` with `#[repr(C)]` or another `repr` attribute",
48+
cx.tcx.def_path_str(item.def_id.to_def_id())
49+
),
6550
);
6651
}
6752
}
6853
}
6954

7055
fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) -> bool {
71-
// TODO: when finalized, replace with an `if_chain`. I have it like this because my rust-analyzer
72-
// doesn't work when it's an `if_chain`.
56+
if_chain! {
57+
// First check if last field is an array
58+
if let ItemKind::Struct(data, _) = &item.kind;
59+
if let Some(last_field) = data.fields().last();
60+
if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind;
7361

74-
// First check if last field is an array
75-
if let ItemKind::Struct(data, _) = &item.kind {
76-
if let Some(last_field) = data.fields().last() {
77-
if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind {
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)
83-
} else {
84-
false
85-
}
62+
// Then check if that that array zero-sized
63+
let length_ldid = cx.tcx.hir().local_def_id(length.hir_id);
64+
let length = Const::from_anon_const(cx.tcx, length_ldid);
65+
let length = length.try_eval_usize(cx.tcx, cx.param_env);
66+
if let Some(length) = length;
67+
then {
68+
length == 0
8669
} else {
8770
false
8871
}
89-
} else {
90-
false
9172
}
9273
}
9374

tests/ui/trailing_zero_sized_array_without_repr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ struct ZeroSizedWithConstFunction {
4646
last: [usize; compute_zero()],
4747
}
4848

49+
const fn compute_zero_from_arg(x: usize) -> usize {
50+
x - 1
51+
}
52+
struct ZeroSizedWithConstFunction2 {
53+
field: i32,
54+
last: [usize; compute_zero_from_arg(1)],
55+
}
56+
4957
struct ZeroSizedArrayWrapper([usize; 0]);
5058

5159
struct TupleStruct(i32, [usize; 0]);

tests/ui/trailing_zero_sized_array_without_repr.stderr

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | | }
88
| |_^
99
|
1010
= note: `-D clippy::trailing-zero-sized-array-without-repr` implied by `-D warnings`
11-
= help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
11+
= help: consider annotating `RarelyUseful` with `#[repr(C)]` or another `repr` attribute
1212

1313
error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
1414
--> $DIR/trailing_zero_sized_array_without_repr.rs:11:1
@@ -18,7 +18,7 @@ LL | | first_and_last: [usize; 0],
1818
LL | | }
1919
| |_^
2020
|
21-
= help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
21+
= help: consider annotating `OnlyField` with `#[repr(C)]` or another `repr` attribute
2222

2323
error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
2424
--> $DIR/trailing_zero_sized_array_without_repr.rs:15:1
@@ -29,19 +29,18 @@ LL | | last: [T; 0],
2929
LL | | }
3030
| |_^
3131
|
32-
= help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
32+
= help: consider annotating `GenericArrayType` with `#[repr(C)]` or another `repr` attribute
3333

3434
error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
35-
--> $DIR/trailing_zero_sized_array_without_repr.rs:20:1
35+
--> $DIR/trailing_zero_sized_array_without_repr.rs:21:1
3636
|
37-
LL | / #[must_use]
38-
LL | | struct OnlyAnotherAttribute {
37+
LL | / struct OnlyAnotherAttribute {
3938
LL | | field: i32,
4039
LL | | last: [usize; 0],
4140
LL | | }
4241
| |_^
4342
|
44-
= help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
43+
= help: consider annotating `OnlyAnotherAttribute` with `#[repr(C)]` or another `repr` attribute
4544

4645
error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
4746
--> $DIR/trailing_zero_sized_array_without_repr.rs:29:1
@@ -52,7 +51,7 @@ LL | | last: [usize; 0],
5251
LL | | }
5352
| |_^
5453
|
55-
= help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
54+
= help: consider annotating `OnlyADeriveAttribute` with `#[repr(C)]` or another `repr` attribute
5655

5756
error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
5857
--> $DIR/trailing_zero_sized_array_without_repr.rs:35:1
@@ -63,7 +62,7 @@ LL | | last: [usize; ZERO],
6362
LL | | }
6463
| |_^
6564
|
66-
= help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
65+
= help: consider annotating `ZeroSizedWithConst` with `#[repr(C)]` or another `repr` attribute
6766

6867
error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
6968
--> $DIR/trailing_zero_sized_array_without_repr.rs:44:1
@@ -74,26 +73,37 @@ LL | | last: [usize; compute_zero()],
7473
LL | | }
7574
| |_^
7675
|
77-
= help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
76+
= help: consider annotating `ZeroSizedWithConstFunction` with `#[repr(C)]` or another `repr` attribute
7877

7978
error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
80-
--> $DIR/trailing_zero_sized_array_without_repr.rs:49:1
79+
--> $DIR/trailing_zero_sized_array_without_repr.rs:52:1
80+
|
81+
LL | / struct ZeroSizedWithConstFunction2 {
82+
LL | | field: i32,
83+
LL | | last: [usize; compute_zero_from_arg(1)],
84+
LL | | }
85+
| |_^
86+
|
87+
= help: consider annotating `ZeroSizedWithConstFunction2` with `#[repr(C)]` or another `repr` attribute
88+
89+
error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
90+
--> $DIR/trailing_zero_sized_array_without_repr.rs:57:1
8191
|
8292
LL | struct ZeroSizedArrayWrapper([usize; 0]);
8393
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8494
|
85-
= help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
95+
= help: consider annotating `ZeroSizedArrayWrapper` with `#[repr(C)]` or another `repr` attribute
8696

8797
error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
88-
--> $DIR/trailing_zero_sized_array_without_repr.rs:51:1
98+
--> $DIR/trailing_zero_sized_array_without_repr.rs:59:1
8999
|
90100
LL | struct TupleStruct(i32, [usize; 0]);
91101
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92102
|
93-
= help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
103+
= help: consider annotating `TupleStruct` with `#[repr(C)]` or another `repr` attribute
94104

95105
error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
96-
--> $DIR/trailing_zero_sized_array_without_repr.rs:53:1
106+
--> $DIR/trailing_zero_sized_array_without_repr.rs:61:1
97107
|
98108
LL | / struct LotsOfFields {
99109
LL | | f1: u32,
@@ -104,7 +114,7 @@ LL | | last: [usize; 0],
104114
LL | | }
105115
| |_^
106116
|
107-
= help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
117+
= help: consider annotating `LotsOfFields` with `#[repr(C)]` or another `repr` attribute
108118

109-
error: aborting due to 10 previous errors
119+
error: aborting due to 11 previous errors
110120

0 commit comments

Comments
 (0)