Merge pull request #143 from birkenfeld/more_methods

methods: move misc.StrToStringPass to MethodsPass
This commit is contained in:
Manish Goregaokar 2015-08-12 17:32:16 +05:30
commit 6ff1e9a766
3 changed files with 12 additions and 34 deletions

View file

@ -37,7 +37,6 @@ pub mod returns;
pub fn plugin_registrar(reg: &mut Registry) { pub fn plugin_registrar(reg: &mut Registry) {
reg.register_lint_pass(box types::TypePass as LintPassObject); reg.register_lint_pass(box types::TypePass as LintPassObject);
reg.register_lint_pass(box misc::MiscPass as LintPassObject); reg.register_lint_pass(box misc::MiscPass as LintPassObject);
reg.register_lint_pass(box misc::StrToStringPass as LintPassObject);
reg.register_lint_pass(box misc::TopLevelRefPass as LintPassObject); reg.register_lint_pass(box misc::TopLevelRefPass as LintPassObject);
reg.register_lint_pass(box misc::CmpNan as LintPassObject); reg.register_lint_pass(box misc::CmpNan as LintPassObject);
reg.register_lint_pass(box eq_op::EqOp as LintPassObject); reg.register_lint_pass(box eq_op::EqOp as LintPassObject);
@ -61,7 +60,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_lint_pass(box methods::MethodsPass as LintPassObject); reg.register_lint_pass(box methods::MethodsPass as LintPassObject);
reg.register_lint_group("clippy", vec![types::BOX_VEC, types::LINKEDLIST, reg.register_lint_group("clippy", vec![types::BOX_VEC, types::LINKEDLIST,
misc::SINGLE_MATCH, misc::STR_TO_STRING, misc::SINGLE_MATCH,
misc::TOPLEVEL_REF_ARG, eq_op::EQ_OP, misc::TOPLEVEL_REF_ARG, eq_op::EQ_OP,
bit_mask::BAD_BIT_MASK, bit_mask::BAD_BIT_MASK,
bit_mask::INEFFECTIVE_BIT_MASK, bit_mask::INEFFECTIVE_BIT_MASK,
@ -83,5 +82,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
misc::MODULO_ONE, misc::MODULO_ONE,
methods::OPTION_UNWRAP_USED, methods::OPTION_UNWRAP_USED,
methods::RESULT_UNWRAP_USED, methods::RESULT_UNWRAP_USED,
methods::STR_TO_STRING,
]); ]);
} }

View file

@ -11,16 +11,19 @@ declare_lint!(pub OPTION_UNWRAP_USED, Warn,
"Warn on using unwrap() on an Option value"); "Warn on using unwrap() on an Option value");
declare_lint!(pub RESULT_UNWRAP_USED, Allow, declare_lint!(pub RESULT_UNWRAP_USED, Allow,
"Warn on using unwrap() on a Result value"); "Warn on using unwrap() on a Result value");
declare_lint!(pub STR_TO_STRING, Warn,
"Warn when a String could use to_owned() instead of to_string()");
impl LintPass for MethodsPass { impl LintPass for MethodsPass {
fn get_lints(&self) -> LintArray { fn get_lints(&self) -> LintArray {
lint_array!(OPTION_UNWRAP_USED, RESULT_UNWRAP_USED) lint_array!(OPTION_UNWRAP_USED, RESULT_UNWRAP_USED, STR_TO_STRING)
} }
fn check_expr(&mut self, cx: &Context, expr: &Expr) { fn check_expr(&mut self, cx: &Context, expr: &Expr) {
if let ExprMethodCall(ref ident, _, ref args) = expr.node { if let ExprMethodCall(ref ident, _, ref args) = expr.node {
let ref obj_ty = walk_ptrs_ty(cx.tcx.expr_ty(&*args[0])).sty;
if ident.node.name == "unwrap" { if ident.node.name == "unwrap" {
if let ty::TyEnum(did, _) = walk_ptrs_ty(cx.tcx.expr_ty(&*args[0])).sty { if let ty::TyEnum(did, _) = *obj_ty {
if match_def_path(cx, did.did, &["core", "option", "Option"]) { if match_def_path(cx, did.did, &["core", "option", "Option"]) {
span_lint(cx, OPTION_UNWRAP_USED, expr.span, span_lint(cx, OPTION_UNWRAP_USED, expr.span,
"used unwrap() on an Option value. If you don't want \ "used unwrap() on an Option value. If you don't want \
@ -34,6 +37,11 @@ impl LintPass for MethodsPass {
} }
} }
} }
else if ident.node.name == "to_string" {
if let ty::TyStr = *obj_ty {
span_lint(cx, STR_TO_STRING, expr.span, "`str.to_owned()` is faster");
}
}
} }
} }
} }

View file

@ -59,36 +59,6 @@ impl LintPass for MiscPass {
} }
declare_lint!(pub STR_TO_STRING, Warn, "Warn when a String could use to_owned() instead of to_string()");
#[allow(missing_copy_implementations)]
pub struct StrToStringPass;
impl LintPass for StrToStringPass {
fn get_lints(&self) -> LintArray {
lint_array!(STR_TO_STRING)
}
fn check_expr(&mut self, cx: &Context, expr: &ast::Expr) {
match expr.node {
ast::ExprMethodCall(ref method, _, ref args)
if method.node.name == "to_string"
&& is_str(cx, &*args[0]) => {
span_lint(cx, STR_TO_STRING, expr.span, "`str.to_owned()` is faster");
},
_ => ()
}
fn is_str(cx: &Context, expr: &ast::Expr) -> bool {
match walk_ptrs_ty(cx.tcx.expr_ty(expr)).sty {
ty::TyStr => true,
_ => false
}
}
}
}
declare_lint!(pub TOPLEVEL_REF_ARG, Warn, "Warn about pattern matches with top-level `ref` bindings"); declare_lint!(pub TOPLEVEL_REF_ARG, Warn, "Warn about pattern matches with top-level `ref` bindings");
#[allow(missing_copy_implementations)] #[allow(missing_copy_implementations)]