From 0e98c82b575c03d757f5fc6558c7bec30df9acc3 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Tue, 22 Apr 2025 18:06:42 +0100 Subject: [PATCH] Deny some rustc lints The unreachable_pub lint is commented out because it seems like a good idea but fixing the errors it causes would cause too much churn for it to be done in this commit. --- src/archive/find.rs | 44 +++++++++++++++++++++------------ src/database/mod.rs | 4 --- src/game.rs | 26 +++++-------------- src/lib.rs | 16 ++++++++++++ src/metadata/plugin_metadata.rs | 2 +- src/plugin/mod.rs | 2 +- 6 files changed, 52 insertions(+), 42 deletions(-) diff --git a/src/archive/find.rs b/src/archive/find.rs index 93f5785c..4c80319c 100644 --- a/src/archive/find.rs +++ b/src/archive/find.rs @@ -1,5 +1,8 @@ use std::path::{Path, PathBuf}; +#[cfg(windows)] +use windows::Win32::Storage::FileSystem::BY_HANDLE_FILE_INFORMATION; + use crate::{GameType, game::GameCache, plugin::has_ascii_extension}; const BSA_FILE_EXTENSION: &str = "bsa"; @@ -126,11 +129,6 @@ fn find_associated_archives_with_arbitrary_suffixes( #[cfg(windows)] fn are_file_paths_equivalent(lhs: &Path, rhs: &Path) -> bool { use std::fs::File; - use std::os::windows::io::AsRawHandle; - use windows::Win32::{ - Foundation::HANDLE, - Storage::FileSystem::{BY_HANDLE_FILE_INFORMATION, GetFileInformationByHandle}, - }; if lhs == rhs { return true; @@ -144,24 +142,38 @@ fn are_file_paths_equivalent(lhs: &Path, rhs: &Path) -> bool { return false; }; - let mut lhs_info = BY_HANDLE_FILE_INFORMATION::default(); - let mut rhs_info = BY_HANDLE_FILE_INFORMATION::default(); - // SAFETY: This is safe because the file handles and the info struct pointers are all valid until this function exits. - unsafe { - if GetFileInformationByHandle(HANDLE(lhs_file.as_raw_handle()), &mut lhs_info).is_err() { - return false; - } + let Some(lhs_info) = get_file_info(&lhs_file) else { + return false; + }; - if GetFileInformationByHandle(HANDLE(rhs_file.as_raw_handle()), &mut rhs_info).is_err() { - return false; - } - } + let Some(rhs_info) = get_file_info(&rhs_file) else { + return false; + }; lhs_info.dwVolumeSerialNumber == rhs_info.dwVolumeSerialNumber && lhs_info.nFileIndexHigh == rhs_info.nFileIndexHigh && lhs_info.nFileIndexLow == rhs_info.nFileIndexLow } +#[cfg(windows)] +fn get_file_info(file: &std::fs::File) -> Option { + use std::os::windows::io::AsRawHandle; + use windows::Win32::{Foundation::HANDLE, Storage::FileSystem::GetFileInformationByHandle}; + + let mut info = BY_HANDLE_FILE_INFORMATION::default(); + + // SAFETY: This is safe because the file handles and the info struct pointers are all valid until this function exits. + #[expect( + unsafe_code, + reason = "There is currently no way to get this data safely" + )] + unsafe { + GetFileInformationByHandle(HANDLE(file.as_raw_handle()), &mut info) + .is_ok() + .then_some(info) + } +} + #[cfg(not(windows))] fn are_file_paths_equivalent(lhs: &Path, rhs: &Path) -> bool { use std::os::unix::fs::MetadataExt; diff --git a/src/database/mod.rs b/src/database/mod.rs index 8897b772..25f514cb 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -602,8 +602,6 @@ plugins: } mod write_minimal_list { - use crate::tests::{BLANK_DIFFERENT_ESM, BLANK_ESM}; - use super::*; #[test] @@ -964,8 +962,6 @@ plugins: } mod plugin_metadata { - use crate::tests::BLANK_ESM; - use super::*; #[test] diff --git a/src/game.rs b/src/game.rs index bc5f848b..0879e4e1 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1176,11 +1176,6 @@ mod tests { mod set_additional_data_paths { use std::time::{Duration, SystemTime}; - use crate::{ - metadata::{File, PluginMetadata}, - tests::{BLANK_ESM, BLANK_ESP}, - }; - use super::*; #[test] @@ -1274,9 +1269,7 @@ mod tests { mod is_valid_plugin { use super::*; - use crate::tests::{ - BLANK_ESM, BLANK_MASTER_DEPENDENT_ESM, NON_ASCII_ESM, NON_PLUGIN_FILE, - }; + use crate::tests::{NON_ASCII_ESM, NON_PLUGIN_FILE}; #[apply(all_game_types)] fn should_return_true_for_a_valid_non_ascii_plugin(game_type: GameType) { @@ -1310,7 +1303,7 @@ mod tests { .unwrap(); let plugin = fixture.data_path().join("empty.omwscripts"); - let _ = std::fs::File::create(&plugin).unwrap(); + std::fs::File::create(&plugin).unwrap(); if game_type == GameType::OpenMW { assert!(game.is_valid_plugin(&plugin)); @@ -1345,7 +1338,7 @@ mod tests { .unwrap(); let empty_file_path = fixture.data_path().join("empty.esp"); - let _ = std::fs::File::create(&empty_file_path).unwrap(); + std::fs::File::create(&empty_file_path).unwrap(); assert!(!game.is_valid_plugin(&empty_file_path)); } @@ -1416,7 +1409,7 @@ mod tests { } mod load_plugin_headers { - use crate::tests::{BLANK_DIFFERENT_ESM, BLANK_ESM, BLANK_ESP, NON_PLUGIN_FILE}; + use crate::tests::NON_PLUGIN_FILE; use super::*; @@ -1517,10 +1510,7 @@ mod tests { mod load_plugins { use std::error::Error; - use crate::tests::{ - BLANK_DIFFERENT_ESM, BLANK_ESM, BLANK_ESP, BLANK_FULL_ESM, - BLANK_MASTER_DEPENDENT_ESM, - }; + use crate::tests::BLANK_FULL_ESM; use super::*; @@ -1692,8 +1682,6 @@ mod tests { } mod load_plugins_common { - use crate::tests::{BLANK_ESM, BLANK_MASTER_DEPENDENT_ESM}; - use super::*; #[apply(all_game_types)] @@ -1896,7 +1884,7 @@ mod tests { } mod sort_plugins { - use crate::tests::{BLANK_DIFFERENT_ESP, BLANK_ESP, initial_load_order}; + use crate::tests::initial_load_order; use super::*; @@ -1958,8 +1946,6 @@ mod tests { } mod is_plugin_active { - use crate::tests::BLANK_ESP; - use super::*; #[test] diff --git a/src/lib.rs b/src/lib.rs index 2d019c77..9fcd0fa7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,19 @@ +// Deny some rustc lints that are allow-by-default. +#![deny( + ambiguous_negative_literals, + impl_trait_overcaptures, + let_underscore_drop, + missing_copy_implementations, + missing_debug_implementations, + non_ascii_idents, + redundant_imports, + redundant_lifetimes, + trivial_casts, + trivial_numeric_casts, + unit_bindings, + // unreachable_pub, + unsafe_code +)] #![deny(clippy::pedantic)] // Allow a few clippy pedantic lints. #![allow(clippy::doc_markdown)] diff --git a/src/metadata/plugin_metadata.rs b/src/metadata/plugin_metadata.rs index f60265bd..189152c7 100644 --- a/src/metadata/plugin_metadata.rs +++ b/src/metadata/plugin_metadata.rs @@ -1017,7 +1017,7 @@ mod tests { mod emit_yaml { use super::*; - use crate::metadata::{MessageType, TagSuggestion, emit}; + use crate::metadata::emit; #[test] fn should_omit_group_if_not_set() { diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index 6131ec6f..02f5f2a2 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -705,7 +705,7 @@ mod tests { std::fs::copy(data_path.join(blank_esm(game_type)), &omwgame).unwrap(); std::fs::copy(data_path.join(BLANK_ESP), &omwaddon).unwrap(); - let _ = File::create(&omwscripts).unwrap(); + File::create(&omwscripts).unwrap(); assert!( Plugin::new(