]> git.lizzy.rs Git - rust.git/commitdiff
Use consistent span for repr attr suggestion
authorEsteban Küber <esteban@kuber.com.ar>
Wed, 6 Jun 2018 22:48:08 +0000 (15:48 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Thu, 7 Jun 2018 00:36:28 +0000 (17:36 -0700)
src/librustc_lint/builtin.rs
src/test/compile-fail/issue-43988.rs
src/test/ui/feature-gate/issue-43106-gating-of-builtin-attrs.stderr
src/test/ui/suggestions/repr.stderr

index 5c05fdb5621a7d87fedbd452e584461aa5dc6328..d95e0d77d86d388a140e5491e215ec67219750df 100644 (file)
@@ -687,6 +687,18 @@ impl EarlyLintPass for BadRepr {
     fn check_attribute(&mut self, cx: &EarlyContext, attr: &ast::Attribute) {
         if attr.name() == "repr" {
             let list = attr.meta_item_list();
+            let outer = match attr.style {
+                ast::AttrStyle::Outer => true,
+                ast::AttrStyle::Inner => false,
+            };
+
+            let repr_str = move |lit: &str| {
+                if outer {
+                    format!("#[repr({})]", lit)
+                } else {
+                    format!("#![repr({})]", lit)
+                }
+            };
 
             // Emit warnings with `repr` either has a literal assignment (`#[repr = "C"]`) or
             // no hints (``#[repr]`)
@@ -695,33 +707,28 @@ fn check_attribute(&mut self, cx: &EarlyContext, attr: &ast::Attribute) {
                 let mut suggested = false;
                 let mut warn = if let Some(ref lit) = attr.value_str() {
                     // avoid warning about empty `repr` on `#[repr = "foo"]`
-                    let sp = match format!("{}", lit).as_ref() {
+                    let mut warn = cx.struct_span_lint(
+                        BAD_REPR,
+                        attr.span,
+                        "`repr` attribute isn't configurable with a literal",
+                    );
+                    match format!("{}", lit).as_ref() {
                         | "C" | "packed" | "rust" | "transparent"
                         | "u8" | "u16" | "u32" | "u64" | "u128" | "usize"
                         | "i8" | "i16" | "i32" | "i64" | "i128" | "isize" => {
-                            let lo = attr.span.lo() + BytePos(2);
-                            let hi = attr.span.hi() - BytePos(1);
+                            // if the literal could have been a valid `repr` arg,
+                            // suggest the correct syntax
+                            warn.span_suggestion(
+                                attr.span,
+                                "give `repr` a hint",
+                                repr_str(&lit.as_str()),
+                            );
                             suggested = true;
-                            attr.span.with_lo(lo).with_hi(hi)
                         }
-                        _ => attr.span,  // the literal wasn't a valid `repr` arg
+                        _ => {  // the literal wasn't a valid `repr` arg
+                            warn.span_label(attr.span, "needs a hint");
+                        }
                     };
-                    let mut warn = cx.struct_span_lint(
-                        BAD_REPR,
-                        sp,
-                        "`repr` attribute isn't configurable with a literal",
-                    );
-                    if suggested {
-                        // if the literal could have been a valid `repr` arg,
-                        // suggest the correct syntax
-                        warn.span_suggestion(
-                            sp,
-                            "give `repr` a hint",
-                            format!("repr({})", lit),
-                        );
-                    } else {
-                        warn.span_label(attr.span, "needs a hint");
-                    }
                     warn
                 } else {
                     let mut warn = cx.struct_span_lint(
@@ -733,8 +740,13 @@ fn check_attribute(&mut self, cx: &EarlyContext, attr: &ast::Attribute) {
                     warn
                 };
                 if !suggested {
-                    warn.help("valid hints include `#[repr(C)]`, `#[repr(packed)]`, \
-                               `#[repr(rust)]` and `#[repr(transparent)]`");
+                    warn.help(&format!(
+                        "valid hints include `{}`, `{}`, `{}` and `{}`",
+                        repr_str("C"),
+                        repr_str("packed"),
+                        repr_str("rust"),
+                        repr_str("transparent"),
+                    ));
                     warn.note("for more information, visit \
                                <https://doc.rust-lang.org/reference/type-layout.html>");
                 }
index 0dfa9f6f0d3417f7c196d6590ed705ee9992a1ef..6361af7648258b5b8762640f6f74046294a816e0 100644 (file)
@@ -34,6 +34,7 @@ fn main() {
     #[repr]
     let _y = "123";
     //~^^ ERROR attribute should not be applied to a statement
+    //~| WARN `repr` attribute must have a hint
 
 
     fn foo() {}
@@ -44,5 +45,5 @@ fn foo() {}
 
     let _z = #[repr] 1;
     //~^ ERROR attribute should not be applied to an expression
-
+    //~| WARN `repr` attribute must have a hint
 }
index f351a9e69d01135e018894a56a0a840c53e67a0a..3569eecd883f6e869d3c180de5a0813a3b58992c 100644 (file)
@@ -193,7 +193,7 @@ LL |     mod inner { #![repr="3900"] }
    |                 ^^^^^^^^^^^^^^^ needs a hint
    |
    = note: #[warn(bad_repr)] on by default
-   = help: valid hints include `#[repr(C)]`, `#[repr(packed)]`, `#[repr(rust)]` and `#[repr(transparent)]`
+   = help: valid hints include `#![repr(C)]`, `#![repr(packed)]`, `#![repr(rust)]` and `#![repr(transparent)]`
    = note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html>
 
 warning: `repr` attribute isn't configurable with a literal
@@ -238,7 +238,7 @@ warning: `repr` attribute isn't configurable with a literal
 LL | #![repr                       = "3900"]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ needs a hint
    |
-   = help: valid hints include `#[repr(C)]`, `#[repr(packed)]`, `#[repr(rust)]` and `#[repr(transparent)]`
+   = help: valid hints include `#![repr(C)]`, `#![repr(packed)]`, `#![repr(rust)]` and `#![repr(transparent)]`
    = note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html>
 
 warning: unused attribute
index 83f5bb48f7a0802473a7a5eae5ddc86a29d8b4dc..7a99d8c04488f2c38c8d3a79c1ea6dfdf00bf879 100644 (file)
@@ -18,8 +18,8 @@ LL | #[repr = "B"]
    = note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html>
 
 warning: `repr` attribute isn't configurable with a literal
-  --> $DIR/repr.rs:21:3
+  --> $DIR/repr.rs:21:1
    |
 LL | #[repr = "C"]
-   |   ^^^^^^^^^^ help: give `repr` a hint: `repr(C)`
+   | ^^^^^^^^^^^^^ help: give `repr` a hint: `#[repr(C)]`