types: use middle::ty types instead of ast types
This gets rid of the match_ty_unwrap function.
This commit is contained in:
parent
b2df15d65a
commit
707e95f2e5
71
src/types.rs
71
src/types.rs
@ -2,11 +2,10 @@ use rustc::lint::*;
|
|||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
use syntax::ast::*;
|
use syntax::ast::*;
|
||||||
use syntax::ast_util::{is_comparison_binop, binop_to_string};
|
use syntax::ast_util::{is_comparison_binop, binop_to_string};
|
||||||
use syntax::ptr::P;
|
|
||||||
use rustc::middle::ty;
|
use rustc::middle::ty;
|
||||||
use syntax::codemap::ExpnInfo;
|
use syntax::codemap::ExpnInfo;
|
||||||
|
|
||||||
use utils::{in_macro, snippet, span_lint, span_help_and_lint, in_external_macro};
|
use utils::{in_macro, match_def_path, snippet, span_lint, span_help_and_lint, in_external_macro};
|
||||||
|
|
||||||
/// Handles all the linting of funky types
|
/// Handles all the linting of funky types
|
||||||
#[allow(missing_copy_implementations)]
|
#[allow(missing_copy_implementations)]
|
||||||
@ -18,61 +17,37 @@ declare_lint!(pub LINKEDLIST, Warn,
|
|||||||
"usage of LinkedList, usually a vector is faster, or a more specialized data \
|
"usage of LinkedList, usually a vector is faster, or a more specialized data \
|
||||||
structure like a RingBuf");
|
structure like a RingBuf");
|
||||||
|
|
||||||
/// Matches a type with a provided string, and returns its type parameters if successful
|
|
||||||
pub fn match_ty_unwrap<'a>(ty: &'a Ty, segments: &[&str]) -> Option<&'a [P<Ty>]> {
|
|
||||||
match ty.node {
|
|
||||||
TyPath(_, Path {segments: ref seg, ..}) => {
|
|
||||||
// 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) {
|
|
||||||
match seg[..].last() {
|
|
||||||
Some(&PathSegment {parameters: AngleBracketedParameters(ref a), ..}) => {
|
|
||||||
Some(&a.types[..])
|
|
||||||
}
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
impl LintPass for TypePass {
|
impl LintPass for TypePass {
|
||||||
fn get_lints(&self) -> LintArray {
|
fn get_lints(&self) -> LintArray {
|
||||||
lint_array!(BOX_VEC, LINKEDLIST)
|
lint_array!(BOX_VEC, LINKEDLIST)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_ty(&mut self, cx: &Context, ty: &ast::Ty) {
|
fn check_ty(&mut self, cx: &Context, ast_ty: &ast::Ty) {
|
||||||
{
|
{
|
||||||
// In case stuff gets moved around
|
// In case stuff gets moved around
|
||||||
use std::boxed::Box;
|
use collections::vec::Vec;
|
||||||
use std::vec::Vec;
|
use collections::linked_list::LinkedList;
|
||||||
}
|
}
|
||||||
match_ty_unwrap(ty, &["std", "boxed", "Box"]).and_then(|t| t.first())
|
if let Some(ty) = cx.tcx.ast_ty_to_ty_cache.borrow().get(&ast_ty.id) {
|
||||||
.and_then(|t| match_ty_unwrap(&**t, &["std", "vec", "Vec"]))
|
if let ty::TyBox(ref inner) = ty.sty {
|
||||||
.map(|_| {
|
if let ty::TyStruct(did, _) = inner.sty {
|
||||||
span_help_and_lint(cx, BOX_VEC, ty.span,
|
if match_def_path(cx, did.did, &["collections", "vec", "Vec"]) {
|
||||||
"you seem to be trying to use `Box<Vec<T>>`. Did you mean to use `Vec<T>`?",
|
span_help_and_lint(
|
||||||
"`Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation");
|
cx, BOX_VEC, ast_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");
|
||||||
// In case stuff gets moved around
|
}
|
||||||
use collections::linked_list::LinkedList as DL1;
|
}
|
||||||
use std::collections::linked_list::LinkedList as DL2;
|
}
|
||||||
}
|
if let ty::TyStruct(did, _) = ty.sty {
|
||||||
let dlists = [vec!["std","collections","linked_list","LinkedList"],
|
if match_def_path(cx, did.did, &["collections", "linked_list", "LinkedList"]) {
|
||||||
vec!["collections","linked_list","LinkedList"]];
|
span_help_and_lint(
|
||||||
for path in &dlists {
|
cx, LINKEDLIST, ast_ty.span,
|
||||||
if match_ty_unwrap(ty, &path[..]).is_some() {
|
"I see you're using a LinkedList! Perhaps you meant some other data structure?",
|
||||||
span_help_and_lint(cx, LINKEDLIST, ty.span,
|
"a RingBuf might work");
|
||||||
"I see you're using a LinkedList! Perhaps you meant some other data structure?",
|
return;
|
||||||
"a RingBuf might work");
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
tests/compile-fail/dlist.rs
Normal file → Executable file
2
tests/compile-fail/dlist.rs
Normal file → Executable file
@ -12,4 +12,4 @@ pub fn test(foo: LinkedList<u8>) { //~ ERROR I see you're using a LinkedList!
|
|||||||
|
|
||||||
fn main(){
|
fn main(){
|
||||||
test(LinkedList::new());
|
test(LinkedList::new());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user