summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/assertions_on_result_states.fixed
blob: 795f435f24cd40b3e713959334ff266aaba3cc9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// run-rustfix
#![warn(clippy::assertions_on_result_states)]

use std::result::Result;

struct Foo;

#[derive(Debug)]
struct DebugFoo;

#[derive(Copy, Clone, Debug)]
struct CopyFoo;

macro_rules! get_ok_macro {
    () => {
        Ok::<_, DebugFoo>(Foo)
    };
}

fn main() {
    // test ok
    let r: Result<Foo, DebugFoo> = Ok(Foo);
    debug_assert!(r.is_ok());
    r.unwrap();

    // test ok with non-debug error type
    let r: Result<Foo, Foo> = Ok(Foo);
    assert!(r.is_ok());

    // test ok with some messages
    let r: Result<Foo, DebugFoo> = Ok(Foo);
    assert!(r.is_ok(), "oops");

    // test ok with unit error
    let r: Result<Foo, ()> = Ok(Foo);
    assert!(r.is_ok());

    // test temporary ok
    fn get_ok() -> Result<Foo, DebugFoo> {
        Ok(Foo)
    }
    get_ok().unwrap();

    // test macro ok
    get_ok_macro!().unwrap();

    // test ok that shouldn't be moved
    let r: Result<CopyFoo, DebugFoo> = Ok(CopyFoo);
    fn test_ref_unmoveable_ok(r: &Result<CopyFoo, DebugFoo>) {
        assert!(r.is_ok());
    }
    test_ref_unmoveable_ok(&r);
    assert!(r.is_ok());
    r.unwrap();

    // test ok that is copied
    let r: Result<CopyFoo, CopyFoo> = Ok(CopyFoo);
    r.unwrap();
    r.unwrap();

    // test reference to ok
    let r: Result<CopyFoo, CopyFoo> = Ok(CopyFoo);
    fn test_ref_copy_ok(r: &Result<CopyFoo, CopyFoo>) {
        r.unwrap();
    }
    test_ref_copy_ok(&r);
    r.unwrap();

    // test err
    let r: Result<DebugFoo, Foo> = Err(Foo);
    debug_assert!(r.is_err());
    r.unwrap_err();

    // test err with non-debug value type
    let r: Result<Foo, Foo> = Err(Foo);
    assert!(r.is_err());
}