rust/src/types.rs

86 lines
3.2 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;
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>>");
2014-12-25 17:54:44 -06:00
declare_lint!(pub DLIST, Warn,
2014-12-19 03:11:00 -06:00
"Warn on usage of DList");
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 {
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.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 03:02:47 -06:00
/// 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 02:57:34 -06:00
cx.span_lint(lint, span, msg);
if cx.current_level(lint) != Level::Allow {
cx.sess().span_note(span, note);
}
}
2014-11-19 03:02:47 -06:00
2014-11-19 02:57:34 -06:00
impl LintPass for TypePass {
fn get_lints(&self) -> LintArray {
2014-12-25 17:54:44 -06:00
lint_array!(BOX_VEC, DLIST)
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())
2014-11-19 02:57:34 -06:00
.map(|t| match_ty_unwrap(&**t, &["std", "vec", "Vec"]))
.map(|_| {
2014-12-25 17:54:44 -06:00
span_note_and_lint(cx, BOX_VEC, ty.span,
2014-11-19 03:04:18 -06:00
"You seem to be trying to use Box<Vec<T>>. Did you mean to use Vec<T>?",
2014-11-19 02:57:34 -06:00
"Vec<T> is already on the heap, Box<Vec<T>> makes an extra allocation");
});
2014-11-20 01:07:37 -06:00
{
// In case stuff gets moved around
use collections::dlist::DList as DL1;
use std::collections::dlist::DList as DL2;
use std::collections::DList as DL3;
}
let dlists = [vec!["std","collections","dlist","DList"],
vec!["std","collections","DList"],
vec!["collections","dlist","DList"]];
for path in dlists.iter() {
if match_ty_unwrap(ty, path.as_slice()).is_some() {
2014-12-25 17:54:44 -06:00
span_note_and_lint(cx, DLIST, ty.span,
2014-11-20 01:08:27 -06:00
"I see you're using a DList! Perhaps you meant some other data structure?",
2014-11-20 01:07:37 -06:00
"A RingBuf might work.");
return;
}
}
2014-11-19 02:57:34 -06:00
}
}