rust/src/types.rs

110 lines
4.0 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::middle::ty;
2014-11-19 14:27:34 +05:30
use rustc::lint::{Context, LintPass, LintArray, Lint, Level};
use syntax::codemap::{ExpnInfo, Span};
2014-11-19 14:27:34 +05:30
use utils::{in_macro, snippet, span_lint, span_help_and_lint};
2015-07-26 20:23:11 +05:30
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 {
2015-04-13 23:14:45 +05:30
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.name == b) {
2015-04-13 23:14:45 +05:30
match seg[..].last() {
2014-11-19 14:27:34 +05:30
Some(&PathSegment {parameters: AngleBracketedParameters(ref a), ..}) => {
2015-04-13 23:14:45 +05:30
Some(&a.types[..])
2014-11-19 14:27:34 +05:30
}
_ => None
}
} else {
None
}
},
_ => None
}
}
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())
2015-05-06 22:52:16 -07:00
.and_then(|t| match_ty_unwrap(&**t, &["std", "vec", "Vec"]))
2014-11-19 14:27:34 +05:30
.map(|_| {
span_help_and_lint(cx, BOX_VEC, 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");
2014-11-19 14:27:34 +05:30
});
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() {
2015-04-13 23:14:45 +05:30
if match_ty_unwrap(ty, &path[..]).is_some() {
span_help_and_lint(cx, LINKEDLIST, ty.span,
2015-03-02 16:13:44 +05:30
"I see you're using a LinkedList! Perhaps you meant some other data structure?",
"a RingBuf might work");
2014-11-20 12:37:37 +05:30
return;
}
}
2014-11-19 14:27:34 +05:30
}
}
#[allow(missing_copy_implementations)]
pub struct LetPass;
declare_lint!(pub LET_UNIT_VALUE, Warn,
"Warn on let-binding a value of unit type");
fn check_let_unit(cx: &Context, decl: &Decl, info: Option<&ExpnInfo>) {
if in_macro(cx, info) { return; }
if let DeclLocal(ref local) = decl.node {
let bindtype = &cx.tcx.pat_ty(&*local.pat).sty;
if *bindtype == ty::TyTuple(vec![]) {
span_lint(cx, LET_UNIT_VALUE, decl.span, &format!(
"this let-binding has unit value. Consider omitting `let {} =`.",
snippet(cx, local.pat.span, "..")));
}
}
}
impl LintPass for LetPass {
fn get_lints(&self) -> LintArray {
lint_array!(LET_UNIT_VALUE)
}
fn check_decl(&mut self, cx: &Context, decl: &Decl) {
cx.sess().codemap().with_expn_info(
decl.span.expn_id,
|info| check_let_unit(cx, decl, info));
}
}