Skip to content

Commit a65d514

Browse files
committed
small fixes
1 parent 4560744 commit a65d514

File tree

5 files changed

+42
-39
lines changed

5 files changed

+42
-39
lines changed

ci/svd2rust-regress/src/diff.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ impl Diffing {
146146
}
147147
})
148148
.filter(|t| {
149-
if !self.chip.is_empty() {
149+
if self.chip.is_empty() {
150+
false
151+
} else {
150152
self.chip.iter().any(|c| {
151153
wildmatch::WildMatch::new(&c.to_ascii_lowercase())
152154
.matches(&t.chip.to_ascii_lowercase())
153155
})
154-
} else {
155-
false
156156
}
157157
})
158158
.collect::<Vec<_>>();
@@ -206,7 +206,7 @@ impl Diffing {
206206
// FIXME: refactor this to be less ugly
207207
let [baseline_sc, current_sc] = self.get_source_and_command();
208208
let baseline = match baseline_sc.and_then(|(source, _)| source) {
209-
reference @ None | reference @ Some("" | "master") => {
209+
reference @ (None | Some("" | "master")) => {
210210
github::get_release_binary_artifact(reference.unwrap_or("master"), &opts.output_dir)
211211
.with_context(|| "couldn't get svd2rust latest unreleased artifact")?
212212
}

ci/svd2rust-regress/src/main.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub fn get_cargo_metadata() -> &'static CargoMetadata {
4444
}
4545

4646
/// Returns the cargo workspace for the manifest
47+
#[must_use]
4748
pub fn get_cargo_workspace() -> &'static std::path::Path {
4849
&get_cargo_metadata().workspace_root
4950
}
@@ -129,11 +130,11 @@ impl TestOpts {
129130
})
130131
// Specify chip - note: may match multiple
131132
.filter(|t| {
132-
if !self.chip.is_empty() {
133-
self.chip.iter().any(|c| WildMatch::new(c).matches(&t.chip))
134-
} else {
133+
if self.chip.is_empty() {
135134
// Don't run failable tests unless wanted
136135
self.bad_tests || t.should_pass
136+
} else {
137+
self.chip.iter().any(|c| WildMatch::new(c).matches(&t.chip))
137138
}
138139
})
139140
.collect::<Vec<_>>();
@@ -190,10 +191,10 @@ impl TestOpts {
190191
read_file(stderr, &mut buf);
191192
buf
192193
}
193-
_ => "".into(),
194+
_ => String::new(),
194195
}
195196
} else {
196-
"".into()
197+
String::new()
197198
};
198199
tracing::error!(
199200
"Failed: {} - {} seconds. {:?}{}",
@@ -253,38 +254,39 @@ pub struct Opts {
253254
}
254255

255256
impl Opts {
256-
fn use_rustfmt(&self) -> bool {
257+
const fn use_rustfmt(&self) -> bool {
257258
match self.subcommand {
258-
Subcommand::Tests(TestOpts { format, .. }) => format,
259-
Subcommand::Diff(Diffing { format, .. }) => format,
260-
Subcommand::Ci(Ci { format, .. }) => format,
259+
Subcommand::Tests(TestOpts { format, .. })
260+
| Subcommand::Diff(Diffing { format, .. })
261+
| Subcommand::Ci(Ci { format, .. }) => format,
261262
}
262263
}
263264

264-
fn use_form(&self) -> bool {
265+
const fn use_form(&self) -> bool {
265266
match self.subcommand {
266-
Subcommand::Tests(TestOpts { form_lib, .. }) => form_lib,
267-
Subcommand::Diff(Diffing {
267+
Subcommand::Tests(TestOpts { form_lib, .. })
268+
| Subcommand::Diff(Diffing {
268269
form_split: form_lib,
269270
..
270-
}) => form_lib,
271-
Subcommand::Ci(Ci { form_lib, .. }) => form_lib,
271+
})
272+
| Subcommand::Ci(Ci { form_lib, .. }) => form_lib,
272273
}
273274
}
274275
}
275276

276277
/// Hack to use ci/tests.yml as default value when running as `cargo run`
277278
fn default_test_cases() -> std::ffi::OsString {
278-
std::env::var_os("CARGO_MANIFEST_DIR")
279-
.map(|mut e| {
279+
std::env::var_os("CARGO_MANIFEST_DIR").map_or_else(
280+
|| std::ffi::OsString::from("tests.yml".to_owned()),
281+
|mut e| {
280282
e.extend([std::ffi::OsStr::new("/tests.yml")]);
281283
std::path::PathBuf::from(e)
282284
.strip_prefix(std::env::current_dir().unwrap())
283285
.unwrap()
284286
.to_owned()
285287
.into_os_string()
286-
})
287-
.unwrap_or_else(|| std::ffi::OsString::from("tests.yml".to_owned()))
288+
},
289+
)
288290
}
289291

290292
fn default_svd2rust() -> std::ffi::OsString {
@@ -326,9 +328,7 @@ fn validate_tests(tests: &[tests::TestCase]) {
326328
}
327329
}
328330

329-
if fail {
330-
panic!("Tests failed validation");
331-
}
331+
assert!(!fail, "Tests failed validation");
332332
}
333333

334334
fn read_file(path: &PathBuf, buf: &mut String) {
@@ -376,9 +376,10 @@ fn main() -> Result<(), anyhow::Error> {
376376
}
377377
(&None, true) => {
378378
// FIXME: Use Option::filter instead when stable, rust-lang/rust#45860
379-
if !default_rustfmt.iter().any(|p| p.is_file()) {
380-
panic!("No rustfmt found");
381-
}
379+
assert!(
380+
default_rustfmt.iter().any(|p| p.is_file()),
381+
"No rustfmt found"
382+
);
382383
if let Some(default_rustfmt) = default_rustfmt {
383384
RUSTFMT.get_or_init(|| default_rustfmt);
384385
}

ci/svd2rust-regress/src/svd_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ impl TestCase {
216216
let svd_url = &self.svd_url();
217217
let svd = reqwest::blocking::get(svd_url)
218218
.with_context(|| format!("Failed to get svd URL: {svd_url}"))?
219+
.error_for_status()
220+
.with_context(|| anyhow!("Response is not ok for svd url"))?
219221
.text()
220222
.with_context(|| "SVD is bad text")?;
221-
if svd == "404: Not Found" {
222-
return Err(anyhow!("Failed to get svd URL: {svd_url}. {svd}").into());
223-
}
223+
224224
let chip_svd = format!("{}.svd", &self.chip);
225225
let svd_file = path_helper_base(&chip_dir, &[&chip_svd]);
226226
file_helper(&svd, &svd_file)?;
@@ -262,7 +262,7 @@ impl TestCase {
262262
Target::CortexM | Target::Mips | Target::Msp430 | Target::XtensaLX => {
263263
// TODO: Give error the path to stderr
264264
fs::rename(path_helper_base(&chip_dir, &["lib.rs"]), &lib_rs_file)
265-
.with_context(|| "While moving lib.rs file")?
265+
.with_context(|| "While moving lib.rs file")?;
266266
}
267267
_ => {}
268268
}

ci/svd2rust-regress/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl TestCase {
8686
}
8787
}
8888

89-
pub fn should_run(&self, short_test: bool) -> bool {
89+
pub const fn should_run(&self, short_test: bool) -> bool {
9090
match (&self.run_when, short_test) {
9191
(&Always, _) => true,
9292
(&NotShort, true) => false,

src/util.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub fn respace(s: &str) -> String {
144144

145145
pub fn escape_brackets(s: &str) -> String {
146146
s.split('[')
147-
.fold("".to_string(), |acc, x| {
147+
.fold(String::new(), |acc, x| {
148148
if acc.is_empty() {
149149
x.to_string()
150150
} else if acc.ends_with('\\') {
@@ -154,7 +154,7 @@ pub fn escape_brackets(s: &str) -> String {
154154
}
155155
})
156156
.split(']')
157-
.fold("".to_string(), |acc, x| {
157+
.fold(String::new(), |acc, x| {
158158
if acc.is_empty() {
159159
x.to_string()
160160
} else if acc.ends_with('\\') {
@@ -445,11 +445,13 @@ pub fn peripheral_names(d: &Device) -> Vec<String> {
445445
for p in &d.peripherals {
446446
match p {
447447
Peripheral::Single(info) => {
448-
v.push(replace_suffix(&info.name.to_sanitized_snake_case(), ""))
448+
v.push(replace_suffix(&info.name.to_sanitized_snake_case(), ""));
449+
}
450+
Peripheral::Array(info, dim) => {
451+
v.extend(
452+
svd_rs::array::names(info, dim).map(|n| n.to_sanitized_snake_case().into()),
453+
);
449454
}
450-
Peripheral::Array(info, dim) => v.extend(
451-
svd_rs::array::names(info, dim).map(|n| n.to_sanitized_snake_case().into()),
452-
),
453455
}
454456
}
455457
v.sort();

0 commit comments

Comments
 (0)