4 use rustc::lint::{Context, LintPass, LintArray, Lint, Level};
5 use syntax::codemap::Span;
7 use utils::{span_lint, span_help_and_lint};
9 /// Handles all the linting of funky types
10 #[allow(missing_copy_implementations)]
13 declare_lint!(pub BOX_VEC, Warn,
14 "Warn on usage of Box<Vec<T>>");
15 declare_lint!(pub LINKEDLIST, Warn,
16 "Warn on usage of LinkedList");
18 /// Matches a type with a provided string, and returns its type parameters if successful
19 pub fn match_ty_unwrap<'a>(ty: &'a Ty, segments: &[&str]) -> Option<&'a [P<Ty>]> {
21 TyPath(_, Path {segments: ref seg, ..}) => {
22 // So ast::Path isn't the full path, just the tokens that were provided.
23 // I could muck around with the maps and find the full path
24 // however the more efficient way is to simply reverse the iterators and zip them
25 // which will compare them in reverse until one of them runs out of segments
26 if seg.iter().rev().zip(segments.iter().rev()).all(|(a,b)| a.identifier.name == b) {
27 match seg[..].last() {
28 Some(&PathSegment {parameters: AngleBracketedParameters(ref a), ..}) => {
41 impl LintPass for TypePass {
42 fn get_lints(&self) -> LintArray {
43 lint_array!(BOX_VEC, LINKEDLIST)
46 fn check_ty(&mut self, cx: &Context, ty: &ast::Ty) {
48 // In case stuff gets moved around
52 match_ty_unwrap(ty, &["std", "boxed", "Box"]).and_then(|t| t.first())
53 .and_then(|t| match_ty_unwrap(&**t, &["std", "vec", "Vec"]))
55 span_help_and_lint(cx, BOX_VEC, ty.span,
56 "you seem to be trying to use `Box<Vec<T>>`. Did you mean to use `Vec<T>`?",
57 "`Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation");
60 // In case stuff gets moved around
61 use collections::linked_list::LinkedList as DL1;
62 use std::collections::linked_list::LinkedList as DL2;
63 use std::collections::linked_list::LinkedList as DL3;
65 let dlists = [vec!["std","collections","linked_list","LinkedList"],
66 vec!["std","collections","linked_list","LinkedList"],
67 vec!["collections","linked_list","LinkedList"]];
68 for path in dlists.iter() {
69 if match_ty_unwrap(ty, &path[..]).is_some() {
70 span_help_and_lint(cx, LINKEDLIST, ty.span,
71 "I see you're using a LinkedList! Perhaps you meant some other data structure?",
72 "a RingBuf might work");