Skip to content

Commit a72e93f

Browse files
committed
Merge branch 'clippy'
2 parents bb6d619 + 70b7f81 commit a72e93f

File tree

15 files changed

+72
-39
lines changed

15 files changed

+72
-39
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ run-unit-tests: | build-build
9191
$(MAKE) -C build-build test
9292
run-rust-unit-tests:
9393
${MAKE} -C build-build rust-test
94+
run-rust-clippy:
95+
${MAKE} -C build-build rust-clippy
9496
# Must run tests before creating coverage report
9597
coverage: | build-build
9698
${MAKE} -C build-build coverage

src/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,33 @@ if(NOT CMAKE_CROSSCOMPILING)
401401
${CARGO} test --all-features --manifest-path ${CMAKE_CURRENT_SOURCE_DIR}/rust/Cargo.toml --target-dir ${RUST_BINARY_DIR} ${RUST_CARGO_FLAG}
402402
)
403403
add_dependencies(rust-test rust-bindgen)
404+
405+
add_custom_target(rust-clippy
406+
COMMAND
407+
# Force clippy to fully re-run. It is bad at figuring out when to run again and when to use caches.
408+
${CARGO} clean --manifest-path ${CMAKE_CURRENT_SOURCE_DIR}/rust/Cargo.toml --target-dir ${RUST_BINARY_DIR}
409+
COMMAND
410+
${CMAKE_COMMAND} -E env
411+
CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
412+
# enable nightly features in the stable release - needed to activate alloc_error_handler.
413+
# see src/rust/bitbox02-rust-c/src/lib.rs.
414+
# https://github.com/rust-lang/rust/issues/66740
415+
RUSTC_BOOTSTRAP=1
416+
${CARGO} clippy
417+
--all-features
418+
--manifest-path ${CMAKE_CURRENT_SOURCE_DIR}/rust/Cargo.toml
419+
--target-dir ${RUST_BINARY_DIR}
420+
--release
421+
-- # disabled linters:
422+
-A clippy::large_enum_variant
423+
-A clippy::identity_op
424+
-A clippy::match_bool
425+
-A clippy::new_without_default
426+
-A clippy::single_match
427+
-A clippy::iter_nth_zero
428+
429+
)
430+
add_dependencies(rust-clippy rust-bindgen)
404431
endif()
405432

406433
# If a bootloader that locks the bootloader is flashed the bootloader area is permanently read-only.

src/rust/apps/ethereum/src/keypath.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static ACCOUNT_MAX: u32 = 99; // 100 accounts
2222
/// xpub keypath.
2323
/// @return true if the keypath is valid, false if it is invalid.
2424
pub fn is_valid_keypath_xpub(keypath: &[u32], expected_coin: u32) -> bool {
25-
keypath.len() == 4 && &keypath[..4] == &[44 + HARDENED, expected_coin, 0 + HARDENED, 0]
25+
keypath.len() == 4 && keypath[..4] == [44 + HARDENED, expected_coin, 0 + HARDENED, 0]
2626
}
2727

2828
/// Does limit checks the keypath, whitelisting bip44 purpose, account and change.

src/rust/bitbox02-noise/src/noise_xx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<R: Random32> State<R> {
9393
/// `generate_static_private_key()`.
9494
pub fn init(&mut self, static_private_key: Sensitive<PrivateKey>) {
9595
let hs = HandshakeState::new(
96-
noise_protocol::patterns::noise_xx().clone(),
96+
noise_protocol::patterns::noise_xx(),
9797
false, /* is_initiator = false; the app is the initiator */
9898
&b"Noise_XX_25519_ChaChaPoly_SHA256"[..],
9999
Some(static_private_key),

src/rust/bitbox02-rust-c/src/app_ethereum.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
use super::util::{Bytes, CStrMut};
1616

17+
/// # Safety
18+
/// `keypath` must be not NULL and contain `keypath_len` u32 elements.
1719
#[no_mangle]
1820
pub unsafe extern "C" fn rust_ethereum_keypath_is_valid_xpub(
1921
keypath: *const u32,
@@ -24,6 +26,8 @@ pub unsafe extern "C" fn rust_ethereum_keypath_is_valid_xpub(
2426
ethereum::keypath::is_valid_keypath_xpub(keypath, expected_coin)
2527
}
2628

29+
/// # Safety
30+
/// `keypath` must be not NULL and contain `keypath_len` u32 elements.
2731
#[no_mangle]
2832
pub unsafe extern "C" fn rust_ethereum_keypath_is_valid_address(
2933
keypath: *const u32,

src/rust/bitbox02-rust-c/src/bitboxbase.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#[allow(non_camel_case_types)]
2323
type c_char = u8;
2424

25-
use bitbox02;
2625
use core::time::Duration;
2726

2827
use bitbox02_rust::platform::bitboxbase::config::Config;
@@ -141,29 +140,27 @@ pub fn display_status(
141140
bitbox02::COMMANDER_OK
142141
}
143142

143+
/// # Safety
144+
/// request must be not NULL.
144145
#[no_mangle]
145-
pub extern "C" fn commander_bitboxbase(
146+
pub unsafe extern "C" fn commander_bitboxbase(
146147
request: *const bitbox02::BitBoxBaseRequest,
147148
) -> bitbox02::commander_error_t {
148149
// Accessing raw pointers are unsafe
149-
let request = match unsafe { request.as_ref() } {
150+
let request = match request.as_ref() {
150151
Some(request) => request,
151152
None => return bitbox02::COMMANDER_ERR_GENERIC,
152153
};
153154
// It is unsafe to access C style unions
154155
match request.which_request {
155156
bitbox02::BitBoxBaseRequest_confirm_pairing_tag => {
156-
confirm_pairing(unsafe { &request.request.confirm_pairing })
157-
}
158-
bitbox02::BitBoxBaseRequest_heartbeat_tag => {
159-
heartbeat(unsafe { &request.request.heartbeat })
157+
confirm_pairing(&request.request.confirm_pairing)
160158
}
159+
bitbox02::BitBoxBaseRequest_heartbeat_tag => heartbeat(&request.request.heartbeat),
161160
bitbox02::BitBoxBaseRequest_display_status_tag => {
162-
display_status(unsafe { &request.request.display_status })
163-
}
164-
bitbox02::BitBoxBaseRequest_set_config_tag => {
165-
set_config(unsafe { &request.request.set_config })
161+
display_status(&request.request.display_status)
166162
}
163+
bitbox02::BitBoxBaseRequest_set_config_tag => set_config(&request.request.set_config),
167164
_ => bitbox02::COMMANDER_ERR_GENERIC,
168165
}
169166
}
@@ -177,11 +174,13 @@ pub extern "C" fn bitboxbase_config_led_mode_get() -> StatusLedMode {
177174
config.status_led_mode.clone()
178175
}
179176

177+
/// # Safety
178+
/// `res` must be NOT NULL and of size at least `res_len`.
180179
#[no_mangle]
181-
pub extern "C" fn bitboxbase_config_ip_get(res: *mut c_char, res_len: usize) {
180+
pub unsafe extern "C" fn bitboxbase_config_ip_get(res: *mut c_char, res_len: usize) {
182181
// It is not safe to call any functions that also touch CONFIG at the same time
183-
let config = unsafe { &CONFIG };
184-
let buf = unsafe { core::slice::from_raw_parts_mut(res, res_len) };
182+
let config = &CONFIG;
183+
let buf = core::slice::from_raw_parts_mut(res, res_len);
185184
let mut fcstring = FixedCString::new(buf);
186185

187186
if let Some(ip) = &config.ip {
@@ -211,11 +210,13 @@ pub extern "C" fn bitboxbase_state_get() -> BitBoxBaseBackgroundState {
211210
state.state.clone()
212211
}
213212

213+
/// # Safety
214+
/// `buf` must be not NULL and be of size at least `buf_len`.
214215
#[no_mangle]
215-
pub extern "C" fn bitboxbase_state_get_description(buf: *mut c_char, buf_len: usize) {
216+
pub unsafe extern "C" fn bitboxbase_state_get_description(buf: *mut c_char, buf_len: usize) {
216217
assert!(!buf.is_null());
217-
let state = unsafe { &STATE };
218-
let buf = unsafe { core::slice::from_raw_parts_mut(buf, buf_len) };
218+
let state = &STATE;
219+
let buf = core::slice::from_raw_parts_mut(buf, buf_len);
219220
let mut buf = FixedCString::new(buf);
220221
let _ = write!(
221222
buf,
@@ -226,9 +227,11 @@ pub extern "C" fn bitboxbase_state_get_description(buf: *mut c_char, buf_len: us
226227
);
227228
}
228229

230+
/// # Safety
231+
/// `ptr` must be not NULL and of size at least `ptr_len`.
229232
#[no_mangle]
230-
pub extern "C" fn bitboxbase_status_get(ptr: *mut c_char, ptr_len: usize) {
231-
let buf = unsafe { core::slice::from_raw_parts_mut(ptr, ptr_len) };
233+
pub unsafe extern "C" fn bitboxbase_status_get(ptr: *mut c_char, ptr_len: usize) {
234+
let buf = core::slice::from_raw_parts_mut(ptr, ptr_len);
232235
let mut wrapper = FixedCString::new(buf);
233-
write_status(&mut wrapper, unsafe { &CONFIG });
236+
write_status(&mut wrapper, &CONFIG);
234237
}

src/rust/bitbox02-rust-c/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl CStrMut {
199199
if write_slice.iter().any(|&c| c == 0) {
200200
panic!("null terminated strings can't contain null");
201201
}
202-
if let Err(_) = core::str::from_utf8(write_slice) {
202+
if core::str::from_utf8(write_slice).is_err() {
203203
panic!("strings must be valid utf-8");
204204
}
205205
slice[req] = 0;

src/rust/bitbox02-rust/src/platform/bitboxbase/config.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,10 @@ impl Config {
7979
if !hostname.is_ascii() {
8080
return Err(Error::InvalidHostname);
8181
}
82-
if hostname.len() < 1 || hostname.len() > 63 {
82+
if hostname.is_empty() || hostname.len() > 63 {
8383
return Err(Error::InvalidHostname);
8484
}
85-
// Since we checked that the string contains at least one character we can safely unwrap
86-
if hostname.chars().nth(0).unwrap() == '-' || hostname.chars().last().unwrap() == '-' {
85+
if hostname.starts_with('-') || hostname.ends_with('-') {
8786
return Err(Error::InvalidHostname);
8887
}
8988
if hostname.chars().any(|c| !c.is_alphanumeric() && c != '-') {

src/rust/bitbox02-rust/src/platform/bitboxbase/display.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ pub fn write_status<W: Write>(w: &mut W, config: &Config) {
2727
} else {
2828
let _ = write!(w, "<unnamed>");
2929
}
30-
let _ = write!(w, "\n");
30+
let _ = writeln!(w);
3131
let _ = write!(w, "ip: ");
3232
if let Some(ip) = &config.ip {
3333
let _ = write!(w, "{}", ip);
3434
} else {
3535
let _ = write!(w, "<unassigned>");
3636
}
37-
let _ = write!(w, "\n");
38-
let _ = write!(w, "status: OK\n");
39-
let _ = write!(w, "mode: {:?}\n", config.status_led_mode);
37+
let _ = writeln!(w);
38+
let _ = writeln!(w, "status: OK");
39+
let _ = writeln!(w, "mode: {:?}", config.status_led_mode);
4040
}
4141

4242
pub fn display_status(config: &Config, duration: Option<Duration>) {

src/rust/bitbox02-rust/src/util.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ pub struct FixedCString<'a> {
4646

4747
impl<'a> FixedCString<'a> {
4848
pub fn new(buf: &'a mut [u8]) -> Self {
49-
FixedCString {
50-
buf: buf,
51-
offset: 0,
52-
}
49+
FixedCString { buf, offset: 0 }
5350
}
5451
}
5552

src/rust/bitbox02-rust/src/workflow/pairing.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
use arrayvec::ArrayString;
16-
use binascii;
1716
use core::fmt::Write;
1817

1918
pub async fn confirm(hash: &[u8; 32]) -> bool {

src/rust/bitbox02/src/password.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ impl Password {
3131

3232
/// Returns the underlying C string buffer (null terminated), to be used in C function calls.
3333
pub fn as_cstr(&self) -> *const util::c_types::c_char {
34-
return &self.0 as *const _;
34+
&self.0 as *const _
3535
}
3636

3737
/// Returns the buffer size (including null terminator).
3838
pub fn cap(&self) -> usize {
39-
return self.0.len();
39+
self.0.len()
4040
}
4141

4242
/// Returns a &str instance for use in Rust. panics if the

src/rust/bitbox02/src/ui.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl Font {
116116

117117
impl Default for Font {
118118
fn default() -> Self {
119-
return Font::Default;
119+
Font::Default
120120
}
121121
}
122122

src/rust/bitbox02/src/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// limitations under the License.
1414

1515
/// Must be given a null-terminated string
16+
/// # Safety
17+
/// ptr must be not NULL.
1618
pub unsafe fn strlen_ptr(ptr: *const u8) -> isize {
1719
let mut end = ptr;
1820
loop {

src/rust/util/src/name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/// size, consist of printable ASCII characters only (and space), not
1717
/// start or end with whitespace, and contain no whitespace other than space.
1818
pub fn validate(name: &str, max_len: usize) -> bool {
19-
if name.len() == 0 || name.len() > max_len {
19+
if name.is_empty() || name.len() > max_len {
2020
return false;
2121
}
2222
if !super::ascii::all_ascii(name) {

0 commit comments

Comments
 (0)