Skip to content

Commit ca7ad5f

Browse files
committed
Add DList lint (fixes #2)
1 parent 7cf7e43 commit ca7ad5f

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ extern crate syntax;
77
#[phase(plugin, link)]
88
extern crate rustc;
99

10-
10+
// Only for the compile time checking of paths
11+
extern crate collections;
1112

1213
use rustc::plugin::Registry;
1314
use rustc::lint::LintPassObject;

src/types.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub struct TypePass;
1111

1212
declare_lint!(CLIPPY_BOX_VEC, Warn,
1313
"Warn on usage of Box<Vec<T>>")
14+
declare_lint!(CLIPPY_DLIST, Warn,
15+
"Warn on usage of DList")
1416

1517
/// Matches a type with a provided string, and returns its type parameters if successful
1618
pub fn match_ty_unwrap<'a>(ty: &'a Ty, segments: &[&str]) -> Option<&'a [P<Ty>]> {
@@ -45,16 +47,38 @@ pub fn span_note_and_lint(cx: &Context, lint: &'static Lint, span: Span, msg: &s
4547

4648
impl LintPass for TypePass {
4749
fn get_lints(&self) -> LintArray {
48-
lint_array!(CLIPPY_BOX_VEC)
50+
lint_array!(CLIPPY_BOX_VEC, CLIPPY_DLIST)
4951
}
5052

5153
fn check_ty(&mut self, cx: &Context, ty: &ast::Ty) {
54+
{
55+
// In case stuff gets moved around
56+
use std::boxed::Box;
57+
use std::vec::Vec;
58+
}
5259
match_ty_unwrap(ty, &["std", "boxed", "Box"]).and_then(|t| t.head())
5360
.map(|t| match_ty_unwrap(&**t, &["std", "vec", "Vec"]))
5461
.map(|_| {
5562
span_note_and_lint(cx, CLIPPY_BOX_VEC, ty.span,
5663
"You seem to be trying to use Box<Vec<T>>. Did you mean to use Vec<T>?",
5764
"Vec<T> is already on the heap, Box<Vec<T>> makes an extra allocation");
5865
});
66+
{
67+
// In case stuff gets moved around
68+
use collections::dlist::DList as DL1;
69+
use std::collections::dlist::DList as DL2;
70+
use std::collections::DList as DL3;
71+
}
72+
let dlists = [vec!["std","collections","dlist","DList"],
73+
vec!["std","collections","DList"],
74+
vec!["collections","dlist","DList"]];
75+
for path in dlists.iter() {
76+
if match_ty_unwrap(ty, path.as_slice()).is_some() {
77+
span_note_and_lint(cx, CLIPPY_DLIST, ty.span,
78+
"You seem to be trying to use a DList. Perhaps you meant some other data structure?",
79+
"A RingBuf might work.");
80+
return;
81+
}
82+
}
5983
}
6084
}

0 commit comments

Comments
 (0)