Skip to content

Commit

Permalink
style: Make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Mar 5, 2025
1 parent a42c3a7 commit 31764d2
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 79 deletions.
25 changes: 15 additions & 10 deletions crates/lexarg/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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<Self::Item> {
Expand All @@ -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),
)
}
}
18 changes: 9 additions & 9 deletions crates/lexarg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ enum State<'a> {
Escaped,
}

impl<'a> State<'a> {
impl State<'_> {
#[cfg(test)]
fn has_pending(&self) -> bool {
match self {
Expand Down Expand Up @@ -455,7 +455,7 @@ fn split_nonutf8_once(b: &OsStr) -> (&str, Option<&OsStr>) {
}

mod private {
use super::*;
use super::OsStr;

pub trait Sealed {}
impl<const C: usize, S> Sealed for [S; C] where S: AsRef<OsStr> + std::fmt::Debug {}
Expand Down Expand Up @@ -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![]);
}
Expand All @@ -794,7 +794,7 @@ mod tests {
/// Run many sequences of methods on a Parser.
fn exhaust(parser: Parser<'_>, path: Vec<String>) {
if path.len() > 100 {
panic!("Stuck in loop: {:?}", path);
panic!("Stuck in loop: {path:?}");
}

if parser.has_pending() {
Expand All @@ -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);
}

Expand All @@ -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 {
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libtest2-harness/src/case.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use crate::*;
pub(crate) use crate::*;

pub trait Case: Send + Sync + 'static {
/// The name of a test
Expand Down
22 changes: 11 additions & 11 deletions crates/libtest2-harness/src/harness.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use libtest_lexarg::OutputFormat;

use crate::*;
use crate::{Case, RunError, RunMode, State, cli, notify, shuffle};

pub struct Harness {
raw: Vec<std::ffi::OsString>,
Expand Down Expand Up @@ -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)
});

Expand All @@ -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)
});

Expand All @@ -70,7 +70,7 @@ impl Harness {

const ERROR_EXIT_CODE: i32 = 101;

fn parse(parser: &mut cli::Parser) -> cli::Result<libtest_lexarg::TestOpts> {
fn parse(parser: &mut cli::Parser<'_>) -> cli::Result<libtest_lexarg::TestOpts> {
let mut test_opts = libtest_lexarg::TestOptsParseState::new();

let bin = parser.bin();
Expand Down Expand Up @@ -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,
})?;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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))
});
Expand All @@ -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())),
Expand Down
2 changes: 1 addition & 1 deletion crates/libtest2-harness/src/notify/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl<W: std::io::Write> JsonNotifier<W> {
impl<W: std::io::Write> super::Notifier for JsonNotifier<W> {
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(())
}
}
6 changes: 3 additions & 3 deletions crates/libtest2-harness/src/notify/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
}
Expand All @@ -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)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/libtest2-harness/src/shuffle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::time::{SystemTime, UNIX_EPOCH};

use crate::Case;

pub fn get_shuffle_seed(opts: &libtest_lexarg::TestOpts) -> Option<u64> {
pub(crate) fn get_shuffle_seed(opts: &libtest_lexarg::TestOpts) -> Option<u64> {
opts.shuffle_seed.or_else(|| {
opts.shuffle.then(|| {
SystemTime::now()
Expand All @@ -15,7 +15,7 @@ pub fn get_shuffle_seed(opts: &libtest_lexarg::TestOpts) -> Option<u64> {
})
}

pub fn shuffle_tests(shuffle_seed: u64, tests: &mut [Box<dyn Case>]) {
pub(crate) fn shuffle_tests(shuffle_seed: u64, tests: &mut [Box<dyn Case>]) {
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);
Expand Down
8 changes: 4 additions & 4 deletions crates/libtest2-harness/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub use crate::*;
pub(crate) use crate::*;

#[derive(Debug)]
pub struct State {
mode: notify::RunMode,
mode: RunMode,
run_ignored: bool,
}

Expand All @@ -23,7 +23,7 @@ impl State {
}
}

pub fn current_mode(&self) -> notify::RunMode {
pub fn current_mode(&self) -> RunMode {
self.mode
}
}
Expand All @@ -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;
}

Expand Down
Loading

0 comments on commit 31764d2

Please sign in to comment.