Skip to content

Commit 226f2ee

Browse files
danieleadespatrickelectric
authored andcommitted
update linting, address lints
1 parent 74243e3 commit 226f2ee

File tree

9 files changed

+58
-68
lines changed

9 files changed

+58
-68
lines changed

.github/workflows/test.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ jobs:
1919
- uses: actions/checkout@master
2020
- uses: dtolnay/rust-toolchain@master
2121
with:
22-
toolchain: nightly-2022-11-30
22+
toolchain: nightly-2023-10-21
2323
components: clippy
24-
- uses: actions-rs/clippy-check@v1
24+
- uses: actions-rs-plus/clippy-check@v2
2525
with:
26-
token: ${{ secrets.GITHUB_TOKEN }}
27-
args: --all --all-targets
26+
args: --all --all-targets --features format-generated-code
2827

2928
internal-tests:
3029
runs-on: ubuntu-latest

build/binder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub fn generate<W: Write>(modules: Vec<String>, out: &mut W) {
1111
#[allow(clippy::field_reassign_with_default)]
1212
#[allow(non_snake_case)]
1313
#[allow(clippy::unnecessary_cast)]
14+
#[allow(clippy::bad_bit_mask)]
1415
#[cfg(feature = #module)]
1516
pub mod #module_ident;
1617
}

build/parser.rs

+39-48
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,14 @@ impl MavProfile {
5757
fn update_enums(mut self) -> Self {
5858
for msg in self.messages.values() {
5959
for field in &msg.fields {
60-
if let Some(ref enum_name) = field.enumtype {
61-
// it is an enum
62-
if let Some(ref dsp) = field.display {
63-
// it is a bitmask
64-
if dsp == "bitmask" {
65-
// find the corresponding enum
66-
for enm in self.enums.values_mut() {
67-
if enm.name == *enum_name {
68-
// this is the right enum
69-
enm.bitfield = Some(field.mavtype.rust_primitive_type());
70-
}
60+
if let Some(enum_name) = &field.enumtype {
61+
// it is a bitmask
62+
if let Some("bitmask") = &field.display.as_deref() {
63+
// find the corresponding enum
64+
for enm in self.enums.values_mut() {
65+
if enm.name == *enum_name {
66+
// this is the right enum
67+
enm.bitfield = Some(field.mavtype.rust_primitive_type());
7168
}
7269
}
7370
}
@@ -503,13 +500,13 @@ impl MavMessage {
503500
fn emit_serialize_vars(&self) -> TokenStream {
504501
let ser_vars = self.fields.iter().map(|f| f.rust_writer());
505502
quote! {
506-
let mut _tmp = BytesMut::new(bytes);
503+
let mut __tmp = BytesMut::new(bytes);
507504
#(#ser_vars)*
508505
if matches!(version, MavlinkVersion::V2) {
509-
let len = _tmp.len();
510-
crate::remove_trailing_zeroes(&mut bytes[..len])
506+
let len = __tmp.len();
507+
crate::remove_trailing_zeroes(&bytes[..len])
511508
} else {
512-
_tmp.len()
509+
__tmp.len()
513510
}
514511
}
515512
}
@@ -528,21 +525,21 @@ impl MavMessage {
528525
}
529526
} else {
530527
quote! {
531-
let avail_len = _input.len();
528+
let avail_len = __input.len();
532529

533530
let mut payload_buf = [0; Self::ENCODED_LEN];
534531
let mut buf = if avail_len < Self::ENCODED_LEN {
535532
//copy available bytes into an oversized buffer filled with zeros
536-
payload_buf[0..avail_len].copy_from_slice(_input);
533+
payload_buf[0..avail_len].copy_from_slice(__input);
537534
Bytes::new(&payload_buf)
538535
} else {
539536
// fast zero copy
540-
Bytes::new(_input)
537+
Bytes::new(__input)
541538
};
542539

543-
let mut _struct = Self::default();
540+
let mut __struct = Self::default();
544541
#(#deser_vars)*
545-
Ok(_struct)
542+
Ok(__struct)
546543
}
547544
}
548545
}
@@ -607,7 +604,7 @@ impl MavMessage {
607604
const EXTRA_CRC: u8 = #extra_crc;
608605
const ENCODED_LEN: usize = #msg_encoded_len;
609606

610-
fn deser(_version: MavlinkVersion, _input: &[u8]) -> Result<Self, ParserError> {
607+
fn deser(_version: MavlinkVersion, __input: &[u8]) -> Result<Self, ParserError> {
611608
#deser_vars
612609
}
613610

@@ -643,7 +640,7 @@ impl MavField {
643640
if matches!(self.mavtype, MavType::Array(_, _)) {
644641
let rt = TokenStream::from_str(&self.mavtype.rust_type()).unwrap();
645642
mavtype = quote!(#rt);
646-
} else if let Some(ref enumname) = self.enumtype {
643+
} else if let Some(enumname) = &self.enumtype {
647644
let en = TokenStream::from_str(enumname).unwrap();
648645
mavtype = quote!(#en);
649646
} else {
@@ -695,15 +692,15 @@ impl MavField {
695692
}
696693
let ts = TokenStream::from_str(&name).unwrap();
697694
let name = quote!(#ts);
698-
let buf = format_ident!("_tmp");
695+
let buf = format_ident!("__tmp");
699696
self.mavtype.rust_writer(&name, buf)
700697
}
701698

702699
/// Emit reader
703700
fn rust_reader(&self) -> TokenStream {
704701
let _name = TokenStream::from_str(&self.name).unwrap();
705702

706-
let name = quote!(_struct.#_name);
703+
let name = quote!(__struct.#_name);
707704
let buf = format_ident!("buf");
708705
if let Some(enum_name) = &self.enumtype {
709706
// TODO: handle enum arrays properly, rather than just generating
@@ -1122,7 +1119,7 @@ pub fn parse_profile(
11221119
let attr = attr.unwrap();
11231120
match stack.last() {
11241121
Some(&MavXmlElement::Enum) => {
1125-
if let b"name" = attr.key.into_inner() {
1122+
if attr.key.into_inner() == b"name" {
11261123
mavenum.name = attr
11271124
.value
11281125
.clone()
@@ -1273,7 +1270,7 @@ pub fn parse_profile(
12731270
entry.description = Some(s.replace('\n', " "));
12741271
}
12751272
(Some(&Param), Some(&Entry)) => {
1276-
if let Some(ref mut params) = entry.params {
1273+
if let Some(params) = &mut entry.params {
12771274
// Some messages can jump between values, like:
12781275
// 0, 1, 2, 7
12791276
if params.len() < paramid.unwrap() {
@@ -1382,21 +1379,21 @@ pub fn extra_crc(msg: &MavMessage) -> u8 {
13821379
let mut crc = CRCu16::crc16mcrf4cc();
13831380

13841381
crc.digest(msg.name.as_bytes());
1385-
crc.digest(" ".as_bytes());
1382+
crc.digest(b" ");
13861383

13871384
let mut f = msg.fields.clone();
13881385
// only mavlink 1 fields should be part of the extra_crc
13891386
f.retain(|f| !f.is_extension);
13901387
f.sort_by(|a, b| a.mavtype.compare(&b.mavtype));
13911388
for field in &f {
13921389
crc.digest(field.mavtype.primitive_type().as_bytes());
1393-
crc.digest(" ".as_bytes());
1390+
crc.digest(b" ");
13941391
if field.name == "mavtype" {
1395-
crc.digest("type".as_bytes());
1392+
crc.digest(b"type");
13961393
} else {
13971394
crc.digest(field.name.as_bytes());
13981395
}
1399-
crc.digest(" ".as_bytes());
1396+
crc.digest(b" ");
14001397
if let MavType::Array(_, size) = field.mavtype {
14011398
crc.digest(&[size as u8]);
14021399
}
@@ -1443,31 +1440,25 @@ impl MavXmlFilter {
14431440
Ok(content) => {
14441441
match content {
14451442
Event::Start(bytes) | Event::Empty(bytes) => {
1446-
let id = match identify_element(bytes.name().into_inner()) {
1447-
None => {
1448-
panic!(
1449-
"unexpected element {:?}",
1450-
String::from_utf8_lossy(bytes.name().into_inner())
1451-
);
1452-
}
1453-
Some(kind) => kind,
1443+
let Some(id) = identify_element(bytes.name().into_inner()) else {
1444+
panic!(
1445+
"unexpected element {:?}",
1446+
String::from_utf8_lossy(bytes.name().into_inner())
1447+
);
14541448
};
1455-
if let MavXmlElement::Extensions = id {
1449+
if id == MavXmlElement::Extensions {
14561450
self.extension_filter.is_in = true;
14571451
}
14581452
}
14591453
Event::End(bytes) => {
1460-
let id = match identify_element(bytes.name().into_inner()) {
1461-
None => {
1462-
panic!(
1463-
"unexpected element {:?}",
1464-
String::from_utf8_lossy(bytes.name().into_inner())
1465-
);
1466-
}
1467-
Some(kind) => kind,
1454+
let Some(id) = identify_element(bytes.name().into_inner()) else {
1455+
panic!(
1456+
"unexpected element {:?}",
1457+
String::from_utf8_lossy(bytes.name().into_inner())
1458+
);
14681459
};
14691460

1470-
if let MavXmlElement::Message = id {
1461+
if id == MavXmlElement::Message {
14711462
self.extension_filter.is_in = false;
14721463
}
14731464
}

src/bin/mavlink-dump.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn main() {
4949
println!("received: {msg:?}");
5050
}
5151
Err(MessageReadError::Io(e)) => {
52-
if let std::io::ErrorKind::WouldBlock = e.kind() {
52+
if e.kind() == std::io::ErrorKind::WouldBlock {
5353
//no messages currently available to receive -- wait a while
5454
thread::sleep(Duration::from_secs(1));
5555
continue;

src/connection/udp.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ struct PacketBuf {
7474

7575
impl PacketBuf {
7676
pub fn new() -> Self {
77-
let mut v = Vec::new();
78-
v.resize(65536, 0);
77+
let v = vec![0; 65536];
7978
Self {
8079
buf: v,
8180
start: 0,

src/embedded.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ pub trait Read {
55
fn read_u8(&mut self) -> Result<u8, MessageReadError>;
66

77
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), MessageReadError> {
8-
for i in 0..buf.len() {
9-
buf[i] = self.read_u8()?;
8+
for byte in buf {
9+
*byte = self.read_u8()?;
1010
}
1111

1212
Ok(())
@@ -26,8 +26,8 @@ pub trait Write {
2626

2727
impl<W: embedded_hal::serial::Write<u8>> Write for W {
2828
fn write_all(&mut self, buf: &[u8]) -> Result<(), MessageWriteError> {
29-
for i in 0..buf.len() {
30-
nb::block!(self.write(buf[i])).map_err(|_| MessageWriteError::Io)?;
29+
for byte in buf {
30+
nb::block!(self.write(*byte)).map_err(|_| MessageWriteError::Io)?;
3131
}
3232

3333
Ok(())

src/lib.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl<M: Message> MavFrame<M> {
191191

192192
let msg_id = match version {
193193
MavlinkVersion::V2 => buf.get_u24_le(),
194-
MavlinkVersion::V1 => buf.get_u8() as u32,
194+
MavlinkVersion::V1 => buf.get_u8().into(),
195195
};
196196

197197
match M::parse(version, msg_id, buf.remaining_bytes()) {
@@ -498,10 +498,10 @@ impl MAVLinkV2MessageRaw {
498498
let payload_length: usize = self.payload_length().into();
499499

500500
// Signature to ensure the link is tamper-proof.
501-
let signature_size = if (self.incompatibility_flags() & MAVLINK_IFLAG_SIGNED) != 0 {
502-
Self::SIGNATURE_SIZE
503-
} else {
501+
let signature_size = if (self.incompatibility_flags() & MAVLINK_IFLAG_SIGNED) == 0 {
504502
0
503+
} else {
504+
Self::SIGNATURE_SIZE
505505
};
506506

507507
&mut self.0
@@ -521,10 +521,10 @@ impl MAVLinkV2MessageRaw {
521521
pub fn raw_bytes(&self) -> &[u8] {
522522
let payload_length = self.payload_length() as usize;
523523

524-
let signature_size = if (self.incompatibility_flags() & MAVLINK_IFLAG_SIGNED) != 0 {
525-
Self::SIGNATURE_SIZE
526-
} else {
524+
let signature_size = if (self.incompatibility_flags() & MAVLINK_IFLAG_SIGNED) == 0 {
527525
0
526+
} else {
527+
Self::SIGNATURE_SIZE
528528
};
529529

530530
&self.0[..(1 + Self::HEADER_SIZE + payload_length + signature_size + 2)]

src/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
///
55
/// There must always be at least one remaining byte even if it is a
66
/// zero byte.
7-
pub(crate) fn remove_trailing_zeroes(data: &mut [u8]) -> usize {
7+
pub(crate) fn remove_trailing_zeroes(data: &[u8]) -> usize {
88
let mut len = data.len();
99

1010
for b in data[1..].iter().rev() {

tests/process_log_files.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ mod process_files {
3636
counter += 1;
3737
}
3838
Err(MessageReadError::Io(e)) => {
39-
if let std::io::ErrorKind::WouldBlock = e.kind() {
39+
if e.kind() == std::io::ErrorKind::WouldBlock {
4040
continue;
4141
} else {
4242
println!("recv error: {e:?}");

0 commit comments

Comments
 (0)