]> git.lizzy.rs Git - rust.git/blob - tests/ui/assign_ops2.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[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 #[allow(unused_assignments)]
11 #[warn(clippy::misrefactored_assign_op, clippy::assign_op_pattern)]
12 fn main() {
13     let mut a = 5;
14     a += a + 1;
15     a += 1 + a;
16     a -= a - 1;
17     a *= a * 99;
18     a *= 42 * a;
19     a /= a / 2;
20     a %= a % 5;
21     a &= a & 1;
22     a *= a * a;
23     a = a * a * a;
24     a = a * 42 * a;
25     a = a * 2 + a;
26     a -= 1 - a;
27     a /= 5 / a;
28     a %= 42 % a;
29     a <<= 6 << a;
30 }
31
32 // check that we don't lint on op assign impls, because that's just the way to impl them
33
34 use std::ops::{Mul, MulAssign};
35
36 #[derive(Copy, Clone, Debug, PartialEq)]
37 pub struct Wrap(i64);
38
39 impl Mul<i64> for Wrap {
40     type Output = Self;
41
42     fn mul(self, rhs: i64) -> Self {
43         Wrap(self.0 * rhs)
44     }
45 }
46
47 impl MulAssign<i64> for Wrap {
48     fn mul_assign(&mut self, rhs: i64) {
49         *self = *self * rhs
50     }
51 }
52
53 fn cow_add_assign() {
54     use std::borrow::Cow;
55     let mut buf = Cow::Owned(String::from("bar"));
56     let cows = Cow::Borrowed("foo");
57
58     // this can be linted
59     buf = buf + cows.clone();
60
61     // this should not as cow<str> Add is not commutative
62     buf = cows + buf;
63     println!("{}", buf);
64 }