-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial impl
repr_packed_without_abi
Fixes #13375
- Loading branch information
Showing
13 changed files
with
187 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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::EarlyContext; | ||
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: &EarlyContext<'_>, item_span: Span, attrs: &[Attribute], msrv: &msrvs::Msrv) { | ||
if msrv.meets(msrvs::REPR_RUST) { | ||
check_packed(cx, item_span, attrs); | ||
} | ||
} | ||
|
||
fn check_packed(cx: &EarlyContext<'_>, 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 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 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 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 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 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 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 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 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 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.