]> git.lizzy.rs Git - rust.git/blob - tests/ui/mut_reference.rs
Merge pull request #3291 from JoshMcguigan/cmp_owned-3289
[rust.git] / tests / ui / mut_reference.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
12
13
14 #![allow(unused_variables, clippy::trivially_copy_pass_by_ref)]
15
16 fn takes_an_immutable_reference(a: &i32) {}
17 fn takes_a_mutable_reference(a: &mut i32) {}
18
19 struct MyStruct;
20
21 impl MyStruct {
22     fn takes_an_immutable_reference(&self, a: &i32) {
23     }
24
25     fn takes_a_mutable_reference(&self, a: &mut i32) {
26     }
27 }
28
29 #[warn(clippy::unnecessary_mut_passed)]
30 fn main() {
31     // Functions
32     takes_an_immutable_reference(&mut 42);
33     let as_ptr: fn(&i32) = takes_an_immutable_reference;
34     as_ptr(&mut 42);
35
36     // Methods
37     let my_struct = MyStruct;
38     my_struct.takes_an_immutable_reference(&mut 42);
39
40
41     // No error
42
43     // Functions
44     takes_an_immutable_reference(&42);
45     let as_ptr: fn(&i32) = takes_an_immutable_reference;
46     as_ptr(&42);
47
48     takes_a_mutable_reference(&mut 42);
49     let as_ptr: fn(&mut i32) = takes_a_mutable_reference;
50     as_ptr(&mut 42);
51
52     let a = &mut 42;
53     takes_an_immutable_reference(a);
54
55     // Methods
56     my_struct.takes_an_immutable_reference(&42);
57     my_struct.takes_a_mutable_reference(&mut 42);
58     my_struct.takes_an_immutable_reference(a);
59 }