Skip to content

Commit

Permalink
Inline format-args
Browse files Browse the repository at this point in the history
This makes the code a bit easier to read and smaller.

Some of it was done with this command, and later fixed by hand:

```
 cargo clippy --workspace --allow-dirty --fix --benches --tests --bins -- -A clippy::all -W clippy::uninlined_format_args
 ```
  • Loading branch information
nyurik committed Aug 28, 2024
1 parent ab88aa5 commit d89b024
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 55 deletions.
2 changes: 1 addition & 1 deletion regex-capi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description = """
A C API for Rust's regular expression library.
"""
workspace = ".."
edition = "2018"
edition = "2021"

[lib]
name = "rure"
Expand Down
2 changes: 1 addition & 1 deletion regex-capi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ffi_fn! {
ffi_fn! {
fn rure_error_message(err: *mut Error) -> *const c_char {
let err = unsafe { &mut *err };
let cmsg = match CString::new(format!("{}", err)) {
let cmsg = match CString::new(format!("{err}")) {
Ok(msg) => msg,
Err(err) => {
// I guess this can probably happen if the regex itself has a
Expand Down
5 changes: 1 addition & 4 deletions regex-capi/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ macro_rules! ffi_fn {
} else {
"UNABLE TO SHOW RESULT OF PANIC.".to_owned()
};
let _ = writeln!(
&mut io::stderr(),
"panic unwind caught, aborting: {:?}",
msg);
let _ = writeln!(&mut io::stderr(), "panic unwind caught, aborting: {msg:?}");
unsafe { abort() }
}
}
Expand Down
4 changes: 2 additions & 2 deletions regex-capi/src/rure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ ffi_fn! {
let re = rure_compile(
pat, len, RURE_DEFAULT_FLAGS, ptr::null(), &mut err);
if err.is_err() {
let _ = writeln!(&mut io::stderr(), "{}", err);
let _ = writeln!(&mut io::stderr(), "{err}");
let _ = writeln!(
&mut io::stderr(), "aborting from rure_compile_must");
unsafe { abort() }
Expand Down Expand Up @@ -579,7 +579,7 @@ ffi_fn! {
let mut err = Error::new(ErrorKind::None);
let esc = rure_escape(pat, len, &mut err);
if err.is_err() {
let _ = writeln!(&mut io::stderr(), "{}", err);
let _ = writeln!(&mut io::stderr(), "{err}");
let _ = writeln!(
&mut io::stderr(), "aborting from rure_escape_must");
unsafe { abort() }
Expand Down
7 changes: 3 additions & 4 deletions regex-lite/src/hir/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,7 @@ impl<'a> Parser<'a> {
'u' => 4,
'U' => 8,
unk => unreachable!(
"invalid start of fixed length hexadecimal number {}",
unk
"invalid start of fixed length hexadecimal number {unk}"
),
};
if !self.bump_and_bump_space() {
Expand Down Expand Up @@ -720,7 +719,7 @@ impl<'a> Parser<'a> {
'?' => (0, Some(1)),
'*' => (0, None),
'+' => (1, None),
unk => unreachable!("unrecognized repetition operator '{}'", unk),
unk => unreachable!("unrecognized repetition operator '{unk}'"),
};
let mut greedy = true;
if self.bump() && self.char() == '?' {
Expand Down Expand Up @@ -1216,7 +1215,7 @@ impl<'a> Parser<'a> {
'd' | 'D' => posix_class("digit").unwrap(),
's' | 'S' => posix_class("space").unwrap(),
'w' | 'W' => posix_class("word").unwrap(),
unk => unreachable!("invalid Perl class \\{}", unk),
unk => unreachable!("invalid Perl class \\{unk}"),
});
if ch.is_ascii_uppercase() {
class.negate();
Expand Down
14 changes: 7 additions & 7 deletions regex-lite/src/nfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl core::fmt::Debug for NFA {
writeln!(f, "NFA(")?;
writeln!(f, "pattern: {}", self.pattern)?;
for (sid, state) in self.states.iter().enumerate() {
writeln!(f, "{:07?}: {:?}", sid, state)?;
writeln!(f, "{sid:07?}: {state:?}")?;
}
writeln!(f, ")")?;
Ok(())
Expand Down Expand Up @@ -206,14 +206,14 @@ impl core::fmt::Debug for State {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match *self {
State::Char { target, ch } => {
write!(f, "{:?} => {:?}", ch, target)
write!(f, "{ch:?} => {target:?}")
}
State::Ranges { target, ref ranges } => {
for (i, &(start, end)) in ranges.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{:?}-{:?} => {:?}", start, end, target)?;
write!(f, "{start:?}-{end:?} => {target:?}")?;
}
Ok(())
}
Expand All @@ -225,18 +225,18 @@ impl core::fmt::Debug for State {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{:?}", sid)?;
write!(f, "{sid:?}")?;
}
write!(f, ")")
}
State::Goto { target, look: None } => {
write!(f, "goto({:?})", target)
write!(f, "goto({target:?})")
}
State::Goto { target, look: Some(look) } => {
write!(f, "{:?} => {:?}", look, target)
write!(f, "{look:?} => {target:?}")
}
State::Capture { target, slot } => {
write!(f, "capture(slot={:?}) => {:?}", slot, target,)
write!(f, "capture(slot={slot:?}) => {target:?}")
}
State::Fail => write!(f, "FAIL"),
State::Match => {
Expand Down
8 changes: 4 additions & 4 deletions regex-lite/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ impl<'h> Captures<'h> {
.nfa()
.static_explicit_captures_len()
.expect("number of capture groups can vary in a match");
assert_eq!(N, len, "asked for {} groups, but must ask for {}", N, len);
assert_eq!(N, len, "asked for {N} groups, but must ask for {len}");
let mut matched = self.iter().flatten();
let whole_match = matched.next().expect("a match").as_str();
let group_matches = [0; N].map(|_| {
Expand Down Expand Up @@ -1965,7 +1965,7 @@ impl<'h> core::fmt::Debug for Captures<'h> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", self.0)?;
if let Some(name) = self.1 {
write!(f, "/{:?}", name)?;
write!(f, "/{name:?}")?;
}
Ok(())
}
Expand Down Expand Up @@ -2013,7 +2013,7 @@ impl<'h> core::ops::Index<usize> for Captures<'h> {
fn index(&self, i: usize) -> &str {
self.get(i)
.map(|m| m.as_str())
.unwrap_or_else(|| panic!("no group at index '{}'", i))
.unwrap_or_else(|| panic!("no group at index '{i}'"))
}
}

Expand All @@ -2039,7 +2039,7 @@ impl<'h, 'n> core::ops::Index<&'n str> for Captures<'h> {
fn index<'a>(&'a self, name: &'n str) -> &'a str {
self.name(name)
.map(|m| m.as_str())
.unwrap_or_else(|| panic!("no group named '{}'", name))
.unwrap_or_else(|| panic!("no group named '{name}'"))
}
}

Expand Down
5 changes: 2 additions & 3 deletions regex-lite/tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ fn run_test(re: &Regex, test: &RegexTest) -> TestResult {
Ok(hay) => hay,
Err(err) => {
return TestResult::fail(&format!(
"haystack is not valid UTF-8: {}",
err
"haystack is not valid UTF-8: {err}",
));
}
};
Expand All @@ -45,7 +44,7 @@ fn run_test(re: &Regex, test: &RegexTest) -> TestResult {
.map(|caps| testify_captures(&caps));
TestResult::captures(it)
}
name => TestResult::fail(&format!("unrecognized test name: {}", name)),
name => TestResult::fail(&format!("unrecognized test name: {name}")),
}
}

Expand Down
14 changes: 7 additions & 7 deletions regex-test/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,17 @@ impl RegexTests {
/// The given group name is assigned to all loaded tests.
pub fn load_slice(&mut self, group_name: &str, data: &[u8]) -> Result<()> {
let data = std::str::from_utf8(&data).with_context(|| {
format!("data in {} is not valid UTF-8", group_name)
format!("data in {group_name} is not valid UTF-8")
})?;
let mut index = 1;
let mut tests: RegexTests =
toml::from_str(&data).with_context(|| {
format!("error decoding TOML for '{}'", group_name)
format!("error decoding TOML for '{group_name}'")
})?;
for t in &mut tests.tests {
t.group = group_name.to_string();
if t.name.is_empty() {
t.name = format!("{}", index);
t.name = format!("{index}");
index += 1;
}
t.full_name = format!("{}/{}", t.group, t.name);
Expand Down Expand Up @@ -1101,7 +1101,7 @@ impl RegexTestFailureKind {
let mut buf = String::new();
match *self {
RegexTestFailureKind::UserFailure { ref why } => {
write!(buf, "failed by implementor because: {}", why)?;
write!(buf, "failed by implementor because: {why}")?;
}
RegexTestFailureKind::IsMatch => {
if test.is_match() {
Expand Down Expand Up @@ -1140,13 +1140,13 @@ impl RegexTestFailureKind {
write!(buf, "expected regex to NOT compile, but it did")?;
}
RegexTestFailureKind::CompileError { ref err } => {
write!(buf, "expected regex to compile, failed: {}", err)?;
write!(buf, "expected regex to compile, failed: {err}")?;
}
RegexTestFailureKind::UnexpectedPanicCompile(ref msg) => {
write!(buf, "got unexpected panic while compiling:\n{}", msg)?;
write!(buf, "got unexpected panic while compiling:\n{msg}")?;
}
RegexTestFailureKind::UnexpectedPanicSearch(ref msg) => {
write!(buf, "got unexpected panic while searching:\n{}", msg)?;
write!(buf, "got unexpected panic while searching:\n{msg}")?;
}
}
Ok(buf)
Expand Down
9 changes: 4 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ impl core::fmt::Display for Error {
Error::Syntax(ref err) => err.fmt(f),
Error::CompiledTooBig(limit) => write!(
f,
"Compiled regex exceeds size limit of {} bytes.",
limit
"Compiled regex exceeds size limit of {limit} bytes.",
),
}
}
Expand All @@ -88,9 +87,9 @@ impl core::fmt::Debug for Error {
Error::Syntax(ref err) => {
let hr: String = core::iter::repeat('~').take(79).collect();
writeln!(f, "Syntax(")?;
writeln!(f, "{}", hr)?;
writeln!(f, "{}", err)?;
writeln!(f, "{}", hr)?;
writeln!(f, "{hr}")?;
writeln!(f, "{err}")?;
writeln!(f, "{hr}")?;
write!(f, ")")?;
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions src/regex/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ impl<'h> Captures<'h> {
.expect("number of capture groups can vary in a match")
.checked_sub(1)
.expect("number of groups is always greater than zero");
assert_eq!(N, len, "asked for {} groups, but must ask for {}", N, len);
assert_eq!(N, len, "asked for {N} groups, but must ask for {len}");
// The regex-automata variant of extract is a bit more permissive.
// It doesn't require the number of matching capturing groups to be
// static, and you can even request fewer groups than what's there. So
Expand Down Expand Up @@ -1942,7 +1942,7 @@ impl<'h> core::fmt::Debug for Captures<'h> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", self.0)?;
if let Some(name) = self.1 {
write!(f, "/{:?}", name)?;
write!(f, "/{name:?}")?;
}
Ok(())
}
Expand Down Expand Up @@ -1992,7 +1992,7 @@ impl<'h> core::ops::Index<usize> for Captures<'h> {
fn index<'a>(&'a self, i: usize) -> &'a [u8] {
self.get(i)
.map(|m| m.as_bytes())
.unwrap_or_else(|| panic!("no group at index '{}'", i))
.unwrap_or_else(|| panic!("no group at index '{i}'"))
}
}

Expand All @@ -2018,7 +2018,7 @@ impl<'h, 'n> core::ops::Index<&'n str> for Captures<'h> {
fn index<'a>(&'a self, name: &'n str) -> &'a [u8] {
self.name(name)
.map(|m| m.as_bytes())
.unwrap_or_else(|| panic!("no group named '{}'", name))
.unwrap_or_else(|| panic!("no group named '{name}'"))
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/regex/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1797,7 +1797,7 @@ impl<'h> Captures<'h> {
.expect("number of capture groups can vary in a match")
.checked_sub(1)
.expect("number of groups is always greater than zero");
assert_eq!(N, len, "asked for {} groups, but must ask for {}", N, len);
assert_eq!(N, len, "asked for {N} groups, but must ask for {len}");
// The regex-automata variant of extract is a bit more permissive.
// It doesn't require the number of matching capturing groups to be
// static, and you can even request fewer groups than what's there. So
Expand Down Expand Up @@ -1952,7 +1952,7 @@ impl<'h> core::fmt::Debug for Captures<'h> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", self.0)?;
if let Some(name) = self.1 {
write!(f, "/{:?}", name)?;
write!(f, "/{name:?}")?;
}
Ok(())
}
Expand Down Expand Up @@ -2000,7 +2000,7 @@ impl<'h> core::ops::Index<usize> for Captures<'h> {
fn index<'a>(&'a self, i: usize) -> &'a str {
self.get(i)
.map(|m| m.as_str())
.unwrap_or_else(|| panic!("no group at index '{}'", i))
.unwrap_or_else(|| panic!("no group at index '{i}'"))
}
}

Expand All @@ -2026,7 +2026,7 @@ impl<'h, 'n> core::ops::Index<&'n str> for Captures<'h> {
fn index<'a>(&'a self, name: &'n str) -> &'a str {
self.name(name)
.map(|m| m.as_str())
.unwrap_or_else(|| panic!("no group named '{}'", name))
.unwrap_or_else(|| panic!("no group named '{name}'"))
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ macro_rules! regex {
fn unclosed_group_error() {
let err = Regex::new(r"(").unwrap_err();
let msg = err.to_string();
assert!(msg.contains("unclosed group"), "error message: {:?}", msg);
assert!(msg.contains("unclosed group"), "error message: {msg:?}");
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion tests/suite_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn run_test(re: &Regex, test: &RegexTest) -> TestResult {
.map(|caps| testify_captures(&caps));
TestResult::captures(it)
}
name => TestResult::fail(&format!("unrecognized test name: {}", name)),
name => TestResult::fail(&format!("unrecognized test name: {name}")),
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/suite_bytes_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn run_test(re: &RegexSet, test: &RegexTest) -> TestResult {
match test.additional_name() {
"is_match" => TestResult::matched(re.is_match(test.haystack())),
"which" => TestResult::which(re.matches(test.haystack()).iter()),
name => TestResult::fail(&format!("unrecognized test name: {}", name)),
name => TestResult::fail(&format!("unrecognized test name: {name}")),
}
}

Expand Down
5 changes: 2 additions & 3 deletions tests/suite_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ fn run_test(re: &Regex, test: &RegexTest) -> TestResult {
Ok(hay) => hay,
Err(err) => {
return TestResult::fail(&format!(
"haystack is not valid UTF-8: {}",
err
"haystack is not valid UTF-8: {err}",
));
}
};
Expand All @@ -45,7 +44,7 @@ fn run_test(re: &Regex, test: &RegexTest) -> TestResult {
.map(|caps| testify_captures(&caps));
TestResult::captures(it)
}
name => TestResult::fail(&format!("unrecognized test name: {}", name)),
name => TestResult::fail(&format!("unrecognized test name: {name}")),
}
}

Expand Down
5 changes: 2 additions & 3 deletions tests/suite_string_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ fn run_test(re: &RegexSet, test: &RegexTest) -> TestResult {
Ok(hay) => hay,
Err(err) => {
return TestResult::fail(&format!(
"haystack is not valid UTF-8: {}",
err
"haystack is not valid UTF-8: {err}",
));
}
};
match test.additional_name() {
"is_match" => TestResult::matched(re.is_match(hay)),
"which" => TestResult::which(re.matches(hay).iter()),
name => TestResult::fail(&format!("unrecognized test name: {}", name)),
name => TestResult::fail(&format!("unrecognized test name: {name}")),
}
}

Expand Down

0 comments on commit d89b024

Please sign in to comment.