]> git.lizzy.rs Git - rust.git/blob - tests/ui/drop_forget_ref.rs
Auto merge of #4314 - chansuke:add-negation-to-is_empty, r=flip1995
[rust.git] / tests / ui / drop_forget_ref.rs
1 #![warn(clippy::drop_ref, clippy::forget_ref)]
2 #![allow(clippy::toplevel_ref_arg, clippy::similar_names, clippy::needless_pass_by_value)]
3
4 use std::mem::{drop, forget};
5
6 struct SomeStruct;
7
8 fn main() {
9     drop(&SomeStruct);
10     forget(&SomeStruct);
11
12     let mut owned1 = SomeStruct;
13     drop(&owned1);
14     drop(&&owned1);
15     drop(&mut owned1);
16     drop(owned1); //OK
17     let mut owned2 = SomeStruct;
18     forget(&owned2);
19     forget(&&owned2);
20     forget(&mut owned2);
21     forget(owned2); //OK
22
23     let reference1 = &SomeStruct;
24     drop(reference1);
25     forget(&*reference1);
26
27     let reference2 = &mut SomeStruct;
28     drop(reference2);
29     let reference3 = &mut SomeStruct;
30     forget(reference3);
31
32     let ref reference4 = SomeStruct;
33     drop(reference4);
34     forget(reference4);
35 }
36
37 #[allow(dead_code)]
38 fn test_generic_fn_drop<T>(val: T) {
39     drop(&val);
40     drop(val); //OK
41 }
42
43 #[allow(dead_code)]
44 fn test_generic_fn_forget<T>(val: T) {
45     forget(&val);
46     forget(val); //OK
47 }
48
49 #[allow(dead_code)]
50 fn test_similarly_named_function() {
51     fn drop<T>(_val: T) {}
52     drop(&SomeStruct); //OK; call to unrelated function which happens to have the same name
53     std::mem::drop(&SomeStruct);
54     fn forget<T>(_val: T) {}
55     forget(&SomeStruct); //OK; call to unrelated function which happens to have the same name
56     std::mem::forget(&SomeStruct);
57 }
58
59 #[derive(Copy, Clone)]
60 pub struct Error;
61 fn produce_half_owl_error() -> Result<(), Error> {
62     Ok(())
63 }
64
65 fn produce_half_owl_ok() -> Result<bool, ()> {
66     Ok(true)
67 }
68
69 #[allow(dead_code)]
70 fn test_owl_result() -> Result<(), ()> {
71     produce_half_owl_error().map_err(|_| ())?;
72     produce_half_owl_ok().map(|_| ())?;
73     // the following should not be linted,
74     // we should not force users to use toilet closures
75     // to produce owl results when drop is more convenient
76     produce_half_owl_error().map_err(drop)?;
77     produce_half_owl_ok().map_err(drop)?;
78     Ok(())
79 }
80
81 #[allow(dead_code)]
82 fn test_owl_result_2() -> Result<u8, ()> {
83     produce_half_owl_error().map_err(|_| ())?;
84     produce_half_owl_ok().map(|_| ())?;
85     // the following should not be linted,
86     // we should not force users to use toilet closures
87     // to produce owl results when drop is more convenient
88     produce_half_owl_error().map_err(drop)?;
89     produce_half_owl_ok().map(drop)?;
90     Ok(1)
91 }