Skip to content
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

Improve clippy coverage & fix missleading .ok() usage. #131

Merged
merged 2 commits into from
Nov 10, 2023
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
3 changes: 3 additions & 0 deletions .github/workflows/rustfmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ jobs:
components: rustfmt
- run: cargo fmt --all -- --check
- run: cargo clippy --all-features
- run: cargo clippy --features defmt
- run: cargo clippy --features control-buffer-256
- run: cargo clippy
6 changes: 3 additions & 3 deletions src/control_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ impl<B: UsbBus> ControlPipe<'_, B> {
| ControlState::DataInLast
| ControlState::DataInZlp
| ControlState::StatusOut => {
self.ep_out.read(&mut []).ok();
let _ = self.ep_out.read(&mut []);
self.state = ControlState::Idle;
}
_ => {
// Discard the packet
self.ep_out.read(&mut []).ok();
let _ = self.ep_out.read(&mut []);

// Unexpected OUT packet
self.set_error()
Expand Down Expand Up @@ -235,7 +235,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
_ => return Err(UsbError::InvalidState),
};

self.ep_in.write(&[]).ok();
let _ = self.ep_in.write(&[]);
self.state = ControlState::StatusIn;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/descriptor/lang_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ pub enum LangID {
MN_MONG_MN = 0x0C50,
DZ_BT = 0x0C51,
TMZ_MA = 0x0C5F,
QUZ_PE = 0x0C6b,
QUZ_PE = 0x0C6B,
LOCALE_CUSTOM_UNSPECIFIED = 0x1000,
AR_LY = 0x1001,
ZH_SG = 0x1004,
Expand Down
102 changes: 48 additions & 54 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,13 @@ impl<B: UsbBus> UsbDevice<'_, B> {
0x0000
};

xfer.accept_with(&status.to_le_bytes()).ok();
let _ = xfer.accept_with(&status.to_le_bytes());
}

(Recipient::Interface, Request::GET_STATUS) => {
let status: u16 = 0x0000;

xfer.accept_with(&status.to_le_bytes()).ok();
let _ = xfer.accept_with(&status.to_le_bytes());
}

(Recipient::Endpoint, Request::GET_STATUS) => {
Expand All @@ -342,7 +342,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
0x0000
};

xfer.accept_with(&status.to_le_bytes()).ok();
let _ = xfer.accept_with(&status.to_le_bytes());
}

(Recipient::Device, Request::GET_DESCRIPTOR) => {
Expand All @@ -355,13 +355,13 @@ impl<B: UsbBus> UsbDevice<'_, B> {
_ => CONFIGURATION_NONE,
};

xfer.accept_with(&config.to_le_bytes()).ok();
let _ = xfer.accept_with(&config.to_le_bytes());
}

(Recipient::Interface, Request::GET_INTERFACE) => {
// Reject interface numbers bigger than 255
if req.index > core::u8::MAX.into() {
xfer.reject().ok();
let _ = xfer.reject();
return;
}

Expand All @@ -370,22 +370,21 @@ impl<B: UsbBus> UsbDevice<'_, B> {
for cls in classes {
if let Some(setting) = cls.get_alt_setting(InterfaceNumber(req.index as u8))
{
xfer.accept_with(&setting.to_le_bytes()).ok();
let _ = xfer.accept_with(&setting.to_le_bytes());
return;
}
}

// If no class returned an alternate setting, return the default value
xfer.accept_with(&DEFAULT_ALTERNATE_SETTING.to_le_bytes())
.ok();
let _ = xfer.accept_with(&DEFAULT_ALTERNATE_SETTING.to_le_bytes());
}

_ => (),
};
}

if self.control.waiting_for_response() {
self.control.reject().ok();
let _ = self.control.reject();
}
}

Expand Down Expand Up @@ -414,13 +413,13 @@ impl<B: UsbBus> UsbDevice<'_, B> {
Request::FEATURE_DEVICE_REMOTE_WAKEUP,
) => {
self.remote_wakeup_enabled = false;
xfer.accept().ok();
let _ = xfer.accept();
}

(Recipient::Endpoint, Request::CLEAR_FEATURE, Request::FEATURE_ENDPOINT_HALT) => {
self.bus
.set_stalled(((req.index as u8) & 0x8f).into(), false);
xfer.accept().ok();
let _ = xfer.accept();
}

(
Expand All @@ -429,13 +428,13 @@ impl<B: UsbBus> UsbDevice<'_, B> {
Request::FEATURE_DEVICE_REMOTE_WAKEUP,
) => {
self.remote_wakeup_enabled = true;
xfer.accept().ok();
let _ = xfer.accept();
}

(Recipient::Endpoint, Request::SET_FEATURE, Request::FEATURE_ENDPOINT_HALT) => {
self.bus
.set_stalled(((req.index as u8) & 0x8f).into(), true);
xfer.accept().ok();
let _ = xfer.accept();
}

(Recipient::Device, Request::SET_ADDRESS, 1..=127) => {
Expand All @@ -445,59 +444,59 @@ impl<B: UsbBus> UsbDevice<'_, B> {
} else {
self.pending_address = req.value as u8;
}
xfer.accept().ok();
let _ = xfer.accept();
}

(Recipient::Device, Request::SET_CONFIGURATION, CONFIGURATION_VALUE_U16) => {
self.device_state = UsbDeviceState::Configured;
xfer.accept().ok();
let _ = xfer.accept();
}

(Recipient::Device, Request::SET_CONFIGURATION, CONFIGURATION_NONE_U16) => {
match self.device_state {
UsbDeviceState::Default => {
xfer.reject().ok();
let _ = xfer.accept();
}
_ => {
self.device_state = UsbDeviceState::Addressed;
xfer.accept().ok();
let _ = xfer.accept();
}
}
}

(Recipient::Interface, Request::SET_INTERFACE, alt_setting) => {
// Reject interface numbers and alt settings bigger than 255
if req.index > core::u8::MAX.into() || alt_setting > core::u8::MAX.into() {
xfer.reject().ok();
let _ = xfer.reject();
return;
}

// Ask class implementations, whether they accept the alternate interface setting.
for cls in classes {
if cls.set_alt_setting(InterfaceNumber(req.index as u8), alt_setting as u8)
{
xfer.accept().ok();
let _ = xfer.accept();
return;
}
}

// Default behaviour, if no class implementation accepted the alternate setting.
if alt_setting == DEFAULT_ALTERNATE_SETTING_U16 {
xfer.accept().ok();
let _ = xfer.accept();
} else {
xfer.reject().ok();
let _ = xfer.reject();
}
}

_ => {
xfer.reject().ok();
let _ = xfer.reject();
return;
}
}
}

if self.control.waiting_for_response() {
self.control.reject().ok();
let _ = self.control.reject();
}
}

Expand All @@ -510,12 +509,11 @@ impl<B: UsbBus> UsbDevice<'_, B> {
xfer: ControlIn<B>,
f: impl FnOnce(&mut DescriptorWriter) -> Result<()>,
) {
xfer.accept(|buf| {
let _ = xfer.accept(|buf| {
let mut writer = DescriptorWriter::new(buf);
f(&mut writer)?;
Ok(writer.position())
})
.ok();
});
}

match dtype {
Expand Down Expand Up @@ -606,32 +604,28 @@ impl<B: UsbBus> UsbDevice<'_, B> {
}
};

lang_id_list
.iter()
.fuse()
.position(|list_lang_id| match *list_lang_id {
Some(list_lang_id) if req_lang_id == list_lang_id => true,
_ => false,
})
.or_else(|| {
// Since we construct the list of full supported lang_ids previously,
// we can safely reject requests which ask for other lang_id.
#[cfg(feature = "defmt")]
defmt::warn!(
"Receive unknown LANGID {:#06X}, reject the request",
req_lang_id
);
None
})
.and_then(|lang_id_list_index| {
match index {
1 => config.manufacturer,
2 => config.product,
3 => config.serial_number,
_ => unreachable!(),
}
.map(|str_list| str_list[lang_id_list_index])
})
let position =
lang_id_list.iter().fuse().position(|list_lang_id| {
matches!(*list_lang_id, Some(list_lang_id) if req_lang_id == list_lang_id)
});
#[cfg(feature = "defmt")]
if position.is_none() {
// Since we construct the list of full supported lang_ids previously,
// we can safely reject requests which ask for other lang_id.
defmt::warn!(
"Receive unknown LANGID {:#06X}, reject the request",
req_lang_id
);
}
position.and_then(|lang_id_list_index| {
match index {
1 => config.manufacturer,
2 => config.product,
3 => config.serial_number,
_ => unreachable!(),
}
.map(|str_list| str_list[lang_id_list_index])
})
} else {
// for other custom STRINGs

Expand All @@ -646,13 +640,13 @@ impl<B: UsbBus> UsbDevice<'_, B> {
if let Some(s) = s {
accept_writer(xfer, |w| w.string(s));
} else {
xfer.reject().ok();
let _ = xfer.reject();
}
}
},

_ => {
xfer.reject().ok();
let _ = xfer.reject();
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/device_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
///
/// Default: (none)
pub fn set_extra_lang_ids(mut self, extra_lang_ids: &'a [LangID]) -> Self {
if extra_lang_ids.len() == 0 {
if extra_lang_ids.is_empty() {
self.config.extra_lang_ids = None;
return self;
}
Expand Down Expand Up @@ -154,7 +154,7 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
///
/// Default: (none)
pub fn manufacturer(mut self, manufacturer_ls: &'a [&'a str]) -> Self {
if manufacturer_ls.len() == 0 {
if manufacturer_ls.is_empty() {
self.config.manufacturer = None;
return self;
}
Expand Down Expand Up @@ -184,7 +184,7 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
///
/// Default: (none)
pub fn product(mut self, product_ls: &'a [&'a str]) -> Self {
if product_ls.len() == 0 {
if product_ls.is_empty() {
self.config.product = None;
return self;
}
Expand Down Expand Up @@ -214,7 +214,7 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
///
/// Default: (none)
pub fn serial_number(mut self, serial_number_ls: &'a [&'a str]) -> Self {
if serial_number_ls.len() == 0 {
if serial_number_ls.is_empty() {
self.config.serial_number = None;
return self;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_class_host/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn run_tests(tests: &[(&str, TestFn)]) {
}

print!("test {} ... ", name);
stdout().flush().ok();
let _ = stdout().flush();

let mut out = String::new();

Expand Down