-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Initial impl of repr_packed_without_abi
#13398
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use rustc_ast::Attribute; | ||
use rustc_lint::LateContext; | ||
use rustc_span::{Span, sym}; | ||
|
||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::msrvs; | ||
|
||
use super::REPR_PACKED_WITHOUT_ABI; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute], msrv: &msrvs::Msrv) { | ||
if msrv.meets(msrvs::REPR_RUST) { | ||
check_packed(cx, item_span, attrs); | ||
} | ||
} | ||
|
||
fn check_packed(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute]) { | ||
if let Some(items) = attrs.iter().find_map(|attr| { | ||
if attr.ident().is_some_and(|ident| matches!(ident.name, sym::repr)) { | ||
attr.meta_item_list() | ||
} else { | ||
None | ||
} | ||
}) && let Some(packed) = items | ||
.iter() | ||
.find(|item| item.ident().is_some_and(|ident| matches!(ident.name, sym::packed))) | ||
&& !items.iter().any(|item| { | ||
item.ident() | ||
.is_some_and(|ident| matches!(ident.name, sym::C | sym::Rust)) | ||
}) | ||
{ | ||
span_lint_and_then( | ||
cx, | ||
REPR_PACKED_WITHOUT_ABI, | ||
item_span, | ||
"item uses `packed` representation without ABI-qualification", | ||
|diag| { | ||
diag.warn("unqualified `#[repr(packed)]` defaults to `#[repr(Rust, packed)]`, which has no stable ABI") | ||
.help("qualify the desired ABI explicity via `#[repr(C, packed)]` or `#[repr(Rust, packed)]`") | ||
.span_label(packed.span(), "`packed` representation set here"); | ||
}, | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#![deny(clippy::repr_packed_without_abi)] | ||
|
||
#[repr(packed)] | ||
struct NetworkPacketHeader { | ||
header_length: u8, | ||
header_version: u16, | ||
} | ||
|
||
#[repr(packed)] | ||
union Foo { | ||
a: u8, | ||
b: u16, | ||
} | ||
|
||
#[repr(C, packed)] | ||
struct NoLintCNetworkPacketHeader { | ||
header_length: u8, | ||
header_version: u16, | ||
} | ||
|
||
#[repr(Rust, packed)] | ||
struct NoLintRustNetworkPacketHeader { | ||
header_length: u8, | ||
header_version: u16, | ||
} | ||
|
||
#[repr(packed, C)] | ||
union NotLintCFoo { | ||
a: u8, | ||
b: u16, | ||
} | ||
|
||
#[repr(packed, Rust)] | ||
union NotLintRustFoo { | ||
a: u8, | ||
b: u16, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
error: item uses `packed` representation without ABI-qualification | ||
--> tests/ui/repr_packed_without_abi.rs:4:1 | ||
| | ||
LL | #[repr(packed)] | ||
| ------ `packed` representation set here | ||
LL | / struct NetworkPacketHeader { | ||
LL | | header_length: u8, | ||
LL | | header_version: u16, | ||
LL | | } | ||
| |_^ | ||
| | ||
= warning: unqualified `#[repr(packed)]` defaults to `#[repr(Rust, packed)]`, which has no stable ABI | ||
= help: qualify the desired ABI explicity via `#[repr(C, packed)]` or `#[repr(Rust, packed)]` | ||
note: the lint level is defined here | ||
--> tests/ui/repr_packed_without_abi.rs:1:9 | ||
| | ||
LL | #![deny(clippy::repr_packed_without_abi)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: item uses `packed` representation without ABI-qualification | ||
--> tests/ui/repr_packed_without_abi.rs:10:1 | ||
| | ||
LL | #[repr(packed)] | ||
| ------ `packed` representation set here | ||
LL | / union Foo { | ||
LL | | a: u8, | ||
LL | | b: u16, | ||
LL | | } | ||
| |_^ | ||
| | ||
= warning: unqualified `#[repr(packed)]` defaults to `#[repr(Rust, packed)]`, which has no stable ABI | ||
= help: qualify the desired ABI explicity via `#[repr(C, packed)]` or `#[repr(Rust, packed)]` | ||
|
||
error: aborting due to 2 previous errors | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#![warn(clippy::trailing_empty_array)] | ||
#![allow(clippy::repr_packed_without_abi)] | ||
|
||
// Do lint: | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the wait!
Before 1.74 you couldn't write
repr(Rust)
, adding an MSRV check is an option but I think leaving it out is reasonable too since you could just disable the lint if you wantrepr(Rust, packed)
and are targeting older versionsI'll leave that choice up to you, the rest looks great
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Force-pushed a rebase. I've added a msrv filter, although I'm not sure given the docs if
msrv_aliases!
is supposed to only name actual feature-names as used in rustc; as far as I can see,#[repr(Rust)]
didn't get a name, so I made something up. Bumped to 1.84. Had to rebase beyond 63d0ba9, and I'm unsure ifPostExpansionEarlyAttributes
is where this lint should live according to 63d0ba9's intent.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be fine to go in
Attributes
, the late passMSRV name is 👍, just has to be understandable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved it over