about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOliver S̶c̶h̶n̶e̶i̶d̶e̶r Scherer <github35764891676564198441@oli-obk.de>2018-10-10 22:35:02 +0200
committerGitHub <noreply@github.com>2018-10-10 22:35:02 +0200
commitdd11ffac0b944eddc605a06a742b02f43c57755f (patch)
tree0c15a7cd8ac537bf33eb4d45ba91e2bd7803ad82
parentf6882ede4dc32684e000d256644c9e192ea8c738 (diff)
parentf9e4f5695dca025a868e3cdc24bea387c7c475f9 (diff)
downloadrust-dd11ffac0b944eddc605a06a742b02f43c57755f.tar.gz
rust-dd11ffac0b944eddc605a06a742b02f43c57755f.zip
Merge pull request #3292 from kimsnj/commutative_assign_op
Limit commutative assign op lint to primitive types
-rw-r--r--clippy_lints/src/assign_ops.rs4
-rw-r--r--tests/ui/assign_ops2.rs15
-rw-r--r--tests/ui/assign_ops2.stderr10
3 files changed, 27 insertions, 2 deletions
diff --git a/clippy_lints/src/assign_ops.rs b/clippy_lints/src/assign_ops.rs
index 3fbac7bc153..803c79d42fc 100644
--- a/clippy_lints/src/assign_ops.rs
+++ b/clippy_lints/src/assign_ops.rs
@@ -215,7 +215,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
                             lint(assignee, r);
                         }
                         // a = b commutative_op a
-                        if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r) {
+                        // Limited to primitive type as these ops are know to be commutative
+                        if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r)
+                                && cx.tables.expr_ty(assignee).is_primitive_ty() {
                             match op.node {
                                 hir::BinOpKind::Add
                                 | hir::BinOpKind::Mul
diff --git a/tests/ui/assign_ops2.rs b/tests/ui/assign_ops2.rs
index 9eef898c9a7..60a9d2fb73e 100644
--- a/tests/ui/assign_ops2.rs
+++ b/tests/ui/assign_ops2.rs
@@ -53,3 +53,18 @@ impl MulAssign<i64> for Wrap {
         *self = *self * rhs
     }
 }
+
+fn cow_add_assign() {
+    use std::borrow::Cow;
+    let mut buf = Cow::Owned(String::from("bar"));
+    let cows = Cow::Borrowed("foo");
+
+    // this can be linted
+    buf = buf + cows.clone();
+
+    // this should not as cow<str> Add is not commutative
+    buf = cows + buf;
+    println!("{}", buf);
+
+}
+
diff --git a/tests/ui/assign_ops2.stderr b/tests/ui/assign_ops2.stderr
index 8e44fc13bb7..bd49c3cdd80 100644
--- a/tests/ui/assign_ops2.stderr
+++ b/tests/ui/assign_ops2.stderr
@@ -126,5 +126,13 @@ help: or
 26 |     a = a * a * a;
    |     ^^^^^^^^^^^^^
 
-error: aborting due to 9 previous errors
+error: manual implementation of an assign operation
+  --> $DIR/assign_ops2.rs:63:5
+   |
+63 |     buf = buf + cows.clone();
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `buf += cows.clone()`
+   |
+   = note: `-D clippy::assign-op-pattern` implied by `-D warnings`
+
+error: aborting due to 10 previous errors