Skip to content

Commit 29bcfd8

Browse files
authored
Merge pull request #871 from rust-embedded/clippy182
clippy
2 parents f4a317e + 8aadb82 commit 29bcfd8

File tree

6 files changed

+63
-34
lines changed

6 files changed

+63
-34
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
99

10+
- Add warning about indexing register arrays
11+
- Skip generating `.add(0)` and `1 *` in accessors
1012
- Bump MSRV of generated code to 1.76
1113
- move `must_use` from methods to generic type
1214

src/generate/interrupt.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ pub fn render(
6262
interrupt
6363
.0
6464
.description
65-
.as_ref()
66-
.map(|s| util::respace(s))
67-
.as_ref()
68-
.map(|s| util::escape_special_chars(s))
65+
.as_deref()
66+
.map(util::respace)
67+
.as_deref()
68+
.map(util::escape_special_chars)
6969
.unwrap_or_else(|| interrupt.0.name.clone())
7070
);
7171

src/generate/peripheral.rs

+25-17
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl Region {
305305
let mut idents: Vec<_> = self
306306
.rbfs
307307
.iter()
308-
.filter_map(|f| f.syn_field.ident.as_ref().map(|ident| ident.to_string()))
308+
.filter_map(|f| f.syn_field.ident.as_ref().map(ToString::to_string))
309309
.collect();
310310
if idents.is_empty() {
311311
return None;
@@ -343,7 +343,7 @@ impl Region {
343343
let idents: Vec<_> = self
344344
.rbfs
345345
.iter()
346-
.filter_map(|f| f.syn_field.ident.as_ref().map(|ident| ident.to_string()))
346+
.filter_map(|f| f.syn_field.ident.as_ref().map(ToString::to_string))
347347
.collect();
348348

349349
if idents.is_empty() {
@@ -726,7 +726,7 @@ fn check_erc_derive_infos(
726726
};
727727
match register {
728728
Register::Single(_) => {
729-
let ty_name = info_name.to_string();
729+
let ty_name = info_name.clone();
730730
*derive_info = match explicit_rpath {
731731
None => {
732732
match compare_this_against_prev(
@@ -758,7 +758,7 @@ fn check_erc_derive_infos(
758758
let re = Regex::new(format!("^{re_string}$").as_str()).map_err(|_| {
759759
anyhow!("Error creating regex for register {}", register.name)
760760
})?;
761-
let ty_name = info_name.to_string(); // keep suffix for regex matching
761+
let ty_name = info_name.clone(); // keep suffix for regex matching
762762
*derive_info = match explicit_rpath {
763763
None => {
764764
match compare_this_against_prev(
@@ -787,7 +787,7 @@ fn check_erc_derive_infos(
787787
}
788788
RegisterCluster::Cluster(cluster) => {
789789
*derive_info = DeriveInfo::Cluster;
790-
ercs_type_info.push((cluster.name.to_string(), None, erc, derive_info));
790+
ercs_type_info.push((cluster.name.clone(), None, erc, derive_info));
791791
}
792792
};
793793
}
@@ -998,7 +998,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
998998
doc,
999999
name,
10001000
ty,
1001-
offset: unsuffixed(info.address_offset),
1001+
offset: info.address_offset,
10021002
})
10031003
.raw_if(false);
10041004
cluster_expanded.push(RegisterBlockField {
@@ -1052,14 +1052,19 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
10521052
&description,
10531053
);
10541054
let mut accessors = Vec::with_capacity((array_info.dim + 1) as _);
1055+
let first_name = svd::array::names(info, array_info).next().unwrap();
1056+
let note = (array_info.indexes().next().unwrap() != "0").then(||
1057+
format!("<div class=\"warning\">`n` is the index of {0} in the array. `n == 0` corresponds to `{first_name}` {0}.</div>", "cluster")
1058+
);
10551059
accessors.push(
10561060
Accessor::Array(ArrayAccessor {
10571061
doc,
10581062
name: accessor_name.clone(),
10591063
ty: ty.clone(),
1060-
offset: unsuffixed(info.address_offset),
1061-
dim: unsuffixed(array_info.dim),
1062-
increment: unsuffixed(array_info.dim_increment),
1064+
offset: info.address_offset,
1065+
dim: array_info.dim,
1066+
increment: array_info.dim_increment,
1067+
note,
10631068
})
10641069
.raw_if(!array_convertible),
10651070
);
@@ -1071,7 +1076,6 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
10711076
ci.address_offset,
10721077
ci.description.as_deref().unwrap_or(&ci.name),
10731078
);
1074-
let i = unsuffixed(i as u64);
10751079
accessors.push(
10761080
Accessor::ArrayElem(ArrayElemAccessor {
10771081
doc,
@@ -1114,7 +1118,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
11141118
doc,
11151119
name,
11161120
ty: ty.clone(),
1117-
offset: unsuffixed(info.address_offset),
1121+
offset: info.address_offset,
11181122
})
11191123
.raw_if(false);
11201124
cluster_expanded.push(RegisterBlockField {
@@ -1166,7 +1170,7 @@ fn expand_register(
11661170
doc,
11671171
name,
11681172
ty,
1169-
offset: unsuffixed(info.address_offset),
1173+
offset: info.address_offset,
11701174
})
11711175
.raw_if(false);
11721176
register_expanded.push(RegisterBlockField {
@@ -1235,14 +1239,19 @@ fn expand_register(
12351239
&description,
12361240
);
12371241
let mut accessors = Vec::with_capacity((array_info.dim + 1) as _);
1242+
let first_name = svd::array::names(info, array_info).next().unwrap();
1243+
let note = (array_info.indexes().next().unwrap() != "0").then(||
1244+
format!("<div class=\"warning\">`n` is the index of {0} in the array. `n == 0` corresponds to `{first_name}` {0}.</div>", "register")
1245+
);
12381246
accessors.push(
12391247
Accessor::Array(ArrayAccessor {
12401248
doc,
12411249
name: accessor_name.clone(),
12421250
ty: ty.clone(),
1243-
offset: unsuffixed(info.address_offset),
1244-
dim: unsuffixed(array_info.dim),
1245-
increment: unsuffixed(array_info.dim_increment),
1251+
offset: info.address_offset,
1252+
dim: array_info.dim,
1253+
increment: array_info.dim_increment,
1254+
note,
12461255
})
12471256
.raw_if(!array_convertible),
12481257
);
@@ -1259,7 +1268,6 @@ fn expand_register(
12591268
ri.address_offset,
12601269
ri.description.as_deref().unwrap_or(&ri.name),
12611270
);
1262-
let i = unsuffixed(i as u64);
12631271
accessors.push(
12641272
Accessor::ArrayElem(ArrayElemAccessor {
12651273
doc,
@@ -1302,7 +1310,7 @@ fn expand_register(
13021310
doc,
13031311
name,
13041312
ty: ty.clone(),
1305-
offset: unsuffixed(info.address_offset),
1313+
offset: info.address_offset,
13061314
})
13071315
.raw_if(false);
13081316
register_expanded.push(RegisterBlockField {

src/generate/peripheral/accessor.rs

+27-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use proc_macro2::{Ident, Span, TokenStream};
22
use quote::{quote, ToTokens};
33

4+
use crate::util::unsuffixed;
5+
46
#[derive(Clone, Debug)]
57
pub enum Accessor {
68
Reg(RegAccessor),
@@ -51,18 +53,24 @@ impl ToTokens for AccessType {
5153
ty,
5254
offset,
5355
})) => {
56+
let offset = (*offset != 0).then(|| unsuffixed(*offset)).map(|o| quote!(.add(#o)));
5457
quote! {
5558
#[doc = #doc]
5659
#[inline(always)]
5760
pub const fn #name(&self) -> &#ty {
58-
unsafe { &*core::ptr::from_ref(self).cast::<u8>().add(#offset).cast() }
61+
unsafe { &*core::ptr::from_ref(self).cast::<u8>() #offset .cast() }
5962
}
6063
}
6164
}
62-
Self::Ref(Accessor::Array(ArrayAccessor { doc, name, ty, .. })) => {
65+
Self::Ref(Accessor::Array(ArrayAccessor { doc, name, ty, note, .. })) => {
6366
let name_iter = Ident::new(&format!("{name}_iter"), Span::call_site());
67+
let note = note.as_ref().map(|note| quote! {
68+
#[doc = ""]
69+
#[doc = #note]
70+
});
6471
quote! {
6572
#[doc = #doc]
73+
#note
6674
#[inline(always)]
6775
pub const fn #name(&self, n: usize) -> &#ty {
6876
&self.#name[n]
@@ -82,11 +90,20 @@ impl ToTokens for AccessType {
8290
offset,
8391
dim,
8492
increment,
93+
note,
8594
})) => {
8695
let name_iter = Ident::new(&format!("{name}_iter"), Span::call_site());
87-
let cast = quote! { unsafe { &*core::ptr::from_ref(self).cast::<u8>().add(#offset).add(#increment * n).cast() } };
96+
let offset = (*offset != 0).then(|| unsuffixed(*offset)).map(|o| quote!(.add(#o)));
97+
let dim = unsuffixed(*dim);
98+
let increment = (*increment != 1).then(|| unsuffixed(*increment)).map(|i| quote!(#i *));
99+
let note = note.as_ref().map(|note| quote! {
100+
#[doc = ""]
101+
#[doc = #note]
102+
});
103+
let cast = quote! { unsafe { &*core::ptr::from_ref(self).cast::<u8>() #offset .add(#increment n).cast() } };
88104
quote! {
89105
#[doc = #doc]
106+
#note
90107
#[inline(always)]
91108
pub const fn #name(&self, n: usize) -> &#ty {
92109
#[allow(clippy::no_effect)]
@@ -109,6 +126,7 @@ impl ToTokens for AccessType {
109126
basename,
110127
i,
111128
} = elem;
129+
let i = unsuffixed(*i as u64);
112130
quote! {
113131
#[doc = #doc]
114132
#[inline(always)]
@@ -127,17 +145,18 @@ pub struct RegAccessor {
127145
pub doc: String,
128146
pub name: Ident,
129147
pub ty: syn::Type,
130-
pub offset: syn::LitInt,
148+
pub offset: u32,
131149
}
132150

133151
#[derive(Clone, Debug)]
134152
pub struct ArrayAccessor {
135153
pub doc: String,
136154
pub name: Ident,
137155
pub ty: syn::Type,
138-
pub offset: syn::LitInt,
139-
pub dim: syn::LitInt,
140-
pub increment: syn::LitInt,
156+
pub offset: u32,
157+
pub dim: u32,
158+
pub increment: u32,
159+
pub note: Option<String>,
141160
}
142161

143162
#[derive(Clone, Debug)]
@@ -146,5 +165,5 @@ pub struct ArrayElemAccessor {
146165
pub name: Ident,
147166
pub ty: syn::Type,
148167
pub basename: Ident,
149-
pub i: syn::LitInt,
168+
pub i: usize,
150169
}

src/generate/register.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::svd::{
33
ModifiedWriteValues, ReadAction, Register, RegisterProperties, Usage, WriteConstraint,
44
WriteConstraintRange,
55
};
6-
use core::u64;
76
use log::warn;
87
use proc_macro2::{Delimiter, Group, Ident, Span, TokenStream};
98
use quote::quote;
@@ -117,7 +116,7 @@ pub fn render(
117116
register.properties.reset_value.is_some(),
118117
&mod_ty,
119118
false,
120-
&register,
119+
register,
121120
&rpath,
122121
config,
123122
)?,
@@ -162,6 +161,7 @@ fn read_action_docs(can_read: bool, read_action: Option<ReadAction>) -> String {
162161
doc
163162
}
164163

164+
#[allow(clippy::too_many_arguments)]
165165
fn api_docs(
166166
can_read: bool,
167167
can_write: bool,
@@ -222,7 +222,7 @@ fn api_docs(
222222
let idx = format!("[{idx}]");
223223
rpath.name.replace("[%s]", &idx).replace("%s", &idx)
224224
} else {
225-
rpath.name.to_string()
225+
rpath.name.clone()
226226
};
227227
// TODO: support html_urls for registers in cluster
228228
if rpath.block.path.is_empty() {

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
//!
3838
//! - `build.rs`, build script that places `device.x` somewhere the linker can find.
3939
//! - `device.x`, linker script that weakly aliases all the interrupt handlers to the default
40-
//! exception handler (`DefaultHandler`).
40+
//! exception handler (`DefaultHandler`).
4141
//! - `lib.rs`, the generated code.
4242
//!
4343
//! All these files must be included in the same device crate. The `lib.rs` file contains several
@@ -95,7 +95,7 @@
9595
//!
9696
//! - `build.rs`, build script that places `device.x` somewhere the linker can find.
9797
//! - `device.x`, linker script that weakly aliases all the interrupt handlers to the default
98-
//! exception handler (`DefaultHandler`).
98+
//! exception handler (`DefaultHandler`).
9999
//! - `lib.rs`, the generated code.
100100
//!
101101
//! All these files must be included in the same device crate. The `lib.rs` file contains several

0 commit comments

Comments
 (0)