pulled strings passes together, added more tests

This commit is contained in:
llogiq 2015-08-12 16:42:42 +02:00
parent 52c0cf5a9d
commit f9e851e212
3 changed files with 44 additions and 20 deletions

View File

@ -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);

View File

@ -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. \

View File

@ -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);
}