]> git.lizzy.rs Git - rust.git/blob - tests/ui/mut_reference.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[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 #![allow(unused_variables, clippy::trivially_copy_pass_by_ref)]
11
12 fn takes_an_immutable_reference(a: &i32) {}
13 fn takes_a_mutable_reference(a: &mut i32) {}
14
15 struct MyStruct;
16
17 impl MyStruct {
18     fn takes_an_immutable_reference(&self, a: &i32) {}
19
20     fn takes_a_mutable_reference(&self, a: &mut i32) {}
21 }
22
23 #[warn(clippy::unnecessary_mut_passed)]
24 fn main() {
25     // Functions
26     takes_an_immutable_reference(&mut 42);
27     let as_ptr: fn(&i32) = takes_an_immutable_reference;
28     as_ptr(&mut 42);
29
30     // Methods
31     let my_struct = MyStruct;
32     my_struct.takes_an_immutable_reference(&mut 42);
33
34     // No error
35
36     // Functions
37     takes_an_immutable_reference(&42);
38     let as_ptr: fn(&i32) = takes_an_immutable_reference;
39     as_ptr(&42);
40
41     takes_a_mutable_reference(&mut 42);
42     let as_ptr: fn(&mut i32) = takes_a_mutable_reference;
43     as_ptr(&mut 42);
44
45     let a = &mut 42;
46     takes_an_immutable_reference(a);
47
48     // Methods
49     my_struct.takes_an_immutable_reference(&42);
50     my_struct.takes_a_mutable_reference(&mut 42);
51     my_struct.takes_an_immutable_reference(a);
52 }