Skip to content

Commit 797507c

Browse files
committed
Add boilerplate and basic tests
1 parent 389a74b commit 797507c

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,6 +3021,7 @@ Released 2018-09-13
30213021
[`too_many_arguments`]: https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
30223022
[`too_many_lines`]: https://rust-lang.github.io/rust-clippy/master/index.html#too_many_lines
30233023
[`toplevel_ref_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#toplevel_ref_arg
3024+
[`trailing_zero_sized_array_without_repr_c`]: https://rust-lang.github.io/rust-clippy/master/index.html#trailing_zero_sized_array_without_repr_c
30243025
[`trait_duplication_in_bounds`]: https://rust-lang.github.io/rust-clippy/master/index.html#trait_duplication_in_bounds
30253026
[`transmute_bytes_to_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_bytes_to_str
30263027
[`transmute_float_to_int`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_float_to_int

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ store.register_lints(&[
443443
temporary_assignment::TEMPORARY_ASSIGNMENT,
444444
to_digit_is_some::TO_DIGIT_IS_SOME,
445445
to_string_in_display::TO_STRING_IN_DISPLAY,
446+
trailing_zero_sized_array_without_repr_c::TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C,
446447
trait_bounds::TRAIT_DUPLICATION_IN_BOUNDS,
447448
trait_bounds::TYPE_REPETITION_IN_BOUNDS,
448449
transmute::CROSSPOINTER_TRANSMUTE,

clippy_lints/src/lib.register_nursery.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
2525
LintId::of(regex::TRIVIAL_REGEX),
2626
LintId::of(strings::STRING_LIT_AS_BYTES),
2727
LintId::of(suspicious_operation_groupings::SUSPICIOUS_OPERATION_GROUPINGS),
28+
LintId::of(trailing_zero_sized_array_without_repr_c::TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C),
2829
LintId::of(transmute::USELESS_TRANSMUTE),
2930
LintId::of(use_self::USE_SELF),
3031
])

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ mod tabs_in_doc_comments;
355355
mod temporary_assignment;
356356
mod to_digit_is_some;
357357
mod to_string_in_display;
358+
mod trailing_zero_sized_array_without_repr_c;
358359
mod trait_bounds;
359360
mod transmute;
360361
mod transmuting_null;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use rustc_hir::*;
3+
use rustc_lint::{LateContext, LateLintPass};
4+
use rustc_session::{declare_lint_pass, declare_tool_lint};
5+
6+
declare_clippy_lint! {
7+
/// ### What it does
8+
/// Displays a warning when a struct with a trailing zero-sized array is declared without the `repr(C)` attribute.
9+
///
10+
/// ### Why is this bad?
11+
/// Zero-sized arrays aren't very useful in Rust itself, so such a struct is likely being created to pass to C code (or in conjuction with manual allocation to make it easy to compute the offset of the array). Either way, `#[repr(C)]` is needed.
12+
///
13+
/// ### Example
14+
/// ```rust
15+
/// struct RarelyUseful {
16+
/// some_field: usize,
17+
/// last: [SomeType; 0],
18+
/// }
19+
/// ```
20+
///
21+
/// Use instead:
22+
/// ```rust
23+
/// #[repr(C)]
24+
/// struct MakesSense {
25+
/// some_field: usize,
26+
/// last: [SomeType; 0],
27+
/// }
28+
/// ```
29+
pub TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C,
30+
nursery,
31+
"struct with a trailing zero-sized array but without `repr(C)`"
32+
}
33+
declare_lint_pass!(TrailingZeroSizedArrayWithoutReprC => [TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C]);
34+
35+
impl LateLintPass<'_> for TrailingZeroSizedArrayWithoutReprC {
36+
fn check_struct_def(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::VariantData<'tcx>) {}
37+
38+
fn check_struct_def_post(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::VariantData<'tcx>) {}
39+
// https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/sty/enum.TyKind.html#variant.Array in latepass
40+
// or https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/enum.TyKind.html#variant.Array in early pass
41+
42+
fn check_field_def(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::FieldDef<'tcx>) {}
43+
44+
fn check_attribute(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_ast::Attribute) {}
45+
46+
fn enter_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [rustc_ast::Attribute]) {}
47+
48+
fn exit_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [rustc_ast::Attribute]) {}
49+
}
50+
//
51+
// TODO: Register the lint pass in `clippy_lints/src/lib.rs`,
52+
// e.g. store.register_late_pass(||
53+
// Box::new(trailing_zero_sized_array_without_repr_c::TrailingZeroSizedArrayWithoutReprC));
54+
55+
56+
fn temp_alert() {
57+
span_lint_and_sugg(cx, lint, sp, msg, help, sugg, applicability)
58+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![warn(clippy::trailing_zero_sized_array_without_repr_c)]
2+
3+
struct RarelyUseful {
4+
field: i32,
5+
last: [SomeType; 0],
6+
}
7+
8+
#[repr(C)]
9+
struct GoodReason {
10+
field: i32,
11+
last: [SomeType; 0],
12+
}
13+
14+
struct OnlyFieldIsZeroSizeArray {
15+
first_and_last: [SomeType; 0],
16+
}
17+
18+
struct GenericArrayType<T> {
19+
field: i32,
20+
last: [T; 0],
21+
}
22+
23+
fn main() {}

0 commit comments

Comments
 (0)