]> git.lizzy.rs Git - rust.git/blob - tests/ui/drop_forget_ref.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / drop_forget_ref.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 #![warn(clippy::drop_ref, clippy::forget_ref)]
11 #![allow(clippy::toplevel_ref_arg, clippy::similar_names, clippy::needless_pass_by_value)]
12
13 use std::mem::{drop, forget};
14
15 struct SomeStruct;
16
17 fn main() {
18     drop(&SomeStruct);
19     forget(&SomeStruct);
20
21     let mut owned1 = SomeStruct;
22     drop(&owned1);
23     drop(&&owned1);
24     drop(&mut owned1);
25     drop(owned1); //OK
26     let mut owned2 = SomeStruct;
27     forget(&owned2);
28     forget(&&owned2);
29     forget(&mut owned2);
30     forget(owned2); //OK
31
32     let reference1 = &SomeStruct;
33     drop(reference1);
34     forget(&*reference1);
35
36     let reference2 = &mut SomeStruct;
37     drop(reference2);
38     let reference3 = &mut SomeStruct;
39     forget(reference3);
40
41     let ref reference4 = SomeStruct;
42     drop(reference4);
43     forget(reference4);
44 }
45
46 #[allow(dead_code)]
47 fn test_generic_fn_drop<T>(val: T) {
48     drop(&val);
49     drop(val); //OK
50 }
51
52 #[allow(dead_code)]
53 fn test_generic_fn_forget<T>(val: T) {
54     forget(&val);
55     forget(val); //OK
56 }
57
58 #[allow(dead_code)]
59 fn test_similarly_named_function() {
60     fn drop<T>(_val: T) {}
61     drop(&SomeStruct); //OK; call to unrelated function which happens to have the same name
62     std::mem::drop(&SomeStruct);
63     fn forget<T>(_val: T) {}
64     forget(&SomeStruct); //OK; call to unrelated function which happens to have the same name
65     std::mem::forget(&SomeStruct);
66 }