]> git.lizzy.rs Git - rust.git/blob - src/types.rs
all: make style of lint messages consistent
[rust.git] / src / types.rs
1 use syntax::ptr::P;
2 use syntax::ast;
3 use syntax::ast::*;
4 use rustc::lint::{Context, LintPass, LintArray, Lint, Level};
5 use syntax::codemap::Span;
6
7 use utils::{span_lint, span_help_and_lint};
8
9 /// Handles all the linting of funky types
10 #[allow(missing_copy_implementations)]
11 pub struct TypePass;
12
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");
17
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>]> {
20     match ty.node {
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), ..}) => {
29                         Some(&a.types[..])
30                     }
31                     _ => None
32                 }
33             } else {
34                 None
35             }
36         },
37         _ => None
38     }
39 }
40
41 impl LintPass for TypePass {
42     fn get_lints(&self) -> LintArray {
43         lint_array!(BOX_VEC, LINKEDLIST)
44     }
45
46     fn check_ty(&mut self, cx: &Context, ty: &ast::Ty) {
47         {
48             // In case stuff gets moved around
49             use std::boxed::Box;
50             use std::vec::Vec;
51         }
52         match_ty_unwrap(ty, &["std", "boxed", "Box"]).and_then(|t| t.first())
53           .and_then(|t| match_ty_unwrap(&**t, &["std", "vec", "Vec"]))
54           .map(|_| {
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");
58           });
59         {
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;
64         }
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");
73                 return;
74             }
75         }
76     }
77 }