Skip to content

rust/clippy: disable redundant_clone #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ if(NOT CMAKE_CROSSCOMPILING)
-A clippy::new_without_default
-A clippy::single_match
-A clippy::iter_nth_zero
-A clippy::redundant_clone

)
add_dependencies(rust-clippy rust-bindgen)
Expand Down
2 changes: 1 addition & 1 deletion src/rust/bitbox02-rust-c/src/bitboxbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub extern "C" fn bitboxbase_state_set_not_alive() {
#[no_mangle]
pub extern "C" fn bitboxbase_state_get() -> BitBoxBaseBackgroundState {
let state = unsafe { &STATE };
state.state.clone()
state.state
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NickeZ clippy warned this

https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy

Sounds legit, do you remember why there was a clone() here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, can't remember. Usually I add clone()s as temporary fixes to make the borrowchecker happy. Having many clones is a bit of a code smell.

}

/// # Safety
Expand Down
3 changes: 3 additions & 0 deletions src/rust/bitbox02-rust-c/src/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub extern "C" fn rust_sha256_new() -> *mut c_void {
#[no_mangle]
pub unsafe extern "C" fn rust_sha256_update(ctx: *mut c_void, data: *const c_void, len: usize) {
let data = core::slice::from_raw_parts(data as *const u8, len);
#[allow(clippy::cast_ptr_alignment)] // ctx is properly aligned, see `Box::into_raw`.
let ctx = ctx as *mut Sha256;
(*ctx).input(data);
}
Expand All @@ -42,6 +43,7 @@ pub unsafe extern "C" fn rust_sha256_update(ctx: *mut c_void, data: *const c_voi
#[no_mangle]
pub unsafe extern "C" fn rust_sha256_finish(ctx: *mut *mut c_void, out: *mut c_uchar) {
let out = core::slice::from_raw_parts_mut(out, 32);
#[allow(clippy::cast_ptr_alignment)] // ctx is properly aligned, see `Box::into_raw`.
let hasher = Box::from_raw(*ctx as *mut Sha256); // dropped at the end
let hash = hasher.result();
out.copy_from_slice(&hash[..]);
Expand All @@ -54,6 +56,7 @@ pub unsafe extern "C" fn rust_sha256_finish(ctx: *mut *mut c_void, out: *mut c_u
#[no_mangle]
pub unsafe extern "C" fn rust_sha256_free(ctx: *mut *mut c_void) {
if !(*ctx).is_null() {
#[allow(clippy::cast_ptr_alignment)] // ctx is properly aligned, see `Box::into_raw`.
Box::from_raw(*ctx as *mut Sha256);
*ctx = core::ptr::null_mut();
}
Expand Down
38 changes: 21 additions & 17 deletions src/rust/bitbox02-rust-c/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ impl AsRef<[u8]> for Bytes {
/// Create a slice to a buffer. Only allowed for non-null pointers with length or null pointers
/// with 0 length due to limitation in `core::slice`.
fn as_ref(&self) -> &[u8] {
let mut buf = self.buf;
if self.len == 0 && self.buf.is_null() {
buf = core::ptr::NonNull::dangling().as_ptr();
}
let buf = if self.len == 0 && self.buf.is_null() {
core::ptr::NonNull::dangling().as_ptr()
} else {
self.buf
};
assert!(!buf.is_null());
unsafe { core::slice::from_raw_parts(buf, self.len) }
}
Expand All @@ -83,10 +84,11 @@ impl AsRef<[u8]> for BytesMut {
/// Create a slice to a buffer. Only allowed for non-null pointers with length or null pointers
/// with 0 length due to limitation in `core::slice`.
fn as_ref(&self) -> &[u8] {
let mut buf = self.buf;
if self.len == 0 && self.buf.is_null() {
buf = core::ptr::NonNull::dangling().as_ptr();
}
let buf = if self.len == 0 && self.buf.is_null() {
core::ptr::NonNull::dangling().as_ptr()
} else {
self.buf
};
assert!(!buf.is_null());
unsafe { core::slice::from_raw_parts(buf, self.len) }
}
Expand All @@ -96,10 +98,11 @@ impl AsMut<[u8]> for BytesMut {
/// Create a slice to a buffer. Only allowed for non-null pointers with length or null pointers
/// with 0 length due to limitation in `core::slice`.
fn as_mut(&mut self) -> &mut [u8] {
let mut buf = self.buf;
if self.len == 0 && self.buf.is_null() {
buf = core::ptr::NonNull::dangling().as_ptr();
}
let buf = if self.len == 0 && self.buf.is_null() {
core::ptr::NonNull::dangling().as_ptr()
} else {
self.buf
};
assert!(!buf.is_null());
unsafe { core::slice::from_raw_parts_mut(buf, self.len) }
}
Expand All @@ -116,19 +119,20 @@ impl CStr {
/// Create a CStr from a null-terminated string or null pointer. Unsafe because it will read
/// until it finds a null character.
pub unsafe fn new(buf: *const c_char) -> Self {
let mut buf = buf;
let mut len = 0;
if buf.is_null() {
buf = core::ptr::NonNull::dangling().as_ptr();
len = 0;
CStr {
buf: core::ptr::NonNull::dangling().as_ptr(),
len: 0,
}
} else {
let mut len = 0;
let mut b = buf;
while b.read() != 0 {
len += 1;
b = b.offset(1);
}
CStr { buf, len }
}
CStr { buf, len }
}
}

Expand Down