Merge pull request #3292 from kimsnj/commutative_assign_op

Limit commutative assign op lint to primitive types
This commit is contained in:
Oliver S̶c̶h̶n̶e̶i̶d̶e̶r Scherer 2018-10-10 22:35:02 +02:00 committed by GitHub
commit dd11ffac0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 2 deletions

View File

@ -215,7 +215,9 @@ macro_rules! ops {
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

View File

@ -53,3 +53,18 @@ fn mul_assign(&mut self, rhs: i64) {
*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);
}

View File

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