Fix several clippy warnings in components/bluetooth (#31512)

* Fix several clippy warnings in components/bluetooth/lib

* Fix requested change while maintaining clippy rules
This commit is contained in:
Mucha Naibei 2024-03-06 18:32:04 +03:00 committed by GitHub
parent 096bd2977a
commit 7ce19f2885
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -120,7 +120,7 @@ fn matches_filter(device: &BluetoothDevice, filter: &BluetoothScanfilter) -> boo
if !filter.get_services().is_empty() {
if let Ok(device_uuids) = device.get_uuids() {
for service in filter.get_services() {
if device_uuids.iter().find(|x| x == &service).is_none() {
if !device_uuids.iter().any(|x| x == service) {
return false;
}
}
@ -128,12 +128,12 @@ fn matches_filter(device: &BluetoothDevice, filter: &BluetoothScanfilter) -> boo
}
// Step 4.
if let Some(ref manufacturer_data) = filter.get_manufacturer_data() {
if let Some(manufacturer_data) = filter.get_manufacturer_data() {
let advertised_manufacturer_data = match device.get_manufacturer_data() {
Ok(data) => data,
Err(_) => return false,
};
for (ref id, &(ref prefix, ref mask)) in manufacturer_data.iter() {
for (id, (prefix, mask)) in manufacturer_data.iter() {
if let Some(advertised_data) = advertised_manufacturer_data.get(id) {
if !data_filter_matches(advertised_data, prefix, mask) {
return false;
@ -145,12 +145,12 @@ fn matches_filter(device: &BluetoothDevice, filter: &BluetoothScanfilter) -> boo
}
// Step 5.
if let Some(ref service_data) = filter.get_service_data() {
if let Some(service_data) = filter.get_service_data() {
let advertised_service_data = match device.get_service_data() {
Ok(data) => data,
Err(_) => return false,
};
for (uuid, &(ref prefix, ref mask)) in service_data.iter() {
for (uuid, (prefix, mask)) in service_data.iter() {
if let Some(advertised_data) = advertised_service_data.get(uuid.as_str()) {
if !data_filter_matches(advertised_data, prefix, mask) {
return false;
@ -193,10 +193,7 @@ fn matches_filters(device: &BluetoothDevice, filters: &BluetoothScanfilterSequen
}
fn is_mock_adapter(adapter: &BluetoothAdapter) -> bool {
match adapter {
&BluetoothAdapter::Mock(_) => true,
_ => false,
}
matches!(adapter, &BluetoothAdapter::Mock(_))
}
pub struct BluetoothManager {
@ -299,7 +296,7 @@ impl BluetoothManager {
self.allowed_services.clear();
self.adapter = BluetoothAdapter::new_mock().ok();
match test::test(self, data_set_name) {
Ok(_) => return Ok(()),
Ok(_) => Ok(()),
Err(error) => Err(BluetoothError::Type(error.to_string())),
}
}
@ -352,16 +349,16 @@ impl BluetoothManager {
if !adapter.is_powered().unwrap_or(false) {
return Err(BluetoothError::NotFound);
}
return Ok(adapter);
Ok(adapter)
},
None => return Err(BluetoothError::NotFound),
None => Err(BluetoothError::NotFound),
}
}
// Device
fn get_and_cache_devices(&mut self, adapter: &mut BluetoothAdapter) -> Vec<BluetoothDevice> {
let devices = adapter.get_devices().unwrap_or(vec![]);
let devices = adapter.get_devices().unwrap_or_default();
for device in &devices {
if let Ok(address) = device.get_address() {
if !self.address_to_id.contains_key(&address) {
@ -617,11 +614,10 @@ impl BluetoothManager {
// Step 7.
// Note: There are no requiredServiceUUIDS, we scan for all devices.
if let Ok(ref session) = adapter.create_discovery_session() {
if session.start_discovery().is_ok() {
if !is_mock_adapter(&adapter) {
thread::sleep(Duration::from_millis(DISCOVERY_TIMEOUT_MS));
}
if session.start_discovery().is_ok() && !is_mock_adapter(&adapter) {
thread::sleep(Duration::from_millis(DISCOVERY_TIMEOUT_MS));
}
let _ = session.stop_discovery();
}
@ -629,10 +625,7 @@ impl BluetoothManager {
// Step 8.
if !options.is_accepting_all_devices() {
matched_devices = matched_devices
.into_iter()
.filter(|d| matches_filters(d, options.get_filters()))
.collect();
matched_devices.retain(|d| matches_filters(d, options.get_filters()));
}
// Step 9.
@ -686,9 +679,9 @@ impl BluetoothManager {
// TODO: Step 5.1.4: Use the exchange MTU procedure.
}
// Step 5.1.3.
return Err(BluetoothError::Network);
Err(BluetoothError::Network)
},
None => return Err(BluetoothError::NotFound),
None => Err(BluetoothError::NotFound),
}
}
@ -709,9 +702,9 @@ impl BluetoothManager {
return Ok(());
}
}
return Err(BluetoothError::Network);
Err(BluetoothError::Network)
},
None => return Err(BluetoothError::NotFound),
None => Err(BluetoothError::NotFound),
}
}
@ -742,14 +735,14 @@ impl BluetoothManager {
}
let mut services = self.get_and_cache_gatt_services(&mut adapter, &id);
if let Some(uuid) = uuid {
services.retain(|ref e| e.get_uuid().unwrap_or(String::new()) == uuid);
services.retain(|e| e.get_uuid().unwrap_or_default() == uuid);
}
let mut services_vec = vec![];
for service in services {
if service.is_primary().unwrap_or(false) {
if let Ok(uuid) = service.get_uuid() {
services_vec.push(BluetoothServiceMsg {
uuid: uuid,
uuid,
is_primary: true,
instance_id: service.get_id(),
});
@ -761,7 +754,7 @@ impl BluetoothManager {
return Err(BluetoothError::NotFound);
}
return Ok(BluetoothResponse::GetPrimaryServices(services_vec, single));
Ok(BluetoothResponse::GetPrimaryServices(services_vec, single))
},
GATTType::Characteristic => {
// Step 5.
@ -772,14 +765,14 @@ impl BluetoothManager {
let mut characteristics =
self.get_and_cache_gatt_characteristics(&mut adapter, &id);
if let Some(uuid) = uuid {
characteristics.retain(|ref e| e.get_uuid().unwrap_or(String::new()) == uuid);
characteristics.retain(|e| e.get_uuid().unwrap_or_default() == uuid);
}
let mut characteristics_vec = vec![];
for characteristic in characteristics {
if let Ok(uuid) = characteristic.get_uuid() {
let properties = self.get_characteristic_properties(&characteristic);
characteristics_vec.push(BluetoothCharacteristicMsg {
uuid: uuid,
uuid,
instance_id: characteristic.get_id(),
broadcast: properties.contains(Flags::BROADCAST),
read: properties.contains(Flags::READ),
@ -801,10 +794,10 @@ impl BluetoothManager {
return Err(BluetoothError::NotFound);
}
return Ok(BluetoothResponse::GetCharacteristics(
Ok(BluetoothResponse::GetCharacteristics(
characteristics_vec,
single,
));
))
},
GATTType::IncludedService => {
// Step 5.
@ -832,7 +825,7 @@ impl BluetoothManager {
}
}
if let Some(uuid) = uuid {
services_vec.retain(|ref s| s.uuid == uuid);
services_vec.retain(|s| s.uuid == uuid);
}
services_vec.retain(|s| !uuid_is_blocklisted(&s.uuid, Blocklist::All));
@ -841,7 +834,7 @@ impl BluetoothManager {
return Err(BluetoothError::NotFound);
}
return Ok(BluetoothResponse::GetIncludedServices(services_vec, single));
Ok(BluetoothResponse::GetIncludedServices(services_vec, single))
},
GATTType::Descriptor => {
// Step 5.
@ -851,13 +844,13 @@ impl BluetoothManager {
// Step 6.
let mut descriptors = self.get_and_cache_gatt_descriptors(&mut adapter, &id);
if let Some(uuid) = uuid {
descriptors.retain(|ref e| e.get_uuid().unwrap_or(String::new()) == uuid);
descriptors.retain(|e| e.get_uuid().unwrap_or_default() == uuid);
}
let mut descriptors_vec = vec![];
for descriptor in descriptors {
if let Ok(uuid) = descriptor.get_uuid() {
descriptors_vec.push(BluetoothDescriptorMsg {
uuid: uuid,
uuid,
instance_id: descriptor.get_id(),
});
}
@ -867,7 +860,7 @@ impl BluetoothManager {
if descriptors_vec.is_empty() {
return Err(BluetoothError::NotFound);
}
return Ok(BluetoothResponse::GetDescriptors(descriptors_vec, single));
Ok(BluetoothResponse::GetDescriptors(descriptors_vec, single))
},
}
}
@ -882,7 +875,7 @@ impl BluetoothManager {
// (Characteristic) Step 5.3.
let mut value = self
.get_gatt_characteristic(&mut adapter, &id)
.map(|c| c.read_value().unwrap_or(vec![]));
.map(|c| c.read_value().unwrap_or_default());
// (Characteristic) TODO: Step 5.4: Handle all the errors returned from the read_value call.
@ -890,7 +883,7 @@ impl BluetoothManager {
if value.is_none() {
value = self
.get_gatt_descriptor(&mut adapter, &id)
.map(|d| d.read_value().unwrap_or(vec![]));
.map(|d| d.read_value().unwrap_or_default());
}
// (Descriptor) TODO: Step 5.3: Handle all the errors returned from the read_value call.
@ -898,11 +891,11 @@ impl BluetoothManager {
match value {
// (Characteristic) Step 5.5.4.
// (Descriptor) Step 5.4.3.
Some(v) => return Ok(BluetoothResponse::ReadValue(v)),
Some(v) => Ok(BluetoothResponse::ReadValue(v)),
// (Characteristic) Step 4.
// (Descriptor) Step 4.
None => return Err(BluetoothError::InvalidState),
None => Err(BluetoothError::InvalidState),
}
}
@ -933,15 +926,15 @@ impl BluetoothManager {
Some(v) => match v {
// (Characteristic) Step 7.5.3.
// (Descriptor) Step 7.4.3.
Ok(_) => return Ok(BluetoothResponse::WriteValue(value)),
Ok(_) => Ok(BluetoothResponse::WriteValue(value)),
// (Characteristic) Step 7.1.
Err(_) => return Err(BluetoothError::NotSupported),
Err(_) => Err(BluetoothError::NotSupported),
},
// (Characteristic) Step 6.
// (Descriptor) Step 6.
None => return Err(BluetoothError::InvalidState),
None => Err(BluetoothError::InvalidState),
}
}
@ -969,14 +962,14 @@ impl BluetoothManager {
match result {
// (StartNotification) Step 11.
// (StopNotification) Step 5.
Ok(_) => return Ok(BluetoothResponse::EnableNotification(())),
Ok(_) => Ok(BluetoothResponse::EnableNotification(())),
// (StartNotification) Step 5.
Err(_) => return Err(BluetoothError::NotSupported),
Err(_) => Err(BluetoothError::NotSupported),
}
},
// (StartNotification) Step 4.
None => return Err(BluetoothError::InvalidState),
None => Err(BluetoothError::InvalidState),
}
}
@ -984,7 +977,7 @@ impl BluetoothManager {
fn watch_advertisements(&mut self, _device_id: String) -> BluetoothResponseResult {
// Step 2.
// TODO: Implement this when supported in lower level
return Err(BluetoothError::NotSupported);
Err(BluetoothError::NotSupported)
}
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-getavailability