-
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 of
repr_packed_without_abi
(#13398)
Fixes #13375 I've added the lint next to the other attribute-related ones. Not sure if this is the correct place, since while we are looking after the `packed`-attribute (there is nothing we can do about types defined elsewhere), we are more concerned about the type's representation set by the attribute (instead of "duplicate attributes" and such). The lint simply looks at the attributes themselves without concern for the item-kind, since items where `repr` is not allowed end up in a compile-error anyway. I'm somewhat concerned about the level of noise this lint would cause if/when it goes into stable, although it does _not_ come up in `lintcheck`. ``` changelog: [`repr_packed_without_abi`]: Initial implementation ```
- 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::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 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.