1
+ use rustc_attr_parsing:: { find_attr, AttributeKind , ReprAttr } ;
1
2
use rustc_hir:: Attribute ;
2
3
use rustc_lint:: LateContext ;
3
- use rustc_span:: { Span , sym } ;
4
+ use rustc_span:: Span ;
4
5
5
6
use clippy_utils:: diagnostics:: span_lint_and_then;
6
7
use clippy_utils:: msrvs;
@@ -14,30 +15,21 @@ pub(super) fn check(cx: &LateContext<'_>, item_span: Span, attrs: &[Attribute],
14
15
}
15
16
16
17
fn check_packed ( cx : & LateContext < ' _ > , item_span : Span , attrs : & [ Attribute ] ) {
17
- if let Some ( items) = attrs. iter ( ) . find_map ( |attr| {
18
- if attr. ident ( ) . is_some_and ( |ident| matches ! ( ident. name, sym:: repr) ) {
19
- attr. meta_item_list ( )
20
- } else {
21
- None
18
+ if let Some ( reprs) = find_attr ! ( attrs, AttributeKind :: Repr ( r) => r) {
19
+ let packed_span = reprs. iter ( ) . find ( |( r, _) | matches ! ( r, ReprAttr :: ReprPacked ( ..) ) ) . map ( |( _, s) | * s) ;
20
+
21
+ if let Some ( packed_span) = packed_span && !reprs. iter ( ) . any ( |( x, _) | * x == ReprAttr :: ReprC || * x == ReprAttr :: ReprRust ) {
22
+ span_lint_and_then (
23
+ cx,
24
+ REPR_PACKED_WITHOUT_ABI ,
25
+ item_span,
26
+ "item uses `packed` representation without ABI-qualification" ,
27
+ |diag| {
28
+ diag. warn ( "unqualified `#[repr(packed)]` defaults to `#[repr(Rust, packed)]`, which has no stable ABI" )
29
+ . help ( "qualify the desired ABI explicity via `#[repr(C, packed)]` or `#[repr(Rust, packed)]`" )
30
+ . span_label ( packed_span, "`packed` representation set here" ) ;
31
+ } ,
32
+ ) ;
22
33
}
23
- } ) && let Some ( packed) = items
24
- . iter ( )
25
- . find ( |item| item. ident ( ) . is_some_and ( |ident| matches ! ( ident. name, sym:: packed) ) )
26
- && !items. iter ( ) . any ( |item| {
27
- item. ident ( )
28
- . is_some_and ( |ident| matches ! ( ident. name, sym:: C | sym:: Rust ) )
29
- } )
30
- {
31
- span_lint_and_then (
32
- cx,
33
- REPR_PACKED_WITHOUT_ABI ,
34
- item_span,
35
- "item uses `packed` representation without ABI-qualification" ,
36
- |diag| {
37
- diag. warn ( "unqualified `#[repr(packed)]` defaults to `#[repr(Rust, packed)]`, which has no stable ABI" )
38
- . help ( "qualify the desired ABI explicity via `#[repr(C, packed)]` or `#[repr(Rust, packed)]`" )
39
- . span_label ( packed. span ( ) , "`packed` representation set here" ) ;
40
- } ,
41
- ) ;
42
34
}
43
35
}
0 commit comments