Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clippy fixes #1129

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion regex-automata/src/nfa/thompson/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ impl Compiler {
capture_index: u32,
name: Option<&str>,
) -> Result<StateID, BuildError> {
let name = name.map(|n| Arc::from(n));
let name = name.map(Arc::from);
self.builder.borrow_mut().add_capture_start(
StateID::ZERO,
capture_index,
Expand Down
8 changes: 4 additions & 4 deletions regex-automata/src/nfa/thompson/nfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,13 +1471,13 @@ impl fmt::Debug for Inner {
}
let pattern_len = self.start_pattern.len();
if pattern_len > 1 {
writeln!(f, "")?;
writeln!(f)?;
for pid in 0..pattern_len {
let sid = self.start_pattern[pid];
writeln!(f, "START({:06?}): {:?}", pid, sid.as_usize())?;
}
}
writeln!(f, "")?;
writeln!(f)?;
writeln!(
f,
"transition equivalence classes: {:?}",
Expand Down Expand Up @@ -1819,7 +1819,7 @@ impl SparseTransitions {
&self,
unit: alphabet::Unit,
) -> Option<StateID> {
unit.as_u8().map_or(None, |byte| self.matches_byte(byte))
unit.as_u8().and_then(|byte| self.matches_byte(byte))
}

/// This follows the matching transition for a particular byte.
Expand Down Expand Up @@ -1909,7 +1909,7 @@ impl DenseTransitions {
&self,
unit: alphabet::Unit,
) -> Option<StateID> {
unit.as_u8().map_or(None, |byte| self.matches_byte(byte))
unit.as_u8().and_then(|byte| self.matches_byte(byte))
}

/// This follows the matching transition for a particular byte.
Expand Down
4 changes: 2 additions & 2 deletions regex-automata/src/nfa/thompson/pikevm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ impl PikeVM {
// the only thing in 'curr'. So we might as well just skip
// ahead until we find something that we know might advance us
// forward.
if let Some(ref pre) = pre {
if let Some(pre) = pre {
let span = Span::from(at..input.end());
match pre.find(input.haystack(), span) {
None => break,
Expand Down Expand Up @@ -1344,7 +1344,7 @@ impl PikeVM {
// search. If we re-computed it at every position, we would be
// simulating an unanchored search when we were tasked to perform
// an anchored search.
if (!hm.is_some() || allmatches)
if (hm.is_none() || allmatches)
&& (!anchored || at == input.start())
{
// Since we are adding to the 'curr' active states and since
Expand Down
6 changes: 3 additions & 3 deletions regex-automata/src/nfa/thompson/range_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl RangeTrie {
/// Clear this range trie such that it is empty. Clearing a range trie
/// and reusing it can beneficial because this may reuse allocations.
pub fn clear(&mut self) {
self.free.extend(self.states.drain(..));
self.free.append(&mut self.states);
self.add_empty(); // final
self.add_empty(); // root
}
Expand Down Expand Up @@ -296,7 +296,7 @@ impl RangeTrie {
assert!(!ranges.is_empty());
assert!(ranges.len() <= 4);

let mut stack = mem::replace(&mut self.insert_stack, vec![]);
let mut stack = std::mem::take(&mut self.insert_stack);
chris-ha458 marked this conversation as resolved.
Show resolved Hide resolved
stack.clear();

stack.push(NextInsert::new(ROOT, ranges));
Expand Down Expand Up @@ -866,7 +866,7 @@ impl Split {

impl fmt::Debug for RangeTrie {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "")?;
writeln!(f)?;
for (i, state) in self.states.iter().enumerate() {
let status = if i == FINAL.as_usize() { '*' } else { ' ' };
writeln!(f, "{}{:06}: {:?}", status, i, state)?;
Expand Down
2 changes: 1 addition & 1 deletion regex-automata/src/util/determinize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub(crate) fn next(
if !state.look_need().is_empty() {
// Add look-ahead assertions that are now true based on the current
// input unit.
let mut look_have = state.look_have().clone();
let mut look_have = state.look_have();
match unit.as_u8() {
Some(b'\r') => {
if !rev || !state.is_half_crlf() {
Expand Down
14 changes: 6 additions & 8 deletions regex-automata/src/util/determinize/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub(crate) struct State(Arc<[u8]>);
/// without having to convert it into a State first.
impl core::borrow::Borrow<[u8]> for State {
fn borrow(&self) -> &[u8] {
&*self.0
&self.0
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ impl State {
}

fn repr(&self) -> Repr<'_> {
Repr(&*self.0)
Repr(&self.0)
}
}

Expand Down Expand Up @@ -460,12 +460,10 @@ impl<'a> Repr<'a> {
///
/// If this state is not a match state, then this always returns 0.
fn match_len(&self) -> usize {
if !self.is_match() {
return 0;
} else if !self.has_pattern_ids() {
1
} else {
self.encoded_pattern_len()
match (self.is_match(), self.has_pattern_ids()) {
(false, _) => 0,
(true, false) => 1,
(true, true) => self.encoded_pattern_len(),
chris-ha458 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
2 changes: 1 addition & 1 deletion regex-automata/src/util/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl core::fmt::Debug for DebugByte {
let mut len = 0;
for (i, mut b) in core::ascii::escape_default(self.0).enumerate() {
// capitalize \xab to \xAB
if i >= 2 && b'a' <= b && b <= b'f' {
if i >= 2 && (b'a'..=b'f').contains(&b) {
chris-ha458 marked this conversation as resolved.
Show resolved Hide resolved
b -= 32;
}
bytes[len] = b;
Expand Down
9 changes: 3 additions & 6 deletions regex-automata/src/util/interpolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ pub fn string(
}
// Handle escaping of '$'.
if replacement.as_bytes().get(1).map_or(false, |&b| b == b'$') {
dst.push_str("$");
dst.push('$');
replacement = &replacement[2..];
continue;
}
debug_assert!(!replacement.is_empty());
let cap_ref = match find_cap_ref(replacement.as_bytes()) {
Some(cap_ref) => cap_ref,
None => {
dst.push_str("$");
dst.push('$');
replacement = &replacement[1..];
continue;
}
Expand Down Expand Up @@ -321,10 +321,7 @@ fn find_cap_ref_braced(rep: &[u8], mut i: usize) -> Option<CaptureRef<'_>> {
/// Returns true if and only if the given byte is allowed in a capture name
/// written in non-brace form.
fn is_valid_cap_letter(b: u8) -> bool {
match b {
b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' | b'_' => true,
_ => false,
}
matches!(b, b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' | b'_')
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions regex-automata/src/util/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ mod inner {
#[inline]
pub(super) fn value(&self) -> &T {
match self.value {
Ok(ref v) => &**v,
Ok(ref v) => v,
// SAFETY: This is safe because the only way a PoolGuard gets
// created for self.value=Err is when the current thread
// corresponds to the owning thread, of which there can only
Expand All @@ -703,7 +703,7 @@ mod inner {
#[inline]
pub(super) fn value_mut(&mut self) -> &mut T {
match self.value {
Ok(ref mut v) => &mut **v,
Ok(ref mut v) => v,
// SAFETY: This is safe because the only way a PoolGuard gets
// created for self.value=None is when the current thread
// corresponds to the owning thread, of which there can only
Expand Down
6 changes: 3 additions & 3 deletions regex-automata/src/util/prefilter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,17 +478,17 @@ pub(crate) trait PrefilterI:
impl<P: PrefilterI + ?Sized> PrefilterI for Arc<P> {
#[cfg_attr(feature = "perf-inline", inline(always))]
fn find(&self, haystack: &[u8], span: Span) -> Option<Span> {
(&**self).find(haystack, span)
(**self).find(haystack, span)
}

#[cfg_attr(feature = "perf-inline", inline(always))]
fn prefix(&self, haystack: &[u8], span: Span) -> Option<Span> {
(&**self).prefix(haystack, span)
(**self).prefix(haystack, span)
}

#[cfg_attr(feature = "perf-inline", inline(always))]
fn memory_usage(&self) -> usize {
(&**self).memory_usage()
(**self).memory_usage()
}

#[cfg_attr(feature = "perf-inline", inline(always))]
Expand Down
26 changes: 11 additions & 15 deletions regex-automata/src/util/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1365,16 +1365,17 @@ impl<'a> Iterator for PatternSetIter<'a> {
type Item = PatternID;

fn next(&mut self) -> Option<PatternID> {
while let Some((index, &yes)) = self.it.next() {
// Only valid 'PatternID' values can be inserted into the set
// and construction of the set panics if the capacity would
// permit storing invalid pattern IDs. Thus, 'yes' is only true
// precisely when 'index' corresponds to a valid 'PatternID'.
self.it.by_ref().find_map(|(index, &yes)| {
if yes {
// Only valid 'PatternID' values can be inserted into the set
// and construction of the set panics if the capacity would
// permit storing invalid pattern IDs. Thus, 'yes' is only true
// precisely when 'index' corresponds to a valid 'PatternID'.
return Some(PatternID::new_unchecked(index));
Some(PatternID::new_unchecked(index))
} else {
None
}
}
None
})
}

fn size_hint(&self) -> (usize, Option<usize>) {
Expand Down Expand Up @@ -1689,13 +1690,14 @@ impl Anchored {
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Clone, Copy, Default, Debug, Eq, PartialEq)]
pub enum MatchKind {
/// Report all possible matches.
All,
/// Report only the leftmost matches. When multiple leftmost matches exist,
/// report the match corresponding to the part of the regex that appears
/// first in the syntax.
#[default]
LeftmostFirst,
// There is prior art in RE2 that shows that we should be able to add
// LeftmostLongest too. The tricky part of it is supporting ungreedy
Expand All @@ -1721,12 +1723,6 @@ impl MatchKind {
}
}

impl Default for MatchKind {
fn default() -> MatchKind {
MatchKind::LeftmostFirst
}
}

/// An error indicating that a search stopped before reporting whether a
/// match exists or not.
///
Expand Down
2 changes: 1 addition & 1 deletion regex-automata/src/util/sparse_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,6 @@ impl<'a> Iterator for SparseSetIter<'a> {

#[cfg_attr(feature = "perf-inline", inline(always))]
fn next(&mut self) -> Option<StateID> {
self.0.next().map(|&id| id)
self.0.next().copied()
}
}
19 changes: 7 additions & 12 deletions regex-automata/src/util/utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,13 @@ pub(crate) fn decode_last(bytes: &[u8]) -> Option<Result<char, u8>> {
/// `None`.
#[cfg_attr(feature = "perf-inline", inline(always))]
fn len(byte: u8) -> Option<usize> {
if byte <= 0x7F {
return Some(1);
} else if byte & 0b1100_0000 == 0b1000_0000 {
return None;
} else if byte <= 0b1101_1111 {
Some(2)
} else if byte <= 0b1110_1111 {
Some(3)
} else if byte <= 0b1111_0111 {
Some(4)
} else {
None
match byte {
0b0000_0000..=0b0111_1111 => Some(1),
0b1000_0000..=0b1011_1111 => None,
0b1100_0000..=0b1101_1111 => Some(2),
0b1110_0000..=0b1110_1111 => Some(3),
0b1111_0000..=0b1111_0111 => Some(4),
_ => None,
}
}

Expand Down
8 changes: 2 additions & 6 deletions regex-automata/src/util/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,8 @@ pub(crate) fn write_label(
/// is longer than 255 bytes. (The size restriction exists so that searching
/// for a label during deserialization can be done in small bounded space.)
pub(crate) fn write_label_len(label: &str) -> usize {
if label.len() > 255 {
panic!("label must not be longer than 255 bytes");
}
if label.as_bytes().iter().position(|&b| b == 0).is_some() {
panic!("label must not contain NUL bytes");
}
assert!(label.len() <= 255, "label must not be longer than 255 bytes");
assert!(label.bytes().all(|b| b != 0), "label must not contain NUL bytes");
let label_len = label.len() + 1; // +1 for the NUL terminator
label_len + padding_len(label_len)
}
Expand Down
Loading