Add boilerplate and basic tests

This commit is contained in:
Nathaniel Hamovitz 2021-10-14 20:08:38 -07:00
parent 389a74b31a
commit 797507c583
6 changed files with 85 additions and 0 deletions

View File

@ -3021,6 +3021,7 @@ Released 2018-09-13
[`too_many_arguments`]: https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
[`too_many_lines`]: https://rust-lang.github.io/rust-clippy/master/index.html#too_many_lines
[`toplevel_ref_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#toplevel_ref_arg
[`trailing_zero_sized_array_without_repr_c`]: https://rust-lang.github.io/rust-clippy/master/index.html#trailing_zero_sized_array_without_repr_c
[`trait_duplication_in_bounds`]: https://rust-lang.github.io/rust-clippy/master/index.html#trait_duplication_in_bounds
[`transmute_bytes_to_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_bytes_to_str
[`transmute_float_to_int`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_float_to_int

View File

@ -443,6 +443,7 @@ store.register_lints(&[
temporary_assignment::TEMPORARY_ASSIGNMENT,
to_digit_is_some::TO_DIGIT_IS_SOME,
to_string_in_display::TO_STRING_IN_DISPLAY,
trailing_zero_sized_array_without_repr_c::TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C,
trait_bounds::TRAIT_DUPLICATION_IN_BOUNDS,
trait_bounds::TYPE_REPETITION_IN_BOUNDS,
transmute::CROSSPOINTER_TRANSMUTE,

View File

@ -25,6 +25,7 @@ store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
LintId::of(regex::TRIVIAL_REGEX),
LintId::of(strings::STRING_LIT_AS_BYTES),
LintId::of(suspicious_operation_groupings::SUSPICIOUS_OPERATION_GROUPINGS),
LintId::of(trailing_zero_sized_array_without_repr_c::TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C),
LintId::of(transmute::USELESS_TRANSMUTE),
LintId::of(use_self::USE_SELF),
])

View File

@ -355,6 +355,7 @@ mod tabs_in_doc_comments;
mod temporary_assignment;
mod to_digit_is_some;
mod to_string_in_display;
mod trailing_zero_sized_array_without_repr_c;
mod trait_bounds;
mod transmute;
mod transmuting_null;

View File

@ -0,0 +1,58 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use rustc_hir::*;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
declare_clippy_lint! {
/// ### What it does
/// Displays a warning when a struct with a trailing zero-sized array is declared without the `repr(C)` attribute.
///
/// ### Why is this bad?
/// 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.
///
/// ### Example
/// ```rust
/// struct RarelyUseful {
/// some_field: usize,
/// last: [SomeType; 0],
/// }
/// ```
///
/// Use instead:
/// ```rust
/// #[repr(C)]
/// struct MakesSense {
/// some_field: usize,
/// last: [SomeType; 0],
/// }
/// ```
pub TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C,
nursery,
"struct with a trailing zero-sized array but without `repr(C)`"
}
declare_lint_pass!(TrailingZeroSizedArrayWithoutReprC => [TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C]);
impl LateLintPass<'_> for TrailingZeroSizedArrayWithoutReprC {
fn check_struct_def(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::VariantData<'tcx>) {}
fn check_struct_def_post(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::VariantData<'tcx>) {}
// https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/sty/enum.TyKind.html#variant.Array in latepass
// or https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/enum.TyKind.html#variant.Array in early pass
fn check_field_def(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::FieldDef<'tcx>) {}
fn check_attribute(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_ast::Attribute) {}
fn enter_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [rustc_ast::Attribute]) {}
fn exit_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [rustc_ast::Attribute]) {}
}
//
// TODO: Register the lint pass in `clippy_lints/src/lib.rs`,
// e.g. store.register_late_pass(||
// Box::new(trailing_zero_sized_array_without_repr_c::TrailingZeroSizedArrayWithoutReprC));
fn temp_alert() {
span_lint_and_sugg(cx, lint, sp, msg, help, sugg, applicability)
}

View File

@ -0,0 +1,23 @@
#![warn(clippy::trailing_zero_sized_array_without_repr_c)]
struct RarelyUseful {
field: i32,
last: [SomeType; 0],
}
#[repr(C)]
struct GoodReason {
field: i32,
last: [SomeType; 0],
}
struct OnlyFieldIsZeroSizeArray {
first_and_last: [SomeType; 0],
}
struct GenericArrayType<T> {
field: i32,
last: [T; 0],
}
fn main() {}