Merge pull request #117 from Manishearth/rustup

rustup, also first time clippy was used on itself, which led to a sma…
This commit is contained in:
Manish Goregaokar 2015-07-17 00:10:15 +05:30
commit ac698826d7
7 changed files with 33 additions and 11 deletions

View File

@ -86,5 +86,12 @@ You can add options to `allow`/`warn`/`deny`:
*`deny` produces error instead of warnings*
To have cargo compile your crate with clippy without needing `#![plugin(clippy)]`
in your code, you can use:
```
cargo rustc -- -L /path/to/clippy_so -Z extra-plugins=clippy
```
##License
Licensed under [MPL](https://www.mozilla.org/MPL/2.0/). If you're having issues with the license, let me know and I'll try to change it to something more permissive.

View File

@ -18,7 +18,7 @@ impl LintPass for EtaPass {
fn check_expr(&mut self, cx: &Context, expr: &Expr) {
if let ExprClosure(_, ref decl, ref blk) = expr.node {
if blk.stmts.len() != 0 {
if !blk.stmts.is_empty() {
// || {foo(); bar()}; can't be reduced here
return;
}

View File

@ -5,7 +5,7 @@ use std::cell::RefCell;
use syntax::ptr::P;
use rustc::lint::{Context, LintPass, LintArray, Lint};
use rustc::util::nodemap::DefIdMap;
use rustc::middle::ty::{self, TypeVariants, mt, MethodTraitItemId, ImplOrTraitItemId};
use rustc::middle::ty::{self, TypeVariants, TypeAndMut, MethodTraitItemId, ImplOrTraitItemId};
use rustc::middle::def::{DefTy, DefStruct, DefTrait};
use syntax::codemap::{Span, Spanned};
use syntax::ast::*;

View File

@ -36,8 +36,8 @@ impl LintPass for MiscPass {
if arms.len() == 2 {
if arms[0].guard.is_none() && arms[1].pats.len() == 1 {
match arms[1].body.node {
ExprTup(ref v) if v.len() == 0 && arms[1].guard.is_none() => (),
ExprBlock(ref b) if b.stmts.len() == 0 && arms[1].guard.is_none() => (),
ExprTup(ref v) if v.is_empty() && arms[1].guard.is_none() => (),
ExprBlock(ref b) if b.stmts.is_empty() && arms[1].guard.is_none() => (),
_ => return
}
// In some cases, an exhaustive match is preferred to catch situations when

View File

@ -1,7 +1,7 @@
use syntax::ptr::P;
use syntax::ast::*;
use rustc::lint::{Context, LintPass, LintArray, Lint};
use rustc::middle::ty::{TypeVariants, mt, TyRef};
use rustc::middle::ty::{TypeVariants, TypeAndMut, TyRef};
use syntax::codemap::{BytePos, ExpnInfo, Span};
use utils::in_macro;
@ -42,7 +42,7 @@ fn check_expr_expd(cx: &Context, expr: &Expr, info: Option<&ExpnInfo>) {
cx.span_lint(MUT_MUT, expr.span,
"Generally you want to avoid &mut &mut _ if possible.")
}).unwrap_or_else(|| {
if let TyRef(_, mt{ty: _, mutbl: MutMutable}) =
if let TyRef(_, TypeAndMut{ty: _, mutbl: MutMutable}) =
cx.tcx.expr_ty(e).sty {
cx.span_lint(MUT_MUT, expr.span,
"This expression mutably borrows a mutable reference. \

View File

@ -10,6 +10,7 @@ use syntax::ast::*;
use syntax::ast_util::{is_comparison_binop, binop_to_string};
use syntax::ptr::P;
use syntax::codemap::Span;
use utils::de_p;
declare_lint! {
pub NEEDLESS_BOOL,
@ -28,10 +29,18 @@ impl LintPass for NeedlessBool {
fn check_expr(&mut self, cx: &Context, e: &Expr) {
if let ExprIf(_, ref then_block, Option::Some(ref else_expr)) = e.node {
match (fetch_bool_block(then_block), fetch_bool_expr(else_expr)) {
(Option::Some(true), Option::Some(true)) => { cx.span_lint(NEEDLESS_BOOL, e.span, "your if-then-else expression will always return true"); },
(Option::Some(true), Option::Some(false)) => { cx.span_lint(NEEDLESS_BOOL, e.span, "you can reduce your if-statement to its predicate"); },
(Option::Some(false), Option::Some(true)) => { cx.span_lint(NEEDLESS_BOOL, e.span, "you can reduce your if-statement to '!' + your predicate"); },
(Option::Some(false), Option::Some(false)) => { cx.span_lint(NEEDLESS_BOOL, e.span, "your if-then-else expression will always return false"); },
(Option::Some(true), Option::Some(true)) => {
cx.span_lint(NEEDLESS_BOOL, e.span,
"your if-then-else expression will always return true"); },
(Option::Some(true), Option::Some(false)) => {
cx.span_lint(NEEDLESS_BOOL, e.span,
"you can reduce your if-statement to its predicate"); },
(Option::Some(false), Option::Some(true)) => {
cx.span_lint(NEEDLESS_BOOL, e.span,
"you can reduce your if-statement to '!' + your predicate"); },
(Option::Some(false), Option::Some(false)) => {
cx.span_lint(NEEDLESS_BOOL, e.span,
"your if-then-else expression will always return false"); },
_ => ()
}
}
@ -39,7 +48,9 @@ impl LintPass for NeedlessBool {
}
fn fetch_bool_block(block: &Block) -> Option<bool> {
if block.stmts.is_empty() { block.expr.as_ref().and_then(|e| fetch_bool_expr(e)) } else { Option::None }
if block.stmts.is_empty() {
block.expr.as_ref().map(de_p).and_then(fetch_bool_expr)
} else { Option::None }
}
fn fetch_bool_expr(expr: &Expr) -> Option<bool> {

View File

@ -1,6 +1,7 @@
use rustc::lint::Context;
use syntax::ast::{DefId, Name, Path};
use syntax::codemap::{ExpnInfo, Span};
use syntax::ptr::P;
use rustc::middle::ty;
use std::borrow::{Cow, IntoCow};
use std::convert::From;
@ -48,3 +49,6 @@ pub fn match_path(path: &Path, segments: &[&str]) -> bool {
pub fn snippet<'a>(cx: &Context, span: Span, default: &'a str) -> Cow<'a, str> {
cx.sess().codemap().span_to_snippet(span).map(From::from).unwrap_or(Cow::Borrowed(default))
}
/// dereference a P<T> and return a ref on the result
pub fn de_p<T>(p: &P<T>) -> &T { &*p }