about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorJosh Driver <keeperofdakeys@gmail.com>2016-11-13 19:39:27 +1030
committerJosh Driver <keeperofdakeys@gmail.com>2016-11-14 22:22:17 +1030
commitd6689fdc616fd3e7af9946072cb0aaceb2bb26b9 (patch)
tree4a78e61ce34cd00ed57d5f4f55b9ad938003c058 /src/libsyntax
parenta277f9deb0bcccc096334447b0e57765110707c7 (diff)
downloadrust-d6689fdc616fd3e7af9946072cb0aaceb2bb26b9.tar.gz
rust-d6689fdc616fd3e7af9946072cb0aaceb2bb26b9.zip
Add warnings when the #[should_panic] attribute is invalid
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/test.rs44
1 files changed, 37 insertions, 7 deletions
diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs
index 618878c1f79..59a7e75d125 100644
--- a/src/libsyntax/test.rs
+++ b/src/libsyntax/test.rs
@@ -132,7 +132,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
                         path: self.cx.path.clone(),
                         bench: is_bench_fn(&self.cx, &i),
                         ignore: is_ignored(&i),
-                        should_panic: should_panic(&i)
+                        should_panic: should_panic(&i, &self.cx)
                     };
                     self.cx.testfns.push(test);
                     self.tests.push(i.ident);
@@ -395,14 +395,44 @@ fn is_ignored(i: &ast::Item) -> bool {
     i.attrs.iter().any(|attr| attr.check_name("ignore"))
 }
 
-fn should_panic(i: &ast::Item) -> ShouldPanic {
+fn should_panic(i: &ast::Item, cx: &TestCtxt) -> ShouldPanic {
     match i.attrs.iter().find(|attr| attr.check_name("should_panic")) {
         Some(attr) => {
-            let msg = attr.meta_item_list()
-                .and_then(|list| list.iter().find(|mi| mi.check_name("expected")))
-                .and_then(|li| li.meta_item())
-                .and_then(|mi| mi.value_str());
-            ShouldPanic::Yes(msg)
+            let sd = cx.span_diagnostic;
+            if attr.is_value_str() {
+                sd.struct_span_warn(
+                    attr.span(),
+                    "attribute must be of the form: \
+                     `#[should_panic]` or \
+                     `#[should_panic(expected = \"error message\")]`"
+                ).note("Errors in this attribute were erroneously allowed \
+                        and will become a hard error in a future release.")
+                .emit();
+                return ShouldPanic::Yes(None);
+            }
+            match attr.meta_item_list() {
+                // Handle #[should_panic]
+                None => ShouldPanic::Yes(None),
+                // Handle #[should_panic(expected = "foo")]
+                Some(list) => {
+                    let msg = list.iter()
+                        .find(|mi| mi.check_name("expected"))
+                        .and_then(|mi| mi.meta_item())
+                        .and_then(|mi| mi.value_str());
+                    if list.len() != 1 || msg.is_none() {
+                        sd.struct_span_warn(
+                            attr.span(),
+                            "argument must be of the form: \
+                             `expected = \"error message\"`"
+                        ).note("Errors in this attribute were erroneously \
+                                allowed and will become a hard error in a \
+                                future release.").emit();
+                        ShouldPanic::Yes(None)
+                    } else {
+                        ShouldPanic::Yes(msg)
+                    }
+                },
+            }
         }
         None => ShouldPanic::No,
     }