From ca7ad5fa805ee3e2ddd7181734ed0166fe168713 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 20 Nov 2014 12:37:37 +0530 Subject: [PATCH] Add DList lint (fixes #2) --- src/lib.rs | 3 ++- src/types.rs | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4528585c82c..6166767378a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,8 @@ extern crate syntax; #[phase(plugin, link)] extern crate rustc; - +// Only for the compile time checking of paths +extern crate collections; use rustc::plugin::Registry; use rustc::lint::LintPassObject; diff --git a/src/types.rs b/src/types.rs index 1b3c4f73c77..625baec78fe 100644 --- a/src/types.rs +++ b/src/types.rs @@ -11,6 +11,8 @@ pub struct TypePass; declare_lint!(CLIPPY_BOX_VEC, Warn, "Warn on usage of Box>") +declare_lint!(CLIPPY_DLIST, Warn, + "Warn on usage of DList") /// 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]> { @@ -45,10 +47,15 @@ pub fn span_note_and_lint(cx: &Context, lint: &'static Lint, span: Span, msg: &s impl LintPass for TypePass { fn get_lints(&self) -> LintArray { - lint_array!(CLIPPY_BOX_VEC) + lint_array!(CLIPPY_BOX_VEC, CLIPPY_DLIST) } fn check_ty(&mut self, cx: &Context, ty: &ast::Ty) { + { + // In case stuff gets moved around + use std::boxed::Box; + use std::vec::Vec; + } match_ty_unwrap(ty, &["std", "boxed", "Box"]).and_then(|t| t.head()) .map(|t| match_ty_unwrap(&**t, &["std", "vec", "Vec"])) .map(|_| { @@ -56,5 +63,22 @@ impl LintPass for TypePass { "You seem to be trying to use Box>. Did you mean to use Vec?", "Vec is already on the heap, Box> makes an extra allocation"); }); + { + // 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() { + span_note_and_lint(cx, CLIPPY_DLIST, ty.span, + "You seem to be trying to use a DList. Perhaps you meant some other data structure?", + "A RingBuf might work."); + return; + } + } } } \ No newline at end of file