diff --git a/tests/ui/string_add.rs b/tests/ui/string_add.rs new file mode 100644 index 00000000000..c9dd13eea8a --- /dev/null +++ b/tests/ui/string_add.rs @@ -0,0 +1,19 @@ +#[warn(clippy::string_add)] +#[allow(clippy::string_add_assign, unused)] +fn main() { + // ignores assignment distinction + let mut x = "".to_owned(); + + for _ in 1..3 { + x = x + "."; + } + + let y = "".to_owned(); + let z = y + "..."; + + assert_eq!(&x, &z); + + let mut x = 1; + x = x + 1; + assert_eq!(2, x); +} diff --git a/tests/ui/string_add.stderr b/tests/ui/string_add.stderr new file mode 100644 index 00000000000..8345c50f971 --- /dev/null +++ b/tests/ui/string_add.stderr @@ -0,0 +1,30 @@ +error: manual implementation of an assign operation + --> $DIR/string_add.rs:8:9 + | +LL | x = x + "."; + | ^^^^^^^^^^^ help: replace it with: `x += "."` + | + = note: `-D clippy::assign-op-pattern` implied by `-D warnings` + +error: you added something to a string. Consider using `String::push_str()` instead + --> $DIR/string_add.rs:8:13 + | +LL | x = x + "."; + | ^^^^^^^ + | + = note: `-D clippy::string-add` implied by `-D warnings` + +error: you added something to a string. Consider using `String::push_str()` instead + --> $DIR/string_add.rs:12:13 + | +LL | let z = y + "..."; + | ^^^^^^^^^ + +error: manual implementation of an assign operation + --> $DIR/string_add.rs:17:5 + | +LL | x = x + 1; + | ^^^^^^^^^ help: replace it with: `x += 1` + +error: aborting due to 4 previous errors + diff --git a/tests/ui/string_add_assign.fixed b/tests/ui/string_add_assign.fixed new file mode 100644 index 00000000000..db71bab1e52 --- /dev/null +++ b/tests/ui/string_add_assign.fixed @@ -0,0 +1,21 @@ +// run-rustfix + +#[allow(clippy::string_add, unused)] +#[warn(clippy::string_add_assign)] +fn main() { + // ignores assignment distinction + let mut x = "".to_owned(); + + for _ in 1..3 { + x += "."; + } + + let y = "".to_owned(); + let z = y + "..."; + + assert_eq!(&x, &z); + + let mut x = 1; + x += 1; + assert_eq!(2, x); +} diff --git a/tests/ui/string_add_assign.rs b/tests/ui/string_add_assign.rs new file mode 100644 index 00000000000..644991945cb --- /dev/null +++ b/tests/ui/string_add_assign.rs @@ -0,0 +1,21 @@ +// run-rustfix + +#[allow(clippy::string_add, unused)] +#[warn(clippy::string_add_assign)] +fn main() { + // ignores assignment distinction + let mut x = "".to_owned(); + + for _ in 1..3 { + x = x + "."; + } + + let y = "".to_owned(); + let z = y + "..."; + + assert_eq!(&x, &z); + + let mut x = 1; + x = x + 1; + assert_eq!(2, x); +} diff --git a/tests/ui/string_add_assign.stderr b/tests/ui/string_add_assign.stderr new file mode 100644 index 00000000000..7676175c1b8 --- /dev/null +++ b/tests/ui/string_add_assign.stderr @@ -0,0 +1,24 @@ +error: you assigned the result of adding something to this string. Consider using `String::push_str()` instead + --> $DIR/string_add_assign.rs:10:9 + | +LL | x = x + "."; + | ^^^^^^^^^^^ + | + = note: `-D clippy::string-add-assign` implied by `-D warnings` + +error: manual implementation of an assign operation + --> $DIR/string_add_assign.rs:10:9 + | +LL | x = x + "."; + | ^^^^^^^^^^^ help: replace it with: `x += "."` + | + = note: `-D clippy::assign-op-pattern` implied by `-D warnings` + +error: manual implementation of an assign operation + --> $DIR/string_add_assign.rs:19:5 + | +LL | x = x + 1; + | ^^^^^^^^^ help: replace it with: `x += 1` + +error: aborting due to 3 previous errors + diff --git a/tests/ui/strings.rs b/tests/ui/strings.rs deleted file mode 100644 index 766e23c744a..00000000000 --- a/tests/ui/strings.rs +++ /dev/null @@ -1,55 +0,0 @@ -#[warn(clippy::string_add)] -#[allow(clippy::string_add_assign)] -fn add_only() { - // ignores assignment distinction - let mut x = "".to_owned(); - - for _ in 1..3 { - x = x + "."; - } - - let y = "".to_owned(); - let z = y + "..."; - - assert_eq!(&x, &z); -} - -#[warn(clippy::string_add_assign)] -fn add_assign_only() { - let mut x = "".to_owned(); - - for _ in 1..3 { - x = x + "."; - } - - let y = "".to_owned(); - let z = y + "..."; - - assert_eq!(&x, &z); -} - -#[warn(clippy::string_add, clippy::string_add_assign)] -fn both() { - let mut x = "".to_owned(); - - for _ in 1..3 { - x = x + "."; - } - - let y = "".to_owned(); - let z = y + "..."; - - assert_eq!(&x, &z); -} - -#[allow(clippy::assign_op_pattern)] -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); -} diff --git a/tests/ui/strings.stderr b/tests/ui/strings.stderr deleted file mode 100644 index 7f684fe6355..00000000000 --- a/tests/ui/strings.stderr +++ /dev/null @@ -1,56 +0,0 @@ -error: manual implementation of an assign operation - --> $DIR/strings.rs:8:9 - | -LL | x = x + "."; - | ^^^^^^^^^^^ help: replace it with: `x += "."` - | - = note: `-D clippy::assign-op-pattern` implied by `-D warnings` - -error: you added something to a string. Consider using `String::push_str()` instead - --> $DIR/strings.rs:8:13 - | -LL | x = x + "."; - | ^^^^^^^ - | - = note: `-D clippy::string-add` implied by `-D warnings` - -error: you added something to a string. Consider using `String::push_str()` instead - --> $DIR/strings.rs:12:13 - | -LL | let z = y + "..."; - | ^^^^^^^^^ - -error: you assigned the result of adding something to this string. Consider using `String::push_str()` instead - --> $DIR/strings.rs:22:9 - | -LL | x = x + "."; - | ^^^^^^^^^^^ - | - = note: `-D clippy::string-add-assign` implied by `-D warnings` - -error: manual implementation of an assign operation - --> $DIR/strings.rs:22:9 - | -LL | x = x + "."; - | ^^^^^^^^^^^ help: replace it with: `x += "."` - -error: you assigned the result of adding something to this string. Consider using `String::push_str()` instead - --> $DIR/strings.rs:36:9 - | -LL | x = x + "."; - | ^^^^^^^^^^^ - -error: manual implementation of an assign operation - --> $DIR/strings.rs:36:9 - | -LL | x = x + "."; - | ^^^^^^^^^^^ help: replace it with: `x += "."` - -error: you added something to a string. Consider using `String::push_str()` instead - --> $DIR/strings.rs:40:13 - | -LL | let z = y + "..."; - | ^^^^^^^^^ - -error: aborting due to 8 previous errors -