Skip to content

Commit

Permalink
Merge pull request #343 from nbdd0121/warnings
Browse files Browse the repository at this point in the history
Fix warnings in str.rs and binding_generated.rs
  • Loading branch information
ojeda authored Jun 4, 2021
2 parents f9dc4fc + 14f1c7c commit c82b757
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions rust/kernel/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,14 @@ impl CStr {
/// must not be mutated.
#[inline]
pub unsafe fn from_char_ptr<'a>(ptr: *const c_types::c_char) -> &'a Self {
// SAFETY: The safety precondition guarantees `ptr` is a valid pointer
// to a `NUL`-terminated C string.
let len = unsafe { bindings::strlen(ptr) } + 1;
unsafe {
Self::from_bytes_with_nul_unchecked(core::slice::from_raw_parts(ptr as _, len as _))
}
// SAFETY: Lifetime guaranteed by the safety precondition.
let bytes = unsafe { core::slice::from_raw_parts(ptr as _, len as _) };
// SAFETY: As `len` is returned by `strlen`, `bytes` does not contain interior `NUL`.
// As we have added 1 to `len`, the last byte is known to be `NUL`.
unsafe { Self::from_bytes_with_nul_unchecked(bytes) }
}

/// Creates a [`CStr`] from a `[u8]`.
Expand Down Expand Up @@ -146,6 +150,7 @@ impl CStr {
// requires `ptr_metadata`).
// While none of them are current stable, it is very likely that one of
// them will eventually be.
// SAFETY: Properties of `bytes` guaranteed by the safety precondition.
unsafe { &*(bytes as *const [u8] as *const Self) }
}

Expand Down Expand Up @@ -188,11 +193,10 @@ impl Index<ops::RangeFrom<usize>> for CStr {
type Output = CStr;

#[inline]
// Clippy false positive
#[allow(clippy::unnecessary_operation)]
fn index(&self, index: ops::RangeFrom<usize>) -> &Self::Output {
// Delegate bounds checking to slice.
&self.as_bytes()[index.start..];
// Assign to _ to mute clippy's unnecessary operation warning.
let _ = &self.as_bytes()[index.start..];
// SAFETY: We just checked the bounds.
unsafe { Self::from_bytes_with_nul_unchecked(&self.0[index.start..]) }
}
Expand Down

0 comments on commit c82b757

Please sign in to comment.