From 1c3e4648e471e02dfb8f3ea8169bbf157e58c8e8 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Wed, 16 Oct 2024 11:25:22 +0200 Subject: [PATCH] Remove unnecessary null check in FFI exercise (#2432) We only assign `self.dir` once and we only assign it if the pointer is non-null. We can therefore simplify the drop implementation a little. --- src/unsafe-rust/exercise.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/unsafe-rust/exercise.rs b/src/unsafe-rust/exercise.rs index 0eb0cc6c..3cf870e9 100644 --- a/src/unsafe-rust/exercise.rs +++ b/src/unsafe-rust/exercise.rs @@ -123,11 +123,9 @@ impl Drop for DirectoryIterator { fn drop(&mut self) { // Call closedir as needed. // ANCHOR_END: Drop - if !self.dir.is_null() { - // SAFETY: self.dir is not NULL. - if unsafe { ffi::closedir(self.dir) } != 0 { - panic!("Could not close {:?}", self.path); - } + // SAFETY: self.dir is never NULL. + if unsafe { ffi::closedir(self.dir) } != 0 { + panic!("Could not close {:?}", self.path); } } }