]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/assertions_on_result_states.rs
Merge commit '2b2190cb5667cdd276a24ef8b9f3692209c54a89' into clippyup
[rust.git] / src / tools / clippy / tests / ui / assertions_on_result_states.rs
1 // run-rustfix
2 #![warn(clippy::assertions_on_result_states)]
3
4 use std::result::Result;
5
6 struct Foo;
7
8 #[derive(Debug)]
9 struct DebugFoo;
10
11 #[derive(Copy, Clone, Debug)]
12 struct CopyFoo;
13
14 macro_rules! get_ok_macro {
15     () => {
16         Ok::<_, DebugFoo>(Foo)
17     };
18 }
19
20 fn main() {
21     // test ok
22     let r: Result<Foo, DebugFoo> = Ok(Foo);
23     debug_assert!(r.is_ok());
24     assert!(r.is_ok());
25
26     // test ok with non-debug error type
27     let r: Result<Foo, Foo> = Ok(Foo);
28     assert!(r.is_ok());
29
30     // test ok with some messages
31     let r: Result<Foo, DebugFoo> = Ok(Foo);
32     assert!(r.is_ok(), "oops");
33
34     // test ok with unit error
35     let r: Result<Foo, ()> = Ok(Foo);
36     assert!(r.is_ok());
37
38     // test temporary ok
39     fn get_ok() -> Result<Foo, DebugFoo> {
40         Ok(Foo)
41     }
42     assert!(get_ok().is_ok());
43
44     // test macro ok
45     assert!(get_ok_macro!().is_ok());
46
47     // test ok that shouldn't be moved
48     let r: Result<CopyFoo, DebugFoo> = Ok(CopyFoo);
49     fn test_ref_unmoveable_ok(r: &Result<CopyFoo, DebugFoo>) {
50         assert!(r.is_ok());
51     }
52     test_ref_unmoveable_ok(&r);
53     assert!(r.is_ok());
54     r.unwrap();
55
56     // test ok that is copied
57     let r: Result<CopyFoo, CopyFoo> = Ok(CopyFoo);
58     assert!(r.is_ok());
59     r.unwrap();
60
61     // test reference to ok
62     let r: Result<CopyFoo, CopyFoo> = Ok(CopyFoo);
63     fn test_ref_copy_ok(r: &Result<CopyFoo, CopyFoo>) {
64         assert!(r.is_ok());
65     }
66     test_ref_copy_ok(&r);
67     r.unwrap();
68
69     // test err
70     let r: Result<DebugFoo, Foo> = Err(Foo);
71     debug_assert!(r.is_err());
72     assert!(r.is_err());
73
74     // test err with non-debug value type
75     let r: Result<Foo, Foo> = Err(Foo);
76     assert!(r.is_err());
77 }