rust/src/types.rs

86 lines
3.3 KiB
Rust
Raw Normal View History

2014-11-19 14:27:34 +05:30
use syntax::ptr::P;
use syntax::ast;
use syntax::ast::*;
use rustc::lint::{Context, LintPass, LintArray, Lint, Level};
use syntax::codemap::Span;
2014-11-19 14:32:47 +05:30
/// Handles all the linting of funky types
#[allow(missing_copy_implementations)]
2014-11-19 14:27:34 +05:30
pub struct TypePass;
2014-12-26 05:24:44 +05:30
declare_lint!(pub BOX_VEC, Warn,
2014-12-19 14:41:00 +05:30
"Warn on usage of Box<Vec<T>>");
2015-03-02 16:13:44 +05:30
declare_lint!(pub LINKEDLIST, Warn,
"Warn on usage of LinkedList");
2014-11-19 14:27:34 +05:30
2014-11-19 14:32:47 +05:30
/// Matches a type with a provided string, and returns its type parameters if successful
2014-11-19 14:27:34 +05:30
pub fn match_ty_unwrap<'a>(ty: &'a Ty, segments: &[&str]) -> Option<&'a [P<Ty>]> {
match ty.node {
TyPath(Path {segments: ref seg, ..}, _) => {
2014-11-19 14:27:34 +05:30
// 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.as_str() == *b) {
match seg.as_slice().last() {
Some(&PathSegment {parameters: AngleBracketedParameters(ref a), ..}) => {
Some(a.types.as_slice())
}
_ => None
}
} else {
None
}
},
_ => None
}
}
2014-11-19 14:32:47 +05:30
/// Lets me span a note only if the lint is shown
pub fn span_note_and_lint(cx: &Context, lint: &'static Lint, span: Span, msg: &str, note: &str) {
2014-11-19 14:27:34 +05:30
cx.span_lint(lint, span, msg);
if cx.current_level(lint) != Level::Allow {
cx.sess().span_note(span, note);
}
}
2014-11-19 14:32:47 +05:30
2014-11-19 14:27:34 +05:30
impl LintPass for TypePass {
fn get_lints(&self) -> LintArray {
2015-03-02 16:13:44 +05:30
lint_array!(BOX_VEC, LINKEDLIST)
2014-11-19 14:27:34 +05:30
}
fn check_ty(&mut self, cx: &Context, ty: &ast::Ty) {
2014-11-20 12:37:37 +05:30
{
// In case stuff gets moved around
use std::boxed::Box;
use std::vec::Vec;
}
2015-01-07 09:35:34 +05:30
match_ty_unwrap(ty, &["std", "boxed", "Box"]).and_then(|t| t.first())
2014-11-19 14:27:34 +05:30
.map(|t| match_ty_unwrap(&**t, &["std", "vec", "Vec"]))
.map(|_| {
2014-12-26 05:24:44 +05:30
span_note_and_lint(cx, BOX_VEC, ty.span,
2014-11-19 14:34:18 +05:30
"You seem to be trying to use Box<Vec<T>>. Did you mean to use Vec<T>?",
2014-11-19 14:27:34 +05:30
"Vec<T> is already on the heap, Box<Vec<T>> makes an extra allocation");
});
2014-11-20 12:37:37 +05:30
{
// In case stuff gets moved around
2015-03-02 16:13:44 +05:30
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 12:37:37 +05:30
}
2015-03-02 16:13:44 +05:30
let dlists = [vec!["std","collections","linked_list","LinkedList"],
vec!["std","collections","linked_list","LinkedList"],
vec!["collections","linked_list","LinkedList"]];
2014-11-20 12:37:37 +05:30
for path in dlists.iter() {
if match_ty_unwrap(ty, path.as_slice()).is_some() {
2015-03-02 16:13:44 +05:30
span_note_and_lint(cx, LINKEDLIST, ty.span,
"I see you're using a LinkedList! Perhaps you meant some other data structure?",
2014-11-20 12:37:37 +05:30
"A RingBuf might work.");
return;
}
}
2014-11-19 14:27:34 +05:30
}
}