Merge branch 'pr-394'

This commit is contained in:
Manish Goregaokar 2015-10-18 00:54:03 +05:30
commit dea087dd17
5 changed files with 102 additions and 1 deletions

View file

@ -50,6 +50,7 @@ pub mod precedence;
pub mod mutex_atomic;
pub mod zero_div_zero;
pub mod open_options;
pub mod needless_features;
mod reexport {
pub use syntax::ast::{Name, Ident, NodeId};
@ -94,6 +95,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_late_lint_pass(box open_options::NonSensicalOpenOptions);
reg.register_late_lint_pass(box zero_div_zero::ZeroDivZeroPass);
reg.register_late_lint_pass(box mutex_atomic::MutexAtomic);
reg.register_late_lint_pass(box needless_features::NeedlessFeaturesPass);
reg.register_lint_group("clippy_pedantic", vec![
methods::OPTION_UNWRAP_USED,
@ -150,6 +152,8 @@ pub fn plugin_registrar(reg: &mut Registry) {
mut_reference::UNNECESSARY_MUT_PASSED,
mutex_atomic::MUTEX_ATOMIC,
needless_bool::NEEDLESS_BOOL,
needless_features::UNSTABLE_AS_MUT_SLICE,
needless_features::UNSTABLE_AS_SLICE,
open_options::NONSENSICAL_OPEN_OPTIONS,
precedence::PRECEDENCE,
ptr_arg::PTR_ARG,

53
src/needless_features.rs Normal file
View file

@ -0,0 +1,53 @@
//! Checks for usage of nightly features that have simple stable equivalents
//!
//! This lint is **warn** by default
use rustc::lint::*;
use rustc_front::hir::*;
use utils::{span_lint};
use utils;
declare_lint! {
pub UNSTABLE_AS_SLICE,
Warn,
"as_slice is not stable and can be replaced by & v[..]\
see https://github.com/rust-lang/rust/issues/27729"
}
declare_lint! {
pub UNSTABLE_AS_MUT_SLICE,
Warn,
"as_mut_slice is not stable and can be replaced by &mut v[..]\
see https://github.com/rust-lang/rust/issues/27729"
}
#[derive(Copy,Clone)]
pub struct NeedlessFeaturesPass;
impl LintPass for NeedlessFeaturesPass {
fn get_lints(&self) -> LintArray {
lint_array!(UNSTABLE_AS_SLICE,UNSTABLE_AS_MUT_SLICE)
}
}
impl LateLintPass for NeedlessFeaturesPass {
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
if let ExprMethodCall(ref name, _, _) = expr.node {
if name.node.as_str() == "as_slice" && check_paths(cx, expr) {
span_lint(cx, UNSTABLE_AS_SLICE, expr.span,
"used as_slice() from the 'convert' nightly feature. Use &[..] \
instead");
}
if name.node.as_str() == "as_mut_slice" && check_paths(cx, expr) {
span_lint(cx, UNSTABLE_AS_MUT_SLICE, expr.span,
"used as_mut_slice() from the 'convert' nightly feature. Use &mut [..] \
instead");
}
}
}
}
fn check_paths(cx: &LateContext, expr: &Expr) -> bool {
utils::match_impl_method(cx, expr, &["collections", "vec", "Vec<T>"])
}

View file

@ -118,6 +118,19 @@ pub fn match_type(cx: &LateContext, ty: ty::Ty, path: &[&str]) -> bool {
}
}
/// check if method call given in "expr" belongs to given trait
pub fn match_impl_method(cx: &LateContext, expr: &Expr, path: &[&str]) -> bool {
let method_call = ty::MethodCall::expr(expr.id);
let trt_id = cx.tcx.tables
.borrow().method_map.get(&method_call)
.and_then(|callee| cx.tcx.impl_of_method(callee.def_id));
if let Some(trt_id) = trt_id {
match_def_path(cx, trt_id, path)
} else {
false
}
}
/// check if method call given in "expr" belongs to given trait
pub fn match_trait_method(cx: &LateContext, expr: &Expr, path: &[&str]) -> bool {
let method_call = ty::MethodCall::expr(expr.id);