Skip to content

Commit 6ff1e9a

Browse files
committed
Merge pull request #143 from birkenfeld/more_methods
methods: move misc.StrToStringPass to MethodsPass
2 parents 8bcd01f + 225969e commit 6ff1e9a

File tree

3 files changed

+12
-34
lines changed

3 files changed

+12
-34
lines changed

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ pub mod returns;
3737
pub fn plugin_registrar(reg: &mut Registry) {
3838
reg.register_lint_pass(box types::TypePass as LintPassObject);
3939
reg.register_lint_pass(box misc::MiscPass as LintPassObject);
40-
reg.register_lint_pass(box misc::StrToStringPass as LintPassObject);
4140
reg.register_lint_pass(box misc::TopLevelRefPass as LintPassObject);
4241
reg.register_lint_pass(box misc::CmpNan as LintPassObject);
4342
reg.register_lint_pass(box eq_op::EqOp as LintPassObject);
@@ -61,7 +60,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
6160
reg.register_lint_pass(box methods::MethodsPass as LintPassObject);
6261

6362
reg.register_lint_group("clippy", vec![types::BOX_VEC, types::LINKEDLIST,
64-
misc::SINGLE_MATCH, misc::STR_TO_STRING,
63+
misc::SINGLE_MATCH,
6564
misc::TOPLEVEL_REF_ARG, eq_op::EQ_OP,
6665
bit_mask::BAD_BIT_MASK,
6766
bit_mask::INEFFECTIVE_BIT_MASK,
@@ -83,5 +82,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
8382
misc::MODULO_ONE,
8483
methods::OPTION_UNWRAP_USED,
8584
methods::RESULT_UNWRAP_USED,
85+
methods::STR_TO_STRING,
8686
]);
8787
}

src/methods.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ declare_lint!(pub OPTION_UNWRAP_USED, Warn,
1111
"Warn on using unwrap() on an Option value");
1212
declare_lint!(pub RESULT_UNWRAP_USED, Allow,
1313
"Warn on using unwrap() on a Result value");
14+
declare_lint!(pub STR_TO_STRING, Warn,
15+
"Warn when a String could use to_owned() instead of to_string()");
1416

1517
impl LintPass for MethodsPass {
1618
fn get_lints(&self) -> LintArray {
17-
lint_array!(OPTION_UNWRAP_USED, RESULT_UNWRAP_USED)
19+
lint_array!(OPTION_UNWRAP_USED, RESULT_UNWRAP_USED, STR_TO_STRING)
1820
}
1921

2022
fn check_expr(&mut self, cx: &Context, expr: &Expr) {
2123
if let ExprMethodCall(ref ident, _, ref args) = expr.node {
24+
let ref obj_ty = walk_ptrs_ty(cx.tcx.expr_ty(&*args[0])).sty;
2225
if ident.node.name == "unwrap" {
23-
if let ty::TyEnum(did, _) = walk_ptrs_ty(cx.tcx.expr_ty(&*args[0])).sty {
26+
if let ty::TyEnum(did, _) = *obj_ty {
2427
if match_def_path(cx, did.did, &["core", "option", "Option"]) {
2528
span_lint(cx, OPTION_UNWRAP_USED, expr.span,
2629
"used unwrap() on an Option value. If you don't want \
@@ -34,6 +37,11 @@ impl LintPass for MethodsPass {
3437
}
3538
}
3639
}
40+
else if ident.node.name == "to_string" {
41+
if let ty::TyStr = *obj_ty {
42+
span_lint(cx, STR_TO_STRING, expr.span, "`str.to_owned()` is faster");
43+
}
44+
}
3745
}
3846
}
3947
}

src/misc.rs

-30
Original file line numberDiff line numberDiff line change
@@ -59,36 +59,6 @@ impl LintPass for MiscPass {
5959
}
6060

6161

62-
declare_lint!(pub STR_TO_STRING, Warn, "Warn when a String could use to_owned() instead of to_string()");
63-
64-
#[allow(missing_copy_implementations)]
65-
pub struct StrToStringPass;
66-
67-
impl LintPass for StrToStringPass {
68-
fn get_lints(&self) -> LintArray {
69-
lint_array!(STR_TO_STRING)
70-
}
71-
72-
fn check_expr(&mut self, cx: &Context, expr: &ast::Expr) {
73-
match expr.node {
74-
ast::ExprMethodCall(ref method, _, ref args)
75-
if method.node.name == "to_string"
76-
&& is_str(cx, &*args[0]) => {
77-
span_lint(cx, STR_TO_STRING, expr.span, "`str.to_owned()` is faster");
78-
},
79-
_ => ()
80-
}
81-
82-
fn is_str(cx: &Context, expr: &ast::Expr) -> bool {
83-
match walk_ptrs_ty(cx.tcx.expr_ty(expr)).sty {
84-
ty::TyStr => true,
85-
_ => false
86-
}
87-
}
88-
}
89-
}
90-
91-
9262
declare_lint!(pub TOPLEVEL_REF_ARG, Warn, "Warn about pattern matches with top-level `ref` bindings");
9363

9464
#[allow(missing_copy_implementations)]

0 commit comments

Comments
 (0)