summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/assertions_on_result_states.fixed
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/assertions_on_result_states.fixed
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/assertions_on_result_states.fixed')
-rw-r--r--src/tools/clippy/tests/ui/assertions_on_result_states.fixed69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/assertions_on_result_states.fixed b/src/tools/clippy/tests/ui/assertions_on_result_states.fixed
new file mode 100644
index 000000000..7bde72e4b
--- /dev/null
+++ b/src/tools/clippy/tests/ui/assertions_on_result_states.fixed
@@ -0,0 +1,69 @@
+// 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 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());
+}