]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/option_option.rs
Rollup merge of #78216 - workingjubilee:duration-zero, r=m-ou-se
[rust.git] / src / tools / clippy / tests / ui / option_option.rs
1 #![deny(clippy::option_option)]
2
3 fn input(_: Option<Option<u8>>) {}
4
5 fn output() -> Option<Option<u8>> {
6     None
7 }
8
9 fn output_nested() -> Vec<Option<Option<u8>>> {
10     vec![None]
11 }
12
13 // The lint only generates one warning for this
14 fn output_nested_nested() -> Option<Option<Option<u8>>> {
15     None
16 }
17
18 struct Struct {
19     x: Option<Option<u8>>,
20 }
21
22 impl Struct {
23     fn struct_fn() -> Option<Option<u8>> {
24         None
25     }
26 }
27
28 trait Trait {
29     fn trait_fn() -> Option<Option<u8>>;
30 }
31
32 enum Enum {
33     Tuple(Option<Option<u8>>),
34     Struct { x: Option<Option<u8>> },
35 }
36
37 // The lint allows this
38 type OptionOption = Option<Option<u32>>;
39
40 // The lint allows this
41 fn output_type_alias() -> OptionOption {
42     None
43 }
44
45 // The line allows this
46 impl Trait for Struct {
47     fn trait_fn() -> Option<Option<u8>> {
48         None
49     }
50 }
51
52 fn main() {
53     input(None);
54     output();
55     output_nested();
56
57     // The lint allows this
58     let local: Option<Option<u8>> = None;
59
60     // The lint allows this
61     let expr = Some(Some(true));
62 }
63
64 extern crate serde;
65 mod issue_4298 {
66     use serde::{Deserialize, Deserializer, Serialize};
67     use std::borrow::Cow;
68
69     #[derive(Serialize, Deserialize)]
70     struct Foo<'a> {
71         #[serde(deserialize_with = "func")]
72         #[serde(skip_serializing_if = "Option::is_none")]
73         #[serde(default)]
74         #[serde(borrow)]
75         // FIXME: should not lint here
76         #[allow(clippy::option_option)]
77         foo: Option<Option<Cow<'a, str>>>,
78     }
79
80     #[allow(clippy::option_option)]
81     fn func<'a, D>(_: D) -> Result<Option<Option<Cow<'a, str>>>, D::Error>
82     where
83         D: Deserializer<'a>,
84     {
85         Ok(Some(Some(Cow::Borrowed("hi"))))
86     }
87 }