about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/match_as_ref.rs
blob: 049928167901a8c38b64eb8ef4b6a2cfc0c37219 (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
78
79
80
81
82
83
84
85
#![allow(unused)]
#![warn(clippy::match_as_ref)]

fn match_as_ref() {
    let owned: Option<()> = None;
    let borrowed: Option<&()> = match owned {
        //~^ match_as_ref
        None => None,
        Some(ref v) => Some(v),
    };

    let mut mut_owned: Option<()> = None;
    let borrow_mut: Option<&mut ()> = match mut_owned {
        //~^ match_as_ref
        None => None,
        Some(ref mut v) => Some(v),
    };
}

mod issue4437 {
    use std::error::Error;
    use std::fmt;
    use std::num::ParseIntError;

    #[derive(Debug)]
    struct E {
        source: Option<ParseIntError>,
    }

    impl Error for E {
        fn source(&self) -> Option<&(dyn Error + 'static)> {
            match self.source {
                //~^ match_as_ref
                Some(ref s) => Some(s),
                None => None,
            }
        }
    }

    impl fmt::Display for E {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            unimplemented!()
        }
    }
}

fn main() {
    // Don't lint
    let _ = match Some(0) {
        #[cfg(feature = "foo")]
        Some(ref x) if *x > 50 => None,
        Some(ref x) => Some(x),
        None => None,
    };
}

mod issue15691 {
    use std::ops::{Deref, DerefMut};

    struct A(B);
    struct B;

    impl Deref for A {
        type Target = B;
        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    impl DerefMut for A {
        fn deref_mut(&mut self) -> &mut Self::Target {
            &mut self.0
        }
    }

    fn func() {
        let mut a = Some(A(B));
        let mut b = Some(B);
        // Do not lint, we don't have `None => None`
        let _ = match b {
            Some(ref mut x) => Some(x),
            None => a.as_deref_mut(),
        };
    }
}