mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-10 02:56:52 +00:00
Add warnings when the #[should_panic] attribute is invalid
This commit is contained in:
parent
a277f9deb0
commit
d6689fdc61
@ -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,
|
||||
}
|
||||
|
46
src/test/run-pass/test-should-panic-attr.rs
Normal file
46
src/test/run-pass/test-should-panic-attr.rs
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// compile-flags: --test
|
||||
|
||||
#[test]
|
||||
#[should_panic = "foo"]
|
||||
//~^ WARN: attribute must be of the form:
|
||||
fn test1() {
|
||||
panic!();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected)]
|
||||
//~^ WARN: argument must be of the form:
|
||||
fn test2() {
|
||||
panic!();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expect)]
|
||||
//~^ WARN: argument must be of the form:
|
||||
fn test3() {
|
||||
panic!();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected(foo, bar))]
|
||||
//~^ WARN: argument must be of the form:
|
||||
fn test4() {
|
||||
panic!();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "foo", bar)]
|
||||
//~^ WARN: argument must be of the form:
|
||||
fn test5() {
|
||||
panic!();
|
||||
}
|
Loading…
Reference in New Issue
Block a user