Skip to content

Commit dc0de42

Browse files
committed
Add lint and test for malformed but unused #[on_unimplemented] attributes
1 parent 4d17fba commit dc0de42

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/librustc/lint/builtin.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
//! a `pub fn new()`.
2727
use self::MethodContext::*;
2828

29+
30+
use fmt_macros::{Parser, Piece, Position};
31+
2932
use metadata::csearch;
3033
use middle::def::*;
3134
use middle::subst::Substs;
@@ -1921,3 +1924,63 @@ impl LintPass for UnstableFeatures {
19211924
}
19221925
}
19231926
}
1927+
1928+
/// Checks usage of `#[on_unimplemented]`
1929+
#[derive(Copy)]
1930+
pub struct BadOnUnimplemented;
1931+
1932+
declare_lint!(BAD_ON_UNIMPLEMENTED, Deny,
1933+
"Checks usage of `#[on_unimplemented]`");
1934+
1935+
impl LintPass for BadOnUnimplemented {
1936+
fn get_lints(&self) -> LintArray {
1937+
lint_array!(BAD_ON_UNIMPLEMENTED)
1938+
}
1939+
fn check_item(&mut self, ctx: &Context, item: &ast::Item) {
1940+
match item.node {
1941+
ast::ItemTrait(_, ref generics, _, _) => {
1942+
if let Some(ref attr) = item.attrs.iter().find(|&: a| {
1943+
a.check_name("on_unimplemented")
1944+
}) {
1945+
if let Some(ref istring) = attr.value_str() {
1946+
let mut parser = Parser::new(istring.get());
1947+
let types = generics.ty_params.as_slice();
1948+
for token in parser {
1949+
match token {
1950+
Piece::String(_) => (), // Normal string, no need to check it
1951+
Piece::NextArgument(a) => match a.position {
1952+
// `{Self}` is allowed
1953+
Position::ArgumentNamed(s) if s == "Self" => (),
1954+
// So is `{A}` if A is a type parameter
1955+
Position::ArgumentNamed(s) => match types.iter().find(|t| {
1956+
t.ident.as_str() == s
1957+
}) {
1958+
Some(_) => (),
1959+
None => {
1960+
ctx.span_lint(BAD_ON_UNIMPLEMENTED, attr.span,
1961+
format!("there is no type parameter \
1962+
{} on trait {}",
1963+
s, item.ident.as_str())
1964+
.as_slice());
1965+
}
1966+
},
1967+
// `{:1}` and `{}` are not to be used
1968+
Position::ArgumentIs(_) | Position::ArgumentNext => {
1969+
ctx.span_lint(BAD_ON_UNIMPLEMENTED, attr.span,
1970+
"only named substitution \
1971+
parameters are allowed");
1972+
}
1973+
}
1974+
}
1975+
}
1976+
} else {
1977+
ctx.span_lint(BAD_ON_UNIMPLEMENTED, attr.span,
1978+
"this attribute must have a value, \
1979+
eg `#[on_unimplemented = \"foo\"]`")
1980+
}
1981+
}
1982+
},
1983+
_ => () // Not a trait def, move along
1984+
}
1985+
}
1986+
}

src/librustc/lint/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ impl LintStore {
211211
UnusedAllocation,
212212
MissingCopyImplementations,
213213
UnstableFeatures,
214+
BadOnUnimplemented,
214215
);
215216

216217
add_builtin_with_new!(sess,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2014 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+
// ignore-tidy-linelength
11+
12+
#[allow(unused)]
13+
14+
#[on_unimplemented = "test error `{Self}` with `{Bar}` `{Baz}` `{Quux}`"]
15+
trait Foo<Bar, Baz, Quux>{}
16+
17+
#[on_unimplemented="a collection of type `{Self}` cannot be built from an iterator over elements of type `{A}`"]
18+
trait MyFromIterator<A> {
19+
/// Build a container with elements from an external iterator.
20+
fn my_from_iter<T: Iterator<Item=A>>(iterator: T) -> Self;
21+
}
22+
23+
#[on_unimplemented] //~ ERROR this attribute must have a value
24+
trait BadAnnotation1 {}
25+
26+
#[on_unimplemented = "Unimplemented trait error on `{Self}` with params `<{A},{B},{C}>`"]
27+
//~^ ERROR there is no type parameter C on trait BadAnnotation2
28+
trait BadAnnotation2<A,B> {}
29+
30+
31+
pub fn main() {
32+
}

0 commit comments

Comments
 (0)