From 31764d26d88cde128b30a286460b8f459ff645a1 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 5 Mar 2025 11:25:26 -0600 Subject: [PATCH] style: Make clippy happy --- crates/lexarg/src/ext.rs | 25 +++++++++------- crates/lexarg/src/lib.rs | 18 +++++------ crates/libtest2-harness/src/case.rs | 2 +- crates/libtest2-harness/src/harness.rs | 22 +++++++------- crates/libtest2-harness/src/notify/json.rs | 2 +- crates/libtest2-harness/src/notify/summary.rs | 6 ++-- crates/libtest2-harness/src/shuffle.rs | 4 +-- crates/libtest2-harness/src/state.rs | 8 ++--- .../tests/testsuite/mixed_bag.rs | 30 +++++++++---------- crates/libtest2-mimic/tests/testsuite/util.rs | 8 ++--- crates/libtest2/tests/testsuite/mixed_bag.rs | 30 +++++++++---------- crates/libtest2/tests/testsuite/util.rs | 8 ++--- 12 files changed, 84 insertions(+), 79 deletions(-) diff --git a/crates/lexarg/src/ext.rs b/crates/lexarg/src/ext.rs index 9830acb..0c16c71 100644 --- a/crates/lexarg/src/ext.rs +++ b/crates/lexarg/src/ext.rs @@ -2,13 +2,14 @@ use std::ffi::OsStr; pub(crate) trait OsStrExt: private::Sealed { /// Converts to a string slice. - /// The Utf8Error is guaranteed to have a valid UTF8 boundary + /// The `Utf8Error` is guaranteed to have a valid UTF8 boundary /// in its `valid_up_to()` fn try_str(&self) -> Result<&str, std::str::Utf8Error>; /// Returns `true` if the given pattern matches a sub-slice of /// this string slice. /// /// Returns `false` if it does not. + #[allow(dead_code)] fn contains(&self, needle: &str) -> bool; /// Returns the byte index of the first character of this string slice that /// matches the pattern. @@ -29,6 +30,7 @@ pub(crate) trait OsStrExt: private::Sealed { fn starts_with(&self, prefix: &str) -> bool; /// An iterator over substrings of this string slice, separated by /// characters matched by a pattern. + #[allow(dead_code)] fn split<'s, 'n>(&'s self, needle: &'n str) -> Split<'s, 'n>; /// Splits the string on the first occurrence of the specified delimiter and /// returns prefix before delimiter and suffix after delimiter. @@ -92,17 +94,18 @@ impl OsStrExt for OsStr { } mod private { - pub trait Sealed {} + pub(crate) trait Sealed {} impl Sealed for std::ffi::OsStr {} } -pub struct Split<'s, 'n> { +#[allow(dead_code)] +pub(crate) struct Split<'s, 'n> { haystack: Option<&'s OsStr>, needle: &'n str, } -impl<'s, 'n> Iterator for Split<'s, 'n> { +impl<'s> Iterator for Split<'s, '_> { type Item = &'s OsStr; fn next(&mut self) -> Option { @@ -129,10 +132,12 @@ impl<'s, 'n> Iterator for Split<'s, 'n> { /// /// `index` must be at a valid UTF-8 boundary pub(crate) unsafe fn split_at(os: &OsStr, index: usize) -> (&OsStr, &OsStr) { - let bytes = os.as_encoded_bytes(); - let (first, second) = bytes.split_at(index); - ( - OsStr::from_encoded_bytes_unchecked(first), - OsStr::from_encoded_bytes_unchecked(second), - ) + unsafe { + let bytes = os.as_encoded_bytes(); + let (first, second) = bytes.split_at(index); + ( + OsStr::from_encoded_bytes_unchecked(first), + OsStr::from_encoded_bytes_unchecked(second), + ) + } } diff --git a/crates/lexarg/src/lib.rs b/crates/lexarg/src/lib.rs index 986ce28..6dc1052 100644 --- a/crates/lexarg/src/lib.rs +++ b/crates/lexarg/src/lib.rs @@ -417,7 +417,7 @@ enum State<'a> { Escaped, } -impl<'a> State<'a> { +impl State<'_> { #[cfg(test)] fn has_pending(&self) -> bool { match self { @@ -455,7 +455,7 @@ fn split_nonutf8_once(b: &OsStr) -> (&str, Option<&OsStr>) { } mod private { - use super::*; + use super::OsStr; pub trait Sealed {} impl Sealed for [S; C] where S: AsRef + std::fmt::Debug {} @@ -784,7 +784,7 @@ mod tests { } permutations = new; for permutation in &permutations { - println!("Starting {:?}", permutation); + println!("Starting {permutation:?}"); let p = Parser::new(permutation); exhaust(p, vec![]); } @@ -794,7 +794,7 @@ mod tests { /// Run many sequences of methods on a Parser. fn exhaust(parser: Parser<'_>, path: Vec) { if path.len() > 100 { - panic!("Stuck in loop: {:?}", path); + panic!("Stuck in loop: {path:?}"); } if parser.has_pending() { @@ -806,7 +806,7 @@ mod tests { "{next:?} via {path:?}", ); let mut path = path.clone(); - path.push(format!("pending-next-{:?}", next)); + path.push(format!("pending-next-{next:?}")); exhaust(parser, path); } @@ -815,7 +815,7 @@ mod tests { let next = parser.flag_value(); assert!(next.is_some(), "{next:?} via {path:?}",); let mut path = path; - path.push(format!("pending-value-{:?}", next)); + path.push(format!("pending-value-{next:?}")); exhaust(parser, path); } } else { @@ -832,8 +832,8 @@ mod tests { } _ => { let mut path = path.clone(); - path.push(format!("next-{:?}", next)); - exhaust(parser, path) + path.push(format!("next-{next:?}")); + exhaust(parser, path); } } } @@ -857,7 +857,7 @@ mod tests { "{next:?} via {path:?}", ); let mut path = path; - path.push(format!("value-{:?}", next)); + path.push(format!("value-{next:?}")); exhaust(parser, path); } } diff --git a/crates/libtest2-harness/src/case.rs b/crates/libtest2-harness/src/case.rs index f37a551..7dc9e94 100644 --- a/crates/libtest2-harness/src/case.rs +++ b/crates/libtest2-harness/src/case.rs @@ -1,4 +1,4 @@ -pub use crate::*; +pub(crate) use crate::*; pub trait Case: Send + Sync + 'static { /// The name of a test diff --git a/crates/libtest2-harness/src/harness.rs b/crates/libtest2-harness/src/harness.rs index 840919c..c1c7031 100644 --- a/crates/libtest2-harness/src/harness.rs +++ b/crates/libtest2-harness/src/harness.rs @@ -1,6 +1,6 @@ use libtest_lexarg::OutputFormat; -use crate::*; +use crate::{Case, RunError, RunMode, State, cli, notify, shuffle}; pub struct Harness { raw: Vec, @@ -33,7 +33,7 @@ impl Harness { pub fn main(mut self) -> ! { let mut parser = cli::Parser::new(&self.raw); let opts = parse(&mut parser).unwrap_or_else(|err| { - eprintln!("{}", err); + eprintln!("{err}"); std::process::exit(1) }); @@ -45,11 +45,11 @@ impl Harness { .write_global(); let mut notifier = notifier(&opts).unwrap_or_else(|err| { - eprintln!("{}", err); + eprintln!("{err}"); std::process::exit(1) }); discover(&opts, &mut self.cases, notifier.as_mut()).unwrap_or_else(|err| { - eprintln!("{}", err); + eprintln!("{err}"); std::process::exit(1) }); @@ -70,7 +70,7 @@ impl Harness { const ERROR_EXIT_CODE: i32 = 101; -fn parse(parser: &mut cli::Parser) -> cli::Result { +fn parse(parser: &mut cli::Parser<'_>) -> cli::Result { let mut test_opts = libtest_lexarg::TestOptsParseState::new(); let bin = parser.bin(); @@ -194,7 +194,7 @@ fn discover( retain_cases.push(retain_case); notifier.notify(notify::Event::DiscoverCase { name: case.name().to_owned(), - mode: notify::RunMode::Test, + mode: RunMode::Test, run: retain_case, })?; } @@ -253,8 +253,8 @@ fn run( "`--test` and `-bench` are mutually exclusive", )); } - (true, false) => notify::RunMode::Test, - (false, true) => notify::RunMode::Bench, + (true, false) => RunMode::Test, + (false, true) => RunMode::Bench, (false, false) => unreachable!("libtest-lexarg` should always ensure at least one is set"), }; state.set_mode(mode); @@ -309,7 +309,7 @@ fn run( let case = remaining.pop_front().unwrap(); let name = case.name().to_owned(); - let cfg = std::thread::Builder::new().name(name.to_owned()); + let cfg = std::thread::Builder::new().name(name.clone()); let tx = tx.clone(); let case = std::sync::Arc::new(case); let case_fallback = case.clone(); @@ -402,7 +402,7 @@ fn run_case( let msg = match payload { Some(payload) => format!("test panicked: {payload}"), - None => "test panicked".to_string(), + None => "test panicked".to_owned(), }; Err(RunError::fail(msg)) }); @@ -412,7 +412,7 @@ fn run_case( let message = err.and_then(|e| e.cause().map(|c| c.to_string())); notifier.notify(notify::Event::CaseComplete { name: case.name().to_owned(), - mode: notify::RunMode::Test, + mode: RunMode::Test, status, message, elapsed_s: Some(notify::Elapsed(timer.elapsed())), diff --git a/crates/libtest2-harness/src/notify/json.rs b/crates/libtest2-harness/src/notify/json.rs index 3597933..69eb9af 100644 --- a/crates/libtest2-harness/src/notify/json.rs +++ b/crates/libtest2-harness/src/notify/json.rs @@ -14,7 +14,7 @@ impl JsonNotifier { impl super::Notifier for JsonNotifier { fn notify(&mut self, event: Event) -> std::io::Result<()> { let event = serde_json::to_string(&event)?; - writeln!(self.writer, "{}", event)?; + writeln!(self.writer, "{event}")?; Ok(()) } } diff --git a/crates/libtest2-harness/src/notify/summary.rs b/crates/libtest2-harness/src/notify/summary.rs index e684c11..0de6cc8 100644 --- a/crates/libtest2-harness/src/notify/summary.rs +++ b/crates/libtest2-harness/src/notify/summary.rs @@ -59,8 +59,8 @@ impl Summary { // Print messages of all tests for (name, msg) in &self.failures { if let Some(msg) = msg { - writeln!(writer, "---- {} ----", name)?; - writeln!(writer, "{}", msg)?; + writeln!(writer, "---- {name} ----")?; + writeln!(writer, "{msg}")?; writeln!(writer)?; } } @@ -69,7 +69,7 @@ impl Summary { writeln!(writer)?; writeln!(writer, "failures:")?; for name in self.failures.keys() { - writeln!(writer, " {}", name)?; + writeln!(writer, " {name}")?; } } writeln!(writer)?; diff --git a/crates/libtest2-harness/src/shuffle.rs b/crates/libtest2-harness/src/shuffle.rs index 9dbf710..444e664 100644 --- a/crates/libtest2-harness/src/shuffle.rs +++ b/crates/libtest2-harness/src/shuffle.rs @@ -4,7 +4,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; use crate::Case; -pub fn get_shuffle_seed(opts: &libtest_lexarg::TestOpts) -> Option { +pub(crate) fn get_shuffle_seed(opts: &libtest_lexarg::TestOpts) -> Option { opts.shuffle_seed.or_else(|| { opts.shuffle.then(|| { SystemTime::now() @@ -15,7 +15,7 @@ pub fn get_shuffle_seed(opts: &libtest_lexarg::TestOpts) -> Option { }) } -pub fn shuffle_tests(shuffle_seed: u64, tests: &mut [Box]) { +pub(crate) fn shuffle_tests(shuffle_seed: u64, tests: &mut [Box]) { let test_names: Vec<&str> = tests.iter().map(|test| test.name()).collect(); let test_names_hash = calculate_hash(&test_names); let mut rng = Rng::new(shuffle_seed, test_names_hash); diff --git a/crates/libtest2-harness/src/state.rs b/crates/libtest2-harness/src/state.rs index 939bba3..3253954 100644 --- a/crates/libtest2-harness/src/state.rs +++ b/crates/libtest2-harness/src/state.rs @@ -1,8 +1,8 @@ -pub use crate::*; +pub(crate) use crate::*; #[derive(Debug)] pub struct State { - mode: notify::RunMode, + mode: RunMode, run_ignored: bool, } @@ -23,7 +23,7 @@ impl State { } } - pub fn current_mode(&self) -> notify::RunMode { + pub fn current_mode(&self) -> RunMode { self.mode } } @@ -36,7 +36,7 @@ impl State { } } - pub(crate) fn set_mode(&mut self, mode: notify::RunMode) { + pub(crate) fn set_mode(&mut self, mode: RunMode) { self.mode = mode; } diff --git a/crates/libtest2-mimic/tests/testsuite/mixed_bag.rs b/crates/libtest2-mimic/tests/testsuite/mixed_bag.rs index 35eb643..38e5938 100644 --- a/crates/libtest2-mimic/tests/testsuite/mixed_bag.rs +++ b/crates/libtest2-mimic/tests/testsuite/mixed_bag.rs @@ -101,7 +101,7 @@ failures: test result: FAILED. 2 passed; 1 failed; 5 ignored; 0 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -148,7 +148,7 @@ failures: test result: FAILED. 2 passed; 1 failed; 5 ignored; 0 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -195,7 +195,7 @@ failures: test result: FAILED. 2 passed; 1 failed; 5 ignored; 0 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -227,7 +227,7 @@ owl: test 8 tests "#, - ) + ); } #[test] @@ -302,7 +302,7 @@ running 2 tests test result: ok. 1 passed; 0 failed; 1 ignored; 6 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -376,7 +376,7 @@ failures: test result: FAILED. 2 passed; 2 failed; 0 ignored; 4 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -427,7 +427,7 @@ failures: test result: FAILED. 2 passed; 2 failed; 0 ignored; 4 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -498,7 +498,7 @@ failures: test result: FAILED. 4 passed; 4 failed; 0 ignored; 0 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -569,7 +569,7 @@ failures: test result: FAILED. 4 passed; 4 failed; 0 ignored; 0 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -610,7 +610,7 @@ failures: test result: FAILED. 1 passed; 1 failed; 0 ignored; 6 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -641,7 +641,7 @@ fn list_json() { {"event":"discover-case","name":"owl","mode":"test","run":false} {"event":"discover-complete","elapsed_s":"[..]","seed":null} "#, - ) + ); } #[test] @@ -684,7 +684,7 @@ fn test_json() { [..] {"event":"suite-complete","elapsed_s":"[..]"} "#, - ) + ); } #[test] @@ -705,7 +705,7 @@ cat: test 2 tests "#, - ) + ); } #[test] @@ -732,7 +732,7 @@ fn test_junit() { "#, - ) + ); } #[test] @@ -770,7 +770,7 @@ failures: test result: FAILED. 2 passed; 1 failed; 5 ignored; 0 filtered out; finished in [..]s "#, - ) + ); } #[test] diff --git a/crates/libtest2-mimic/tests/testsuite/util.rs b/crates/libtest2-mimic/tests/testsuite/util.rs index b7ebdd4..993777e 100644 --- a/crates/libtest2-mimic/tests/testsuite/util.rs +++ b/crates/libtest2-mimic/tests/testsuite/util.rs @@ -70,7 +70,7 @@ fn tempdir() -> std::path::PathBuf { } mod tests { - pub fn compile_test( + pub(super) fn compile_test( manifest_path: &std::path::Path, target_name: &str, args: impl IntoIterator>, @@ -93,12 +93,12 @@ mod tests { } } - panic!("Unknown error building test {}", target_name) + panic!("Unknown error building test {target_name}") } #[allow(clippy::type_complexity)] fn decode_test_message<'m>( - message: &'m escargot::format::Message, + message: &'m escargot::format::Message<'_>, ) -> Option<(&'m str, std::path::PathBuf)> { match message { escargot::format::Message::CompilerMessage(msg) => { @@ -133,7 +133,7 @@ mod tests { } } - fn is_test_target(target: &escargot::format::Target) -> bool { + fn is_test_target(target: &escargot::format::Target<'_>) -> bool { target.crate_types == ["bin"] && target.kind == ["test"] } } diff --git a/crates/libtest2/tests/testsuite/mixed_bag.rs b/crates/libtest2/tests/testsuite/mixed_bag.rs index f2bb2bb..97a3e3d 100644 --- a/crates/libtest2/tests/testsuite/mixed_bag.rs +++ b/crates/libtest2/tests/testsuite/mixed_bag.rs @@ -108,7 +108,7 @@ failures: test result: FAILED. 2 passed; 1 failed; 5 ignored; 0 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -155,7 +155,7 @@ failures: test result: FAILED. 2 passed; 1 failed; 5 ignored; 0 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -202,7 +202,7 @@ failures: test result: FAILED. 2 passed; 1 failed; 5 ignored; 0 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -234,7 +234,7 @@ owl: test 8 tests "#, - ) + ); } #[test] @@ -309,7 +309,7 @@ running 2 tests test result: ok. 1 passed; 0 failed; 1 ignored; 6 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -383,7 +383,7 @@ failures: test result: FAILED. 2 passed; 2 failed; 0 ignored; 4 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -434,7 +434,7 @@ failures: test result: FAILED. 2 passed; 2 failed; 0 ignored; 4 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -505,7 +505,7 @@ failures: test result: FAILED. 4 passed; 4 failed; 0 ignored; 0 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -576,7 +576,7 @@ failures: test result: FAILED. 4 passed; 4 failed; 0 ignored; 0 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -617,7 +617,7 @@ failures: test result: FAILED. 1 passed; 1 failed; 0 ignored; 6 filtered out; finished in [..]s "#, - ) + ); } #[test] @@ -648,7 +648,7 @@ fn list_json() { {"event":"discover-case","name":"owl","mode":"test","run":false} {"event":"discover-complete","elapsed_s":"[..]","seed":null} "#, - ) + ); } #[test] @@ -691,7 +691,7 @@ fn test_json() { [..] {"event":"suite-complete","elapsed_s":"[..]"} "#, - ) + ); } #[test] @@ -712,7 +712,7 @@ cat: test 2 tests "#, - ) + ); } #[test] @@ -739,7 +739,7 @@ fn test_junit() { "#, - ) + ); } #[test] @@ -777,7 +777,7 @@ failures: test result: FAILED. 2 passed; 1 failed; 5 ignored; 0 filtered out; finished in [..]s "#, - ) + ); } #[test] diff --git a/crates/libtest2/tests/testsuite/util.rs b/crates/libtest2/tests/testsuite/util.rs index a4a81c3..158d58a 100644 --- a/crates/libtest2/tests/testsuite/util.rs +++ b/crates/libtest2/tests/testsuite/util.rs @@ -70,7 +70,7 @@ fn tempdir() -> std::path::PathBuf { } mod tests { - pub fn compile_test( + pub(super) fn compile_test( manifest_path: &std::path::Path, target_name: &str, args: impl IntoIterator>, @@ -93,12 +93,12 @@ mod tests { } } - panic!("Unknown error building test {}", target_name) + panic!("Unknown error building test {target_name}") } #[allow(clippy::type_complexity)] fn decode_test_message<'m>( - message: &'m escargot::format::Message, + message: &'m escargot::format::Message<'_>, ) -> Option<(&'m str, std::path::PathBuf)> { match message { escargot::format::Message::CompilerMessage(msg) => { @@ -133,7 +133,7 @@ mod tests { } } - fn is_test_target(target: &escargot::format::Target) -> bool { + fn is_test_target(target: &escargot::format::Target<'_>) -> bool { target.crate_types == ["bin"] && target.kind == ["test"] } }