@@ -687,6 +687,18 @@ impl EarlyLintPass for BadRepr {
687
687
fn check_attribute ( & mut self , cx : & EarlyContext , attr : & ast:: Attribute ) {
688
688
if attr. name ( ) == "repr" {
689
689
let list = attr. meta_item_list ( ) ;
690
+ let outer = match attr. style {
691
+ ast:: AttrStyle :: Outer => true ,
692
+ ast:: AttrStyle :: Inner => false ,
693
+ } ;
694
+
695
+ let repr_str = move |lit : & str | {
696
+ if outer {
697
+ format ! ( "#[repr({})]" , lit)
698
+ } else {
699
+ format ! ( "#![repr({})]" , lit)
700
+ }
701
+ } ;
690
702
691
703
// Emit warnings with `repr` either has a literal assignment (`#[repr = "C"]`) or
692
704
// no hints (``#[repr]`)
@@ -695,33 +707,28 @@ impl EarlyLintPass for BadRepr {
695
707
let mut suggested = false ;
696
708
let mut warn = if let Some ( ref lit) = attr. value_str ( ) {
697
709
// avoid warning about empty `repr` on `#[repr = "foo"]`
698
- let sp = match format ! ( "{}" , lit) . as_ref ( ) {
710
+ let mut warn = cx. struct_span_lint (
711
+ BAD_REPR ,
712
+ attr. span ,
713
+ "`repr` attribute isn't configurable with a literal" ,
714
+ ) ;
715
+ match format ! ( "{}" , lit) . as_ref ( ) {
699
716
| "C" | "packed" | "rust" | "transparent"
700
717
| "u8" | "u16" | "u32" | "u64" | "u128" | "usize"
701
718
| "i8" | "i16" | "i32" | "i64" | "i128" | "isize" => {
702
- let lo = attr. span . lo ( ) + BytePos ( 2 ) ;
703
- let hi = attr. span . hi ( ) - BytePos ( 1 ) ;
719
+ // if the literal could have been a valid `repr` arg,
720
+ // suggest the correct syntax
721
+ warn. span_suggestion (
722
+ attr. span ,
723
+ "give `repr` a hint" ,
724
+ repr_str ( & lit. as_str ( ) ) ,
725
+ ) ;
704
726
suggested = true ;
705
- attr. span . with_lo ( lo) . with_hi ( hi)
706
727
}
707
- _ => attr. span , // the literal wasn't a valid `repr` arg
728
+ _ => { // the literal wasn't a valid `repr` arg
729
+ warn. span_label ( attr. span , "needs a hint" ) ;
730
+ }
708
731
} ;
709
- let mut warn = cx. struct_span_lint (
710
- BAD_REPR ,
711
- sp,
712
- "`repr` attribute isn't configurable with a literal" ,
713
- ) ;
714
- if suggested {
715
- // if the literal could have been a valid `repr` arg,
716
- // suggest the correct syntax
717
- warn. span_suggestion (
718
- sp,
719
- "give `repr` a hint" ,
720
- format ! ( "repr({})" , lit) ,
721
- ) ;
722
- } else {
723
- warn. span_label ( attr. span , "needs a hint" ) ;
724
- }
725
732
warn
726
733
} else {
727
734
let mut warn = cx. struct_span_lint (
@@ -733,8 +740,13 @@ impl EarlyLintPass for BadRepr {
733
740
warn
734
741
} ;
735
742
if !suggested {
736
- warn. help ( "valid hints include `#[repr(C)]`, `#[repr(packed)]`, \
737
- `#[repr(rust)]` and `#[repr(transparent)]`") ;
743
+ warn. help ( & format ! (
744
+ "valid hints include `{}`, `{}`, `{}` and `{}`" ,
745
+ repr_str( "C" ) ,
746
+ repr_str( "packed" ) ,
747
+ repr_str( "rust" ) ,
748
+ repr_str( "transparent" ) ,
749
+ ) ) ;
738
750
warn. note ( "for more information, visit \
739
751
<https://doc.rust-lang.org/reference/type-layout.html>") ;
740
752
}
0 commit comments