Fix Windows clippy issues (#5593)
These have been a lil bit annoying when running `cargo clippy --all-features --all-targets` on windows
This commit is contained in:
parent
f0d7c74e83
commit
1339639706
|
|
@ -58,27 +58,34 @@ fn roaming_appdata() -> Option<PathBuf> {
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn wcslen(buf: *const u16) -> usize;
|
fn wcslen(buf: *const u16) -> usize;
|
||||||
}
|
}
|
||||||
unsafe {
|
let mut path_raw = ptr::null_mut();
|
||||||
let mut path = ptr::null_mut();
|
|
||||||
match SHGetKnownFolderPath(
|
// SAFETY: SHGetKnownFolderPath allocates for us, we don't pass any pointers to it.
|
||||||
|
// See https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath
|
||||||
|
let result = unsafe {
|
||||||
|
SHGetKnownFolderPath(
|
||||||
&FOLDERID_RoamingAppData,
|
&FOLDERID_RoamingAppData,
|
||||||
KF_FLAG_DONT_VERIFY as u32,
|
KF_FLAG_DONT_VERIFY as u32,
|
||||||
std::ptr::null_mut(),
|
std::ptr::null_mut(),
|
||||||
&mut path,
|
&mut path_raw,
|
||||||
) {
|
)
|
||||||
S_OK => {
|
};
|
||||||
let path_slice = slice::from_raw_parts(path, wcslen(path));
|
|
||||||
let s = OsString::from_wide(&path_slice);
|
let path = if result == S_OK {
|
||||||
CoTaskMemFree(path.cast());
|
// SAFETY: SHGetKnownFolderPath indicated success and is supposed to allocate a nullterminated string for us.
|
||||||
Some(PathBuf::from(s))
|
let path_slice = unsafe { slice::from_raw_parts(path_raw, wcslen(path_raw)) };
|
||||||
}
|
Some(PathBuf::from(OsString::from_wide(path_slice)))
|
||||||
_ => {
|
} else {
|
||||||
// Free any allocated memory even on failure. A null ptr is a no-op for `CoTaskMemFree`.
|
|
||||||
CoTaskMemFree(path.cast());
|
|
||||||
None
|
None
|
||||||
}
|
};
|
||||||
}
|
|
||||||
}
|
// SAFETY:
|
||||||
|
// This memory got allocated by SHGetKnownFolderPath, we didn't touch anything in the process.
|
||||||
|
// A null ptr is a no-op for `CoTaskMemFree`, so in case this failed we're still good.
|
||||||
|
// https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cotaskmemfree
|
||||||
|
unsafe { CoTaskMemFree(path_raw.cast()) };
|
||||||
|
|
||||||
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(not(windows), target_vendor = "uwp"))]
|
#[cfg(any(not(windows), target_vendor = "uwp"))]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue