rust/src/types.rs

78 lines
3.0 KiB
Rust
Raw Normal View History

2014-11-19 02:57:34 -06:00
use syntax::ptr::P;
use syntax::ast;
use syntax::ast::*;
use rustc::lint::{Context, LintPass, LintArray, Lint, Level};
use syntax::codemap::Span;
use utils::{span_lint, span_help_and_lint};
2015-07-26 09:53:11 -05:00
2014-11-19 03:02:47 -06:00
/// Handles all the linting of funky types
#[allow(missing_copy_implementations)]
2014-11-19 02:57:34 -06:00
pub struct TypePass;
2014-12-25 17:54:44 -06:00
declare_lint!(pub BOX_VEC, Warn,
2014-12-19 03:11:00 -06:00
"Warn on usage of Box<Vec<T>>");
2015-03-02 04:43:44 -06:00
declare_lint!(pub LINKEDLIST, Warn,
"Warn on usage of LinkedList");
2014-11-19 02:57:34 -06:00
2014-11-19 03:02:47 -06:00
/// Matches a type with a provided string, and returns its type parameters if successful
2014-11-19 02:57:34 -06:00
pub fn match_ty_unwrap<'a>(ty: &'a Ty, segments: &[&str]) -> Option<&'a [P<Ty>]> {
match ty.node {
2015-04-13 12:44:45 -05:00
TyPath(_, Path {segments: ref seg, ..}) => {
2014-11-19 02:57:34 -06:00
// So ast::Path isn't the full path, just the tokens that were provided.
// I could muck around with the maps and find the full path
// however the more efficient way is to simply reverse the iterators and zip them
// which will compare them in reverse until one of them runs out of segments
if seg.iter().rev().zip(segments.iter().rev()).all(|(a,b)| a.identifier.name == b) {
2015-04-13 12:44:45 -05:00
match seg[..].last() {
2014-11-19 02:57:34 -06:00
Some(&PathSegment {parameters: AngleBracketedParameters(ref a), ..}) => {
2015-04-13 12:44:45 -05:00
Some(&a.types[..])
2014-11-19 02:57:34 -06:00
}
_ => None
}
} else {
None
}
},
_ => None
}
}
impl LintPass for TypePass {
fn get_lints(&self) -> LintArray {
2015-03-02 04:43:44 -06:00
lint_array!(BOX_VEC, LINKEDLIST)
2014-11-19 02:57:34 -06:00
}
fn check_ty(&mut self, cx: &Context, ty: &ast::Ty) {
2014-11-20 01:07:37 -06:00
{
// In case stuff gets moved around
use std::boxed::Box;
use std::vec::Vec;
}
2015-01-06 22:05:34 -06:00
match_ty_unwrap(ty, &["std", "boxed", "Box"]).and_then(|t| t.first())
2015-05-07 00:52:16 -05:00
.and_then(|t| match_ty_unwrap(&**t, &["std", "vec", "Vec"]))
2014-11-19 02:57:34 -06:00
.map(|_| {
span_help_and_lint(cx, BOX_VEC, ty.span,
"you seem to be trying to use `Box<Vec<T>>`. Did you mean to use `Vec<T>`?",
"`Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation");
2014-11-19 02:57:34 -06:00
});
2014-11-20 01:07:37 -06:00
{
// In case stuff gets moved around
2015-03-02 04:43:44 -06:00
use collections::linked_list::LinkedList as DL1;
use std::collections::linked_list::LinkedList as DL2;
use std::collections::linked_list::LinkedList as DL3;
2014-11-20 01:07:37 -06:00
}
2015-03-02 04:43:44 -06:00
let dlists = [vec!["std","collections","linked_list","LinkedList"],
vec!["std","collections","linked_list","LinkedList"],
vec!["collections","linked_list","LinkedList"]];
2014-11-20 01:07:37 -06:00
for path in dlists.iter() {
2015-04-13 12:44:45 -05:00
if match_ty_unwrap(ty, &path[..]).is_some() {
span_help_and_lint(cx, LINKEDLIST, ty.span,
2015-03-02 04:43:44 -06:00
"I see you're using a LinkedList! Perhaps you meant some other data structure?",
"a RingBuf might work");
2014-11-20 01:07:37 -06:00
return;
}
}
2014-11-19 02:57:34 -06:00
}
}