Skip to content

Commit d6689fd

Browse files
Add warnings when the #[should_panic] attribute is invalid
1 parent a277f9d commit d6689fd

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

src/libsyntax/test.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
132132
path: self.cx.path.clone(),
133133
bench: is_bench_fn(&self.cx, &i),
134134
ignore: is_ignored(&i),
135-
should_panic: should_panic(&i)
135+
should_panic: should_panic(&i, &self.cx)
136136
};
137137
self.cx.testfns.push(test);
138138
self.tests.push(i.ident);
@@ -395,14 +395,44 @@ fn is_ignored(i: &ast::Item) -> bool {
395395
i.attrs.iter().any(|attr| attr.check_name("ignore"))
396396
}
397397

398-
fn should_panic(i: &ast::Item) -> ShouldPanic {
398+
fn should_panic(i: &ast::Item, cx: &TestCtxt) -> ShouldPanic {
399399
match i.attrs.iter().find(|attr| attr.check_name("should_panic")) {
400400
Some(attr) => {
401-
let msg = attr.meta_item_list()
402-
.and_then(|list| list.iter().find(|mi| mi.check_name("expected")))
403-
.and_then(|li| li.meta_item())
404-
.and_then(|mi| mi.value_str());
405-
ShouldPanic::Yes(msg)
401+
let sd = cx.span_diagnostic;
402+
if attr.is_value_str() {
403+
sd.struct_span_warn(
404+
attr.span(),
405+
"attribute must be of the form: \
406+
`#[should_panic]` or \
407+
`#[should_panic(expected = \"error message\")]`"
408+
).note("Errors in this attribute were erroneously allowed \
409+
and will become a hard error in a future release.")
410+
.emit();
411+
return ShouldPanic::Yes(None);
412+
}
413+
match attr.meta_item_list() {
414+
// Handle #[should_panic]
415+
None => ShouldPanic::Yes(None),
416+
// Handle #[should_panic(expected = "foo")]
417+
Some(list) => {
418+
let msg = list.iter()
419+
.find(|mi| mi.check_name("expected"))
420+
.and_then(|mi| mi.meta_item())
421+
.and_then(|mi| mi.value_str());
422+
if list.len() != 1 || msg.is_none() {
423+
sd.struct_span_warn(
424+
attr.span(),
425+
"argument must be of the form: \
426+
`expected = \"error message\"`"
427+
).note("Errors in this attribute were erroneously \
428+
allowed and will become a hard error in a \
429+
future release.").emit();
430+
ShouldPanic::Yes(None)
431+
} else {
432+
ShouldPanic::Yes(msg)
433+
}
434+
},
435+
}
406436
}
407437
None => ShouldPanic::No,
408438
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --test
12+
13+
#[test]
14+
#[should_panic = "foo"]
15+
//~^ WARN: attribute must be of the form:
16+
fn test1() {
17+
panic!();
18+
}
19+
20+
#[test]
21+
#[should_panic(expected)]
22+
//~^ WARN: argument must be of the form:
23+
fn test2() {
24+
panic!();
25+
}
26+
27+
#[test]
28+
#[should_panic(expect)]
29+
//~^ WARN: argument must be of the form:
30+
fn test3() {
31+
panic!();
32+
}
33+
34+
#[test]
35+
#[should_panic(expected(foo, bar))]
36+
//~^ WARN: argument must be of the form:
37+
fn test4() {
38+
panic!();
39+
}
40+
41+
#[test]
42+
#[should_panic(expected = "foo", bar)]
43+
//~^ WARN: argument must be of the form:
44+
fn test5() {
45+
panic!();
46+
}

0 commit comments

Comments
 (0)