Skip to content
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 but no repr attribute #7838

Merged
merged 37 commits into from
Oct 20, 2021

Conversation

nhamovitz
Copy link
Contributor

@nhamovitz nhamovitz commented Oct 19, 2021

Closes #2868

changelog: Implement [`trailing_empty_array`], which warns if a struct is defined where the last field is a zero-sized array but there are no repr attributes. 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 some other situation where control over memory layout matters. Either way, a repr attribute is needed.

@rust-highfive
Copy link

r? @camsteffen

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 19, 2021
}

fn has_repr_attr(cx: &LateContext<'tcx>, hir_id: HirId) -> bool {
cx.tcx.hir().attrs(hir_id).iter().any(|attr| attr.has_name(sym::repr))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's at least four other ways to do this but I liked this one the best. (All five agreed on all testcases.) Happy to use another; they're in the commit history if you want to look (or I can go find them).

@nhamovitz
Copy link
Contributor Author

nhamovitz commented Oct 19, 2021

Also want to note that a previous method I use using to detect if the array is zero-sized led to an ICE. I tried for a bit to figure it out but tbh it was above my paygrade, but I figured I should let y'all know bc there's nothing stopping that code from being written. I made a minimal-ish repro and put it on a separate branch in my fork here: https://github.com/nhamovitz/rust-clippy/tree/demonstrates_ice. (Specifically these lines: https://github.com/nhamovitz/rust-clippy/blob/d85f903c91d909534003ee2ff0e16316b20687dc/clippy_lints/src/trailing_zero_sized_array_without_repr.rs#L61-L69). Happy to open a new issue for this (might be a rustc issue, actually); just lmk.

@nhamovitz
Copy link
Contributor Author

Also I guess r? @Manishearth because they've been helping me with this so far :)

Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks pretty good, unsure abotu the name

CHANGELOG.md Outdated
@@ -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`]: https://rust-lang.github.io/rust-clippy/master/index.html#trailing_zero_sized_array_without_repr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, might need a better name (@camsteffen @xFrednet thoughts?)

maybe repr_rust_trailing_empty_array? still kinda long

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, I can't think of a better name, we might be able to just call it trailing_empty_array but that would leave out the condition of having no repr attribute 🤔

Btw. nice commit messages @nhamovitz it's funny to see the full range of emotions that you went, though xD. Welcome to the project ^^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing_empty_array seems fine imo actually

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Changed to trailing_empty_array! (and updated the changelog in the initial PR message)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing_empty_array_no_repr or trailing_zst_no_repr

@Manishearth
Copy link
Member

@bors r+

thanks!

@bors
Copy link
Contributor

bors commented Oct 20, 2021

📌 Commit 0f9f591 has been approved by Manishearth

@bors
Copy link
Contributor

bors commented Oct 20, 2021

⌛ Testing commit 0f9f591 with merge 300b821...

@bors
Copy link
Contributor

bors commented Oct 20, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Manishearth
Pushing 300b821 to master...

@bors bors merged commit 300b821 into rust-lang:master Oct 20, 2021
@nhamovitz nhamovitz deleted the trailing_zs_arr_wo_repr branch October 22, 2021 07:19
@CAD97
Copy link
Contributor

CAD97 commented Oct 29, 2021

Zero-sized arrays aren't very useful in Rust itself,

Using a zero-sized [T; 0] field is the common way to say that a struct should be aligned at least as much as T. This is useful even without controlling anything more about the struct layout.

Typically I've seen this as the first field, so I don't know how much this lint will FP on this convention, but it's a definite possibility. (Personally I always tend to put "phantom fields" at the end, so I'd hit an FP here.)

Personally, I'd say this definitely shouldn't lint if the ZST is the only field, and probably should only lint with a literal zero.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn on structs with a trailing zero-sized array that aren't repr(C)
7 participants