]> git.lizzy.rs Git - rust.git/blob - tests/ui/lint/dead-code/self-assign.rs
Rollup merge of #106427 - mejrs:translation_errors, r=davidtwco
[rust.git] / tests / ui / lint / dead-code / self-assign.rs
1 // Test that dead code warnings are issued for superfluous assignments of
2 // fields or variables to themselves (issue #75356).
3
4 // ignore-test FIXME(81658, 83171)
5
6 // check-pass
7 #![allow(unused_assignments)]
8 #![warn(dead_code)]
9
10 fn main() {
11     let mut x = 0;
12     x = x;
13     //~^ WARNING: useless assignment of variable of type `i32` to itself
14
15     x = (x);
16     //~^ WARNING: useless assignment of variable of type `i32` to itself
17
18     x = {x};
19     // block expressions don't count as self-assignments
20
21
22     struct S<'a> { f: &'a str }
23     let mut s = S { f: "abc" };
24     s = s;
25     //~^ WARNING: useless assignment of variable of type `S` to itself
26
27     s.f = s.f;
28     //~^ WARNING: useless assignment of field of type `&str` to itself
29
30
31     struct N0 { x: Box<i32> }
32     struct N1 { n: N0 }
33     struct N2(N1);
34     struct N3 { n: N2 };
35     let mut n3 = N3 { n: N2(N1 { n: N0 { x: Box::new(42) } }) };
36     n3.n.0.n.x = n3.n.0.n.x;
37     //~^ WARNING: useless assignment of field of type `Box<i32>` to itself
38
39     let mut t = (1, ((2, 3, (4, 5)),));
40     t.1.0.2.1 = t.1.0.2.1;
41     //~^ WARNING: useless assignment of field of type `i32` to itself
42
43
44     let mut y = 0;
45     macro_rules! assign_to_y {
46         ($cur:expr) => {{
47             y = $cur;
48         }};
49     }
50     assign_to_y!(y);
51     // self-assignments in macro expansions are not reported either
52 }