@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
2
2
// use clippy_utils::is_integer_const;
3
3
use clippy_utils:: consts:: { miri_to_const, Constant } ;
4
4
use rustc_errors:: Applicability ;
5
- use rustc_hir:: { Item , ItemKind , TyKind , VariantData } ;
5
+ use rustc_hir:: { HirId , Item , ItemKind , TyKind , VariantData } ;
6
6
use rustc_lint:: { LateContext , LateLintPass } ;
7
7
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
8
8
use rustc_span:: sym;
@@ -45,17 +45,29 @@ declare_lint_pass!(TrailingZeroSizedArrayWithoutReprC => [TRAILING_ZERO_SIZED_AR
45
45
impl < ' tcx > LateLintPass < ' tcx > for TrailingZeroSizedArrayWithoutReprC {
46
46
fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' tcx > ) {
47
47
dbg ! ( item. ident) ;
48
- if is_struct_with_trailing_zero_sized_array ( cx, item) && !has_repr_c ( cx, item) {
49
- // span_lint_and_sugg(
50
- // cx,
51
- // todo!(),
52
- // todo!(),
53
- // todo!(),
54
- // "try",
55
- // "`#[repr(C)]`".to_string(),
56
- // Applicability::MachineApplicable,
57
- // );
58
- // println!("consider yourself linted 😎");
48
+
49
+ let hir_id = cx. tcx . hir ( ) . local_def_id_to_hir_id ( item. def_id ) ;
50
+ let hir_id2 = item. hir_id ( ) ;
51
+ dbg ! ( hir_id) ;
52
+ dbg ! ( hir_id2) ;
53
+ dbg ! ( hir_id == hir_id2) ;
54
+
55
+ let span1 = cx. tcx . hir ( ) . span ( hir_id) ;
56
+ let span2 = item. span ;
57
+ dbg ! ( span1) ;
58
+ dbg ! ( span2) ;
59
+ dbg ! ( span1 == span2) ;
60
+
61
+ if is_struct_with_trailing_zero_sized_array ( cx, item) && !has_repr_c ( cx, hir_id) {
62
+ span_lint_and_sugg (
63
+ cx,
64
+ TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C ,
65
+ span2,
66
+ "trailing zero-sized array in a struct which isn't marked `#[repr(C)]`" ,
67
+ "try" ,
68
+ "#[repr(C)]" . to_string ( ) ,
69
+ Applicability :: MaybeIncorrect ,
70
+ ) ;
59
71
}
60
72
}
61
73
}
@@ -76,7 +88,7 @@ fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'tcx>, item: &'tcx
76
88
. map ( |val| rustc_middle:: ty:: Const :: from_value ( cx. tcx , val, ty) ) ;
77
89
if let Some ( Constant :: Int ( val) ) = constant. and_then ( miri_to_const) {
78
90
if val == 0 {
79
- eprintln ! ( "trailing: true" ) ;
91
+ // eprintln!("trailing: true");
80
92
return true ;
81
93
}
82
94
}
@@ -85,28 +97,21 @@ fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'tcx>, item: &'tcx
85
97
}
86
98
}
87
99
// dbg!(aconst);
88
- eprintln ! ( "trailing: false" ) ;
100
+ // eprintln!("trailing: false");
89
101
false
90
102
}
91
103
92
- fn has_repr_c ( cx : & LateContext < ' tcx > , item : & ' tcx Item < ' tcx > ) -> bool {
93
- // let hir_id2 = if let Some(body) = cx.enclosing_body {
94
- // body.hir_id
95
- // } else {
96
- // todo!();
97
- // };
98
-
99
- let hir_id = cx. tcx . hir ( ) . local_def_id_to_hir_id ( item. def_id ) ;
104
+ fn has_repr_c ( cx : & LateContext < ' tcx > , hir_id : HirId ) -> bool {
100
105
let attrs = cx. tcx . hir ( ) . attrs ( hir_id) ;
101
106
// NOTE: Can there ever be more than one `repr` attribute?
102
107
// other `repr` syms: repr, repr128, repr_align, repr_align_enum, repr_no_niche, repr_packed,
103
108
// repr_simd, repr_transparent
104
109
105
110
if let Some ( repr_attr) = attrs. iter ( ) . find ( |attr| attr. has_name ( sym:: repr) ) {
106
- eprintln ! ( "repr: true" ) ;
111
+ // eprintln!("repr: true");
107
112
true
108
113
} else {
109
- eprintln ! ( "repr: false" ) ;
114
+ // eprintln!("repr: false");
110
115
false
111
116
}
112
117
}
0 commit comments