summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/option_option.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/tools/clippy/tests/ui/option_option.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/clippy/tests/ui/option_option.rs')
-rw-r--r--src/tools/clippy/tests/ui/option_option.rs89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/option_option.rs b/src/tools/clippy/tests/ui/option_option.rs
new file mode 100644
index 000000000..2faab9e03
--- /dev/null
+++ b/src/tools/clippy/tests/ui/option_option.rs
@@ -0,0 +1,89 @@
+#![deny(clippy::option_option)]
+#![allow(clippy::unnecessary_wraps)]
+
+const C: Option<Option<i32>> = None;
+static S: Option<Option<i32>> = None;
+
+fn input(_: Option<Option<u8>>) {}
+
+fn output() -> Option<Option<u8>> {
+ None
+}
+
+fn output_nested() -> Vec<Option<Option<u8>>> {
+ vec![None]
+}
+
+// The lint only generates one warning for this
+fn output_nested_nested() -> Option<Option<Option<u8>>> {
+ None
+}
+
+struct Struct {
+ x: Option<Option<u8>>,
+}
+
+impl Struct {
+ fn struct_fn() -> Option<Option<u8>> {
+ None
+ }
+}
+
+trait Trait {
+ fn trait_fn() -> Option<Option<u8>>;
+}
+
+enum Enum {
+ Tuple(Option<Option<u8>>),
+ Struct { x: Option<Option<u8>> },
+}
+
+// The lint allows this
+type OptionOption = Option<Option<u32>>;
+
+// The lint allows this
+fn output_type_alias() -> OptionOption {
+ None
+}
+
+// The line allows this
+impl Trait for Struct {
+ fn trait_fn() -> Option<Option<u8>> {
+ None
+ }
+}
+
+fn main() {
+ input(None);
+ output();
+ output_nested();
+
+ // The lint allows this
+ let local: Option<Option<u8>> = None;
+
+ // The lint allows this
+ let expr = Some(Some(true));
+}
+
+extern crate serde;
+mod issue_4298 {
+ use serde::{Deserialize, Deserializer, Serialize};
+ use std::borrow::Cow;
+
+ #[derive(Serialize, Deserialize)]
+ struct Foo<'a> {
+ #[serde(deserialize_with = "func")]
+ #[serde(skip_serializing_if = "Option::is_none")]
+ #[serde(default)]
+ #[serde(borrow)]
+ foo: Option<Option<Cow<'a, str>>>,
+ }
+
+ #[allow(clippy::option_option)]
+ fn func<'a, D>(_: D) -> Result<Option<Option<Cow<'a, str>>>, D::Error>
+ where
+ D: Deserializer<'a>,
+ {
+ Ok(Some(Some(Cow::Borrowed("hi"))))
+ }
+}