]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrow_deref_ref.rs
needless_deref
[rust.git] / tests / ui / borrow_deref_ref.rs
1 fn main() {}
2
3 mod should_lint {
4     fn foo() {
5         let a = &12;
6         let b = &*a;
7
8         let s = &String::new();
9         let x: &str = &*s;
10
11         let b = &mut &*bar(&12);
12     }
13
14     fn bar(x: &u32) -> &u32 {
15         x
16     }
17 }
18
19 // this mod explains why we should not lint `&mut &* (&T)`
20 mod should_not_lint1 {
21     fn foo(x: &mut &u32) {
22         *x = &1;
23     }
24
25     fn main() {
26         let mut x = &0;
27         foo(&mut &*x); // should not lint
28         assert_eq!(*x, 0);
29
30         foo(&mut x);
31         assert_eq!(*x, 1);
32     }
33 }
34
35 // similar to should_not_lint1
36 mod should_not_lint2 {
37     struct S<'a> {
38         a: &'a u32,
39         b: u32,
40     }
41
42     fn main() {
43         let s = S { a: &1, b: 1 };
44         let x = &mut &*s.a;
45         *x = &2;
46     }
47 }
48
49 // this mod explains why we should not lint `& &* (&T)`
50 mod false_negative {
51     fn foo() {
52         let x = &12;
53         let addr_x = &x as *const _ as usize;
54         let addr_y = &&*x as *const _ as usize; // assert ok
55         // let addr_y = &x as *const _ as usize; // assert fail
56         assert_ne!(addr_x, addr_y);
57     }
58 }