-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn on structs with a trailing zero-sized array that aren't repr(C) #2868
Comments
may also just be used in conjuction with manual allocation to make it easy to compute the offset of the array, but the result is the same; need repr(C). |
a leading array could also reasonably get a warning to use repr(align) or whatever instead. |
You can't use |
I'm willing to attempt to implement this if it is still relevant. |
Hi o/ — first-time contributor here. I'm interested in trying to fix this. I've been reading some of the Clippy PRs lately so I have some idea where to start but I'll probably be back with questions 😅. @rustbot claim |
Would this still apply if the zero-sized array was the only field in the struct? |
Also, would this have to be an early lint or a late lint? I'm going back and forth and chasing the types around is exhausting, but I feel like someone with more experience might just know. I guess specifically:
|
Hey @nhamovitz, regarding the implementation. The required information should all be available during both lint passes, it will probably be easier to use |
What ought to be the behavior when a #[repr(packed)]
struct ReprPacked {
field: i32,
last: [usize; 0],
}
#[repr(align(64))]
struct ReprAlign {
field: i32,
last: [usize; 0],
}
#[repr(packed, align(64))]
struct ReprPackedAlign {
field: i32,
last: [usize; 0],
}
#[repr(C, align(64))]
struct ReprCAlign {
field: i32,
last: [usize; 0],
} (For reference, There's |
I'd say any repr should suffice, most of them can be replicated in C |
Hi, another behavior question — what do you think about these? #![feature(const_generics_defaults)]
struct ConstParamZeroDefault<const N: usize = 0> {
last: [usize; N],
}
struct ConstParamNoDefault<const N: usize> {
last: [usize; N],
}
struct ConstParamNonZeroDefault<const N: usize = 1> {
last: [usize; N],
}
type A = ConstParamZeroDefault;
// etc
|
Nah none of these should be linted imo |
When linting struct definitions, is it convention to include any attributes on the definition in the pretty-printing? i.e.
or would we start at the |
OK I realized I could search for |
Correct! |
such a struct is likely being created to pass to C (zero sized arrays aren't very useful in Rust itself) since the same pattern is sometimes used in C code for headers of allocations
credit @gankro for the idea
The text was updated successfully, but these errors were encountered: