]> git.lizzy.rs Git - rust.git/blob - tests/ui/assign_ops2.rs
Limit commutative assign op lint to primitive types
[rust.git] / tests / ui / assign_ops2.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11 #![feature(tool_lints)]
12
13
14 #[allow(unused_assignments)]
15 #[warn(clippy::misrefactored_assign_op, clippy::assign_op_pattern)]
16 fn main() {
17     let mut a = 5;
18     a += a + 1;
19     a += 1 + a;
20     a -= a - 1;
21     a *= a * 99;
22     a *= 42 * a;
23     a /= a / 2;
24     a %= a % 5;
25     a &= a & 1;
26     a *= a * a;
27     a = a * a * a;
28     a = a * 42 * a;
29     a = a * 2 + a;
30     a -= 1 - a;
31     a /= 5 / a;
32     a %= 42 % a;
33     a <<= 6 << a;
34 }
35
36 // check that we don't lint on op assign impls, because that's just the way to impl them
37
38 use std::ops::{Mul, MulAssign};
39
40 #[derive(Copy, Clone, Debug, PartialEq)]
41 pub struct Wrap(i64);
42
43 impl Mul<i64> for Wrap {
44     type Output = Self;
45
46     fn mul(self, rhs: i64) -> Self {
47         Wrap(self.0 * rhs)
48     }
49 }
50
51 impl MulAssign<i64> for Wrap {
52     fn mul_assign(&mut self, rhs: i64) {
53         *self = *self * rhs
54     }
55 }
56
57 fn cow_add_assign() {
58     use std::borrow::Cow;
59     let mut buf = Cow::Owned(String::from("bar"));
60     let cows = Cow::Borrowed("foo");
61
62     // this can be linted
63     buf = buf + cows.clone();
64
65     // this should not as cow<str> Add is not commutative
66     buf = cows + buf;
67     println!("{}", buf);
68
69 }
70