Skip to content

Commit 5fdf934

Browse files
committed
intermediate step
1 parent 003972f commit 5fdf934

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

clippy_lints/src/trailing_zero_sized_array_without_repr_c.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use clippy_utils::consts::{miri_to_const, Constant};
2-
use clippy_utils::diagnostics::span_lint_and_sugg;
3-
use clippy_utils::source::snippet;
2+
use clippy_utils::diagnostics::span_lint_and_help;
43
use rustc_ast::Attribute;
5-
use rustc_errors::Applicability;
64
use rustc_hir::def_id::LocalDefId;
75
use rustc_hir::{Item, ItemKind, VariantData};
86
use rustc_lint::{LateContext, LateLintPass};
@@ -47,15 +45,15 @@ impl<'tcx> LateLintPass<'tcx> for TrailingZeroSizedArrayWithoutReprC {
4745
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
4846
dbg!(item.ident);
4947
if is_struct_with_trailing_zero_sized_array(cx, item) && !has_repr_attr(cx, item.def_id) {
50-
span_lint_and_sugg(
51-
cx,
52-
TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C,
53-
item.span,
54-
"trailing zero-sized array in a struct which is not marked `#[repr(C)]`",
55-
"try annotating the struct definition with `#[repr(C)]` (or another `repr` attribute):",
56-
format!("#[repr(C)]\n{}", snippet(cx, item.span, "..")),
57-
Applicability::MaybeIncorrect,
58-
);
48+
// span_lint_and_help(
49+
// cx,
50+
// TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C,
51+
// item.span,
52+
// "trailing zero-sized array in a struct which is not marked `#[repr(C)]`",
53+
// None,
54+
// "consider annotating the struct definition with `#[repr(C)]` (or another `repr` attribute)",
55+
// );
56+
eprintln!("— consider yourself linted — 🦀")
5957
}
6058
}
6159
}
@@ -87,15 +85,16 @@ fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'tcx>, item: &'tcx
8785
fn has_repr_attr(cx: &LateContext<'tcx>, def_id: LocalDefId) -> bool {
8886
let attrs_get_attrs = get_attrs_get_attrs(cx, def_id);
8987
let attrs_hir_map = get_attrs_hir_map(cx, def_id);
90-
let b11 = dbg!(includes_repr_attr_using_sym(attrs_get_attrs));
91-
let b12 = dbg!(includes_repr_attr_using_sym(attrs_hir_map));
92-
let b21 = dbg!(includes_repr_attr_using_helper(cx, attrs_get_attrs));
93-
let b22 = dbg!(includes_repr_attr_using_helper(cx, attrs_hir_map));
94-
let b3 = dbg!(has_repr_attr_using_adt(cx, def_id));
95-
let all_same = b11 && b12 && b21 && b22 && b3;
88+
89+
let b11 = includes_repr_attr_using_sym(attrs_get_attrs);
90+
let b12 = includes_repr_attr_using_sym(attrs_hir_map);
91+
let b21 = includes_repr_attr_using_helper(cx, attrs_get_attrs);
92+
let b22 = includes_repr_attr_using_helper(cx, attrs_hir_map);
93+
let b3 = has_repr_attr_using_adt(cx, def_id);
94+
let all_same = (b11 && b12 && b21 && b22 && b3) || (!b11 && !b12 && !b21 && !b22 && !b3);
9695
dbg!(all_same);
9796

98-
b11
97+
b21
9998
}
10099

101100
fn get_attrs_get_attrs(cx: &LateContext<'tcx>, def_id: LocalDefId) -> &'tcx [Attribute] {
@@ -108,7 +107,9 @@ fn get_attrs_hir_map(cx: &LateContext<'tcx>, def_id: LocalDefId) -> &'tcx [Attri
108107
hir_map.attrs(hir_id)
109108
}
110109

111-
// Don't like this because it's so dependent on the current list of `repr` flags and it would have to be manually updated if that ever expanded. idk if there's any mechanism in `bitflag!` or elsewhere for requiring that sort of exhaustiveness
110+
// Don't like this because it's so dependent on the current list of `repr` flags and it would have
111+
// to be manually updated if that ever expanded. idk if there's any mechanism in `bitflag!` or
112+
// elsewhere for requiring that sort of exhaustiveness
112113
fn has_repr_attr_using_adt(cx: &LateContext<'tcx>, def_id: LocalDefId) -> bool {
113114
let ty = cx.tcx.type_of(def_id.to_def_id());
114115
if let ty_mod::Adt(adt, _) = ty.kind() {
@@ -129,5 +130,7 @@ fn includes_repr_attr_using_sym(attrs: &'tcx [Attribute]) -> bool {
129130
}
130131

131132
fn includes_repr_attr_using_helper(cx: &LateContext<'tcx>, attrs: &'tcx [Attribute]) -> bool {
132-
attrs.iter().any(|attr| !rustc_attr::find_repr_attrs(cx.tcx.sess(), attr).is_empty())
133+
attrs
134+
.iter()
135+
.any(|attr| !rustc_attr::find_repr_attrs(cx.tcx.sess(), attr).is_empty())
133136
}

tests/ui/trailing_zero_sized_array_without_repr_c.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![warn(clippy::trailing_zero_sized_array_without_repr_c)]
2-
// #![feature(const_generics_defaults)] // see below
2+
#![feature(const_generics_defaults)] // see below
33

44
// Do lint:
55

@@ -20,13 +20,13 @@ struct GenericArrayType<T> {
2020
#[derive(Debug)]
2121
struct PlayNiceWithOtherAttributesDerive {
2222
field: i32,
23-
last: [usize; 0]
23+
last: [usize; 0],
2424
}
2525

2626
#[must_use]
2727
struct PlayNiceWithOtherAttributesMustUse {
2828
field: i32,
29-
last: [usize; 0]
29+
last: [usize; 0],
3030
}
3131

3232
const ZERO: usize = 0;
@@ -72,7 +72,7 @@ struct GoodReason {
7272
last: [usize; 0],
7373
}
7474

75-
struct SizedArray {
75+
struct NonZeroSizedArray {
7676
field: i32,
7777
last: [usize; 1],
7878
}
@@ -114,8 +114,8 @@ enum DontLintAnonymousStructsFromDesuraging {
114114
C { x: u32, y: [u64; 0] },
115115
}
116116

117-
// NOTE: including these (along with the required feature) triggers an ICE. Should make sure the
118-
// const generics people are aware of that if they weren't already.
117+
// NOTE: including these (along with the required feature) triggers an ICE. Not sure why. Should
118+
// make sure the const generics people are aware of that if they weren't already.
119119

120120
// #[repr(C)]
121121
// struct ConstParamOk<const N: usize = 0> {
@@ -129,5 +129,5 @@ enum DontLintAnonymousStructsFromDesuraging {
129129
// }
130130

131131
fn main() {
132-
let _ = PlayNiceWithOtherAttributesMustUse {field: 0, last: []};
132+
let _ = PlayNiceWithOtherAttributesMustUse { field: 0, last: [] };
133133
}

0 commit comments

Comments
 (0)