From f9e851e212e08174917aa724b5e56a92d9584cdf Mon Sep 17 00:00:00 2001 From: llogiq Date: Wed, 12 Aug 2015 16:42:42 +0200 Subject: [PATCH] pulled strings passes together, added more tests --- src/lib.rs | 1 - src/strings.rs | 18 ++------------ tests/compile-fail/strings.rs | 45 ++++++++++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e6703e6bb..f788c72db 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,7 +58,6 @@ pub fn plugin_registrar(reg: &mut Registry) { reg.register_lint_pass(box misc::ModuloOne as LintPassObject); reg.register_lint_pass(box unicode::Unicode as LintPassObject); reg.register_lint_pass(box strings::StringAdd as LintPassObject); - reg.register_lint_pass(box strings::StringAddAssign as LintPassObject); reg.register_lint_pass(box returns::ReturnPass as LintPassObject); reg.register_lint_pass(box methods::MethodsPass as LintPassObject); reg.register_lint_pass(box types::LetPass as LintPassObject); diff --git a/src/strings.rs b/src/strings.rs index 568b27bd3..85e6501d3 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -28,7 +28,7 @@ pub struct StringAdd; impl LintPass for StringAdd { fn get_lints(&self) -> LintArray { - lint_array!(STRING_ADD) + lint_array!(STRING_ADD, STRING_ADD_ASSIGN) } fn check_expr(&mut self, cx: &Context, e: &Expr) { @@ -50,21 +50,7 @@ impl LintPass for StringAdd { "you add something to a string. \ Consider using `String::push_str()` instead.") } - } - } -} - - -#[derive(Copy, Clone)] -pub struct StringAddAssign; - -impl LintPass for StringAddAssign { - fn get_lints(&self) -> LintArray { - lint_array!(STRING_ADD_ASSIGN) - } - - fn check_expr(&mut self, cx: &Context, e: &Expr) { - if let &ExprAssign(ref target, ref src) = &e.node { + } else if let &ExprAssign(ref target, ref src) = &e.node { if is_string(cx, target) && is_add(src, target) { span_lint(cx, STRING_ADD_ASSIGN, e.span, "you assign the result of adding something to this string. \ diff --git a/tests/compile-fail/strings.rs b/tests/compile-fail/strings.rs index e898a087d..02ebca2fe 100755 --- a/tests/compile-fail/strings.rs +++ b/tests/compile-fail/strings.rs @@ -1,9 +1,37 @@ #![feature(plugin)] #![plugin(clippy)] -#![deny(string_add_assign)] -#![deny(string_add)] -fn main() { +#[deny(string_add)] +#[allow(string_add_assign)] +fn add_only() { // ignores assignment distinction + let mut x = "".to_owned(); + + for _ in (1..3) { + x = x + "."; //~ERROR you add something to a string. + } + + let y = "".to_owned(); + let z = y + "..."; //~ERROR you add something to a string. + + assert_eq!(&x, &z); +} + +#[deny(string_add_assign)] +fn add_assign_only() { + let mut x = "".to_owned(); + + for _ in (1..3) { + x = x + "."; //~ERROR you assign the result of adding something to this string. + } + + let y = "".to_owned(); + let z = y + "..."; + + assert_eq!(&x, &z); +} + +#[deny(string_add, string_add_assign)] +fn both() { let mut x = "".to_owned(); for _ in (1..3) { @@ -15,3 +43,14 @@ fn main() { assert_eq!(&x, &z); } + +fn main() { + add_only(); + add_assign_only(); + both(); + + // the add is only caught for String + let mut x = 1; + x = x + 1; + assert_eq!(2, x); +}