1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-06-28 19:02:34 +00:00

style(core/rust): enable clippy lints

[no changelog]
This commit is contained in:
Martin Milata 2021-09-22 18:15:40 +02:00
parent bd005e33df
commit 81e66cb024
14 changed files with 37 additions and 32 deletions

View File

@ -29,8 +29,8 @@ let
"thumbv7m-none-eabi" # T1 "thumbv7m-none-eabi" # T1
]; ];
# we use rustfmt from nixpkgs because it's built with the nighly flag needed for wrap_comments # we use rustfmt from nixpkgs because it's built with the nighly flag needed for wrap_comments
# to use official binary, remove rustfmt from buildInputs below and uncomment next line: # to use official binary, remove rustfmt from buildInputs and add it to extensions:
# extensions = [ "rustfmt" ]; extensions = [ "clippy" ];
}; };
in in
with nixpkgs; with nixpkgs;

View File

@ -9,6 +9,7 @@ core unit test:
script: script:
- nix-shell --run "poetry run make -C core test | ts -s" - nix-shell --run "poetry run make -C core test | ts -s"
- nix-shell --run "poetry run make -C core test_rust | ts -s" - nix-shell --run "poetry run make -C core test_rust | ts -s"
- nix-shell --run "poetry run make -C core clippy | ts -s"
core device ui test: core device ui test:
stage: test stage: test

View File

@ -118,6 +118,9 @@ mypy:
src/apps/webauthn \ src/apps/webauthn \
src/trezor/ui src/trezor/ui
clippy:
cd embed/rust ; cargo clippy
## code generation: ## code generation:
templates: ## render Mako templates (for lists of coins, tokens, etc.) templates: ## render Mako templates (for lists of coins, tokens, etc.)

View File

@ -150,7 +150,7 @@ fn generate_micropython_bindings() {
.lines() .lines()
.skip_while(|s| !s.contains("search starts here:")) .skip_while(|s| !s.contains("search starts here:"))
.take_while(|s| !s.contains("End of search list.")) .take_while(|s| !s.contains("End of search list."))
.filter(|s| s.starts_with(" ")) .filter(|s| s.starts_with(' '))
.map(|s| format!("-I{}", s.trim())); .map(|s| format!("-I{}", s.trim()));
bindings = bindings.clang_args(include_args); bindings = bindings.clang_args(include_args);

View File

@ -4,6 +4,7 @@ use cstr_core::CStr;
use crate::micropython::{ffi, obj::Obj, qstr::Qstr}; use crate::micropython::{ffi, obj::Obj, qstr::Qstr};
#[derive(Debug)] #[derive(Debug)]
#[allow(clippy::enum_variant_names)]
pub enum Error { pub enum Error {
TypeError, TypeError,
OutOfRange, OutOfRange,
@ -22,7 +23,7 @@ impl Error {
/// exception, because the object is not guaranteed to remain intact. /// exception, because the object is not guaranteed to remain intact.
/// Micropython might reuse the same space for creating a different /// Micropython might reuse the same space for creating a different
/// exception. /// exception.
pub unsafe fn to_obj(self) -> Obj { pub unsafe fn into_obj(self) -> Obj {
unsafe { unsafe {
// SAFETY: // SAFETY:
// - first argument is a reference to a valid exception type // - first argument is a reference to a valid exception type
@ -34,7 +35,7 @@ impl Error {
Error::AllocationFailed => ffi::mp_obj_new_exception(&ffi::mp_type_MemoryError), Error::AllocationFailed => ffi::mp_obj_new_exception(&ffi::mp_type_MemoryError),
Error::CaughtException(obj) => obj, Error::CaughtException(obj) => obj,
Error::KeyError(key) => { Error::KeyError(key) => {
ffi::mp_obj_new_exception_args(&ffi::mp_type_KeyError, 1, &key.into()) ffi::mp_obj_new_exception_args(&ffi::mp_type_KeyError, 1, &key)
} }
Error::ValueError(msg) => { Error::ValueError(msg) => {
if let Ok(msg) = msg.try_into() { if let Ok(msg) = msg.try_into() {
@ -45,7 +46,7 @@ impl Error {
} }
Error::ValueErrorParam(msg, param) => { Error::ValueErrorParam(msg, param) => {
if let Ok(msg) = msg.try_into() { if let Ok(msg) = msg.try_into() {
let args: [Obj; 2] = [msg, param.into()]; let args: [Obj; 2] = [msg, param];
ffi::mp_obj_new_exception_args(&ffi::mp_type_ValueError, 2, args.as_ptr()) ffi::mp_obj_new_exception_args(&ffi::mp_type_ValueError, 2, args.as_ptr())
} else { } else {
ffi::mp_obj_new_exception(&ffi::mp_type_ValueError) ffi::mp_obj_new_exception(&ffi::mp_type_ValueError)

View File

@ -30,5 +30,5 @@ fn panic(_info: &PanicInfo) -> ! {
// TODO: Ideally we would take the file and line info out of // TODO: Ideally we would take the file and line info out of
// `PanicInfo::location()`. // `PanicInfo::location()`.
trezorhal::common::fatal_error(&empty, &msg, &empty, 0, &empty); trezorhal::common::fatal_error(empty, msg, empty, 0, empty);
} }

View File

@ -4,7 +4,7 @@ pub type Func = ffi::mp_obj_fun_builtin_fixed_t;
impl Func { impl Func {
/// Convert a "static const" function to a MicroPython object. /// Convert a "static const" function to a MicroPython object.
pub const fn to_obj(&'static self) -> Obj { pub const fn as_obj(&'static self) -> Obj {
// SAFETY: // SAFETY:
// - We are an object struct with a base and a type. // - We are an object struct with a base and a type.
// - 'static lifetime holds us in place. // - 'static lifetime holds us in place.

View File

@ -40,7 +40,7 @@ impl Map {
} }
impl Map { impl Map {
pub fn from_fixed<'a>(table: &'a [MapElem]) -> MapRef<'a> { pub fn from_fixed(table: &[MapElem]) -> MapRef {
let mut map = MaybeUninit::uninit(); let mut map = MaybeUninit::uninit();
// SAFETY: `mp_map_init_fixed_table` completely initializes all fields of `map`. // SAFETY: `mp_map_init_fixed_table` completely initializes all fields of `map`.
unsafe { unsafe {

View File

@ -129,7 +129,8 @@ impl TryFrom<Obj> for bool {
// SAFETY: // SAFETY:
// - `obj` can be anything uPy understands. // - `obj` can be anything uPy understands.
// EXCEPTION: Can call Python code (on custom instances) and therefore raise. // EXCEPTION: Can call Python code (on custom instances) and therefore raise.
if catch_exception(|| unsafe { ffi::mp_obj_is_true(obj) })? { let result = catch_exception(|| unsafe { ffi::mp_obj_is_true(obj) })?;
if result {
Ok(true) Ok(true)
} else { } else {
Ok(false) Ok(false)

View File

@ -14,8 +14,8 @@ pub unsafe fn raise_exception(err: Error) -> ! {
unsafe { unsafe {
// SAFETY: // SAFETY:
// - argument must be an exception instance // - argument must be an exception instance
// (err.to_obj() should return the right thing) // (err.into_obj() should return the right thing)
ffi::nlr_jump(err.to_obj().as_ptr()); ffi::nlr_jump(err.into_obj().as_ptr());
} }
panic!(); panic!();
} }

View File

@ -19,7 +19,7 @@ impl Type {
} }
} }
pub fn to_base(&'static self) -> ObjBase { pub fn as_base(&'static self) -> ObjBase {
ObjBase { type_: self } ObjBase { type_: self }
} }
} }

View File

@ -126,8 +126,7 @@ pub fn find_name_by_msg_offset(msg_offset: u16) -> Result<u16, Error> {
name_defs name_defs
.iter() .iter()
.filter(|def| def.msg_offset == msg_offset) .find(|def| def.msg_offset == msg_offset)
.next()
.map(|def| def.msg_name) .map(|def| def.msg_name)
.ok_or_else(|| Error::KeyError(msg_offset.into())) .ok_or_else(|| Error::KeyError(msg_offset.into()))
} }

View File

@ -29,7 +29,7 @@ pub extern "C" fn protobuf_len(obj: Obj) -> Obj {
Encoder.encode_message(stream, &obj.def(), &obj)?; Encoder.encode_message(stream, &obj.def(), &obj)?;
Ok(stream.len.try_into()?) stream.len.try_into()
}; };
unsafe { util::try_or_raise(block) } unsafe { util::try_or_raise(block) }
} }
@ -46,7 +46,7 @@ pub extern "C" fn protobuf_encode(buf: Obj, obj: Obj) -> Obj {
Encoder.encode_message(stream, &obj.def(), &obj)?; Encoder.encode_message(stream, &obj.def(), &obj)?;
Ok(stream.len().try_into()?) stream.len().try_into()
}; };
unsafe { util::try_or_raise(block) } unsafe { util::try_or_raise(block) }
} }

View File

@ -28,7 +28,7 @@ pub struct MsgObj {
impl MsgObj { impl MsgObj {
pub fn alloc_with_capacity(capacity: usize, msg: &MsgDef) -> Result<Gc<Self>, Error> { pub fn alloc_with_capacity(capacity: usize, msg: &MsgDef) -> Result<Gc<Self>, Error> {
Gc::new(Self { Gc::new(Self {
base: Self::obj_type().to_base(), base: Self::obj_type().as_base(),
map: Map::with_capacity(capacity)?, map: Map::with_capacity(capacity)?,
msg_wire_id: msg.wire_id, msg_wire_id: msg.wire_id,
msg_offset: msg.offset, msg_offset: msg.offset,
@ -100,13 +100,13 @@ impl MsgObj {
} }
} }
impl Into<Obj> for Gc<MsgObj> { impl From<Gc<MsgObj>> for Obj {
fn into(self) -> Obj { fn from(value: Gc<MsgObj>) -> Self {
// SAFETY: // SAFETY:
// - We are GC-allocated. // - `value` is GC-allocated.
// - We are `repr(C)`. // - `value` is `repr(C)`.
// - We have a `base` as the first field with the correct type. // - `value` has a `base` as the first field with the correct type.
unsafe { Obj::from_ptr(Self::into_raw(self).cast()) } unsafe { Self::from_ptr(Gc::into_raw(value).cast()) }
} }
} }
@ -155,7 +155,7 @@ pub struct MsgDefObj {
impl MsgDefObj { impl MsgDefObj {
pub fn alloc(def: MsgDef) -> Result<Gc<Self>, Error> { pub fn alloc(def: MsgDef) -> Result<Gc<Self>, Error> {
let this = Gc::new(Self { let this = Gc::new(Self {
base: Self::obj_type().to_base(), base: Self::obj_type().as_base(),
def, def,
})?; })?;
Ok(this) Ok(this)
@ -175,13 +175,13 @@ impl MsgDefObj {
} }
} }
impl Into<Obj> for Gc<MsgDefObj> { impl From<Gc<MsgDefObj>> for Obj {
fn into(self) -> Obj { fn from(value: Gc<MsgDefObj>) -> Self {
// SAFETY: // SAFETY:
// - We are GC-allocated. // - `value` is GC-allocated.
// - We are `repr(C)`. // - `value` is `repr(C)`.
// - We have a `base` as the first field with the correct type. // - `value` has a `base` as the first field with the correct type.
unsafe { Obj::from_ptr(Self::into_raw(self).cast()) } unsafe { Self::from_ptr(Gc::into_raw(value).cast()) }
} }
} }
@ -234,7 +234,7 @@ unsafe extern "C" fn msg_def_obj_attr(self_in: Obj, attr: ffi::qstr, dest: *mut
// dest[0] = function_obj // dest[0] = function_obj
// dest[1] = self // dest[1] = self
unsafe { unsafe {
dest.write(MSG_DEF_OBJ_IS_TYPE_OF_OBJ.to_obj()); dest.write(MSG_DEF_OBJ_IS_TYPE_OF_OBJ.as_obj());
dest.offset(1).write(self_in); dest.offset(1).write(self_in);
} }
} }