From 225969e8a3b8025dac6d577f6b23af85485be7d0 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Wed, 12 Aug 2015 13:58:55 +0200 Subject: [PATCH] methods: move misc.StrToStringPass to MethodsPass --- src/lib.rs | 4 ++-- src/methods.rs | 12 ++++++++++-- src/misc.rs | 30 ------------------------------ 3 files changed, 12 insertions(+), 34 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fef678f668d..7b47edaa496 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,6 @@ pub mod returns; pub fn plugin_registrar(reg: &mut Registry) { reg.register_lint_pass(box types::TypePass 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::CmpNan 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_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, bit_mask::BAD_BIT_MASK, bit_mask::INEFFECTIVE_BIT_MASK, @@ -83,5 +82,6 @@ pub fn plugin_registrar(reg: &mut Registry) { misc::MODULO_ONE, methods::OPTION_UNWRAP_USED, methods::RESULT_UNWRAP_USED, + methods::STR_TO_STRING, ]); } diff --git a/src/methods.rs b/src/methods.rs index 3d9aa8c6ffc..f02e0664092 100644 --- a/src/methods.rs +++ b/src/methods.rs @@ -11,16 +11,19 @@ declare_lint!(pub OPTION_UNWRAP_USED, Warn, "Warn on using unwrap() on an Option value"); declare_lint!(pub RESULT_UNWRAP_USED, Allow, "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 { 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) { 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 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"]) { span_lint(cx, OPTION_UNWRAP_USED, expr.span, "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"); + } + } } } } diff --git a/src/misc.rs b/src/misc.rs index fa1847aad9a..d5e1efe2cc3 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -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"); #[allow(missing_copy_implementations)]