Skip to content

Commit

Permalink
Fix clippy lints up to rust 1.41.1 in main regex crate
Browse files Browse the repository at this point in the history
Some lints have been intentionally ignored, especially:
* any lints that would change public APIs (like &self -> self)
* any lints that would introduce new public APIs (like Default over new)
  • Loading branch information
marmeladema committed May 15, 2021
1 parent eeb6be4 commit 31f64cd
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 86 deletions.
12 changes: 3 additions & 9 deletions src/backtrack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,7 @@ impl<'a, 'm, 'r, 's, I: Input> Bounded<'a, 'm, 'r, 's, I> {
let mut cache = cache.borrow_mut();
let cache = &mut cache.backtrack;
let start = input.at(start);
let mut b = Bounded {
prog: prog,
input: input,
matches: matches,
slots: slots,
m: cache,
};
let mut b = Bounded { prog, input, matches, slots, m: cache };
b.exec_(start, end)
}

Expand Down Expand Up @@ -220,14 +214,14 @@ impl<'a, 'm, 'r, 's, I: Input> Bounded<'a, 'm, 'r, 's, I> {
// job is popped and the old capture index is restored.
self.m.jobs.push(Job::SaveRestore {
slot: inst.slot,
old_pos: old_pos,
old_pos,
});
self.slots[inst.slot] = Some(at.pos());
}
ip = inst.goto;
}
Split(ref inst) => {
self.m.jobs.push(Job::Inst { ip: inst.goto2, at: at });
self.m.jobs.push(Job::Inst { ip: inst.goto2, at });
ip = inst.goto1;
}
EmptyLook(ref inst) => {
Expand Down
57 changes: 27 additions & 30 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ impl Compiler {
self.compiled.start = dotstar_patch.entry;
}
self.compiled.captures = vec![None];
let patch = self.c_capture(0, expr)?.unwrap_or(self.next_inst());
let patch =
self.c_capture(0, expr)?.unwrap_or_else(|| self.next_inst());
if self.compiled.needs_dotstar() {
self.fill(dotstar_patch.hole, patch.entry);
} else {
Expand Down Expand Up @@ -175,15 +176,15 @@ impl Compiler {
self.fill_to_next(prev_hole);
let split = self.push_split_hole();
let Patch { hole, entry } =
self.c_capture(0, expr)?.unwrap_or(self.next_inst());
self.c_capture(0, expr)?.unwrap_or_else(|| self.next_inst());
self.fill_to_next(hole);
self.compiled.matches.push(self.insts.len());
self.push_compiled(Inst::Match(i));
prev_hole = self.fill_split(split, Some(entry), None);
}
let i = exprs.len() - 1;
let Patch { hole, entry } =
self.c_capture(0, &exprs[i])?.unwrap_or(self.next_inst());
self.c_capture(0, &exprs[i])?.unwrap_or_else(|| self.next_inst());
self.fill(prev_hole, entry);
self.fill_to_next(hole);
self.compiled.matches.push(self.insts.len());
Expand Down Expand Up @@ -387,11 +388,11 @@ impl Compiler {
} else {
let entry = self.insts.len();
let hole = self.push_hole(InstHole::Save { slot: first_slot });
let patch = self.c(expr)?.unwrap_or(self.next_inst());
let patch = self.c(expr)?.unwrap_or_else(|| self.next_inst());
self.fill(hole, patch.entry);
self.fill_to_next(patch.hole);
let hole = self.push_hole(InstHole::Save { slot: first_slot + 1 });
Ok(Some(Patch { hole: hole, entry: entry }))
Ok(Some(Patch { hole, entry }))
}
}

Expand Down Expand Up @@ -425,7 +426,7 @@ impl Compiler {
self.c_class(&[hir::ClassUnicodeRange::new(c, c)])
}
} else {
let hole = self.push_hole(InstHole::Char { c: c });
let hole = self.push_hole(InstHole::Char { c });
Ok(Some(Patch { hole, entry: self.insts.len() - 1 }))
}
}
Expand All @@ -435,7 +436,7 @@ impl Compiler {

assert!(!ranges.is_empty());
if self.compiled.uses_bytes() {
Ok(Some(CompileClass { c: self, ranges: ranges }.compile()?))
Ok(Some(CompileClass { c: self, ranges }.compile()?))
} else {
let ranges: Vec<(char, char)> =
ranges.iter().map(|r| (r.start(), r.end())).collect();
Expand All @@ -444,9 +445,9 @@ impl Compiler {
} else {
self.extra_inst_bytes +=
ranges.len() * (size_of::<char>() * 2);
self.push_hole(InstHole::Ranges { ranges: ranges })
self.push_hole(InstHole::Ranges { ranges })
};
Ok(Some(Patch { hole: hole, entry: self.insts.len() - 1 }))
Ok(Some(Patch { hole, entry: self.insts.len() - 1 }))
}
}

Expand Down Expand Up @@ -485,8 +486,8 @@ impl Compiler {
}

fn c_empty_look(&mut self, look: EmptyLook) -> ResultOrEmpty {
let hole = self.push_hole(InstHole::EmptyLook { look: look });
Ok(Some(Patch { hole: hole, entry: self.insts.len() - 1 }))
let hole = self.push_hole(InstHole::EmptyLook { look });
Ok(Some(Patch { hole, entry: self.insts.len() - 1 }))
}

fn c_concat<'a, I>(&mut self, exprs: I) -> ResultOrEmpty
Expand All @@ -510,7 +511,7 @@ impl Compiler {
hole = p.hole;
}
}
Ok(Some(Patch { hole: hole, entry: entry }))
Ok(Some(Patch { hole, entry }))
}

fn c_alternate(&mut self, exprs: &[Hir]) -> ResultOrEmpty {
Expand Down Expand Up @@ -653,7 +654,7 @@ impl Compiler {
// None).
let patch_concat = self
.c_concat(iter::repeat(expr).take(min))?
.unwrap_or(self.next_inst());
.unwrap_or_else(|| self.next_inst());
if let Some(patch_rep) = self.c_repeat_zero_or_more(expr, greedy)? {
self.fill(patch_concat.hole, patch_rep.entry);
Ok(Some(Patch { hole: patch_rep.hole, entry: patch_concat.entry }))
Expand All @@ -677,7 +678,7 @@ impl Compiler {
}
// Same reasoning as in c_repeat_range_min_or_more (we know that min <
// max at this point).
let patch_concat = patch_concat.unwrap_or(self.next_inst());
let patch_concat = patch_concat.unwrap_or_else(|| self.next_inst());
let initial_entry = patch_concat.entry;
// It is much simpler to compile, e.g., `a{2,5}` as:
//
Expand Down Expand Up @@ -856,14 +857,14 @@ impl MaybeInst {
}
MaybeInst::Split1(goto1) => {
MaybeInst::Compiled(Inst::Split(InstSplit {
goto1: goto1,
goto1,
goto2: goto,
}))
}
MaybeInst::Split2(goto2) => {
MaybeInst::Compiled(Inst::Split(InstSplit {
goto1: goto,
goto2: goto2,
goto2,
}))
}
_ => unreachable!(
Expand All @@ -877,9 +878,7 @@ impl MaybeInst {

fn fill_split(&mut self, goto1: InstPtr, goto2: InstPtr) {
let filled = match *self {
MaybeInst::Split => {
Inst::Split(InstSplit { goto1: goto1, goto2: goto2 })
}
MaybeInst::Split => Inst::Split(InstSplit { goto1, goto2 }),
_ => unreachable!(
"must be called on Split instruction, \
instead it was called on: {:?}",
Expand Down Expand Up @@ -937,19 +936,17 @@ enum InstHole {
impl InstHole {
fn fill(&self, goto: InstPtr) -> Inst {
match *self {
InstHole::Save { slot } => {
Inst::Save(InstSave { goto: goto, slot: slot })
}
InstHole::Save { slot } => Inst::Save(InstSave { goto, slot }),
InstHole::EmptyLook { look } => {
Inst::EmptyLook(InstEmptyLook { goto: goto, look: look })
Inst::EmptyLook(InstEmptyLook { goto, look })
}
InstHole::Char { c } => Inst::Char(InstChar { goto: goto, c: c }),
InstHole::Char { c } => Inst::Char(InstChar { goto, c }),
InstHole::Ranges { ref ranges } => Inst::Ranges(InstRanges {
goto: goto,
goto,
ranges: ranges.clone().into_boxed_slice(),
}),
InstHole::Bytes { start, end } => {
Inst::Bytes(InstBytes { goto: goto, start: start, end: end })
Inst::Bytes(InstBytes { goto, start, end })
}
}
}
Expand Down Expand Up @@ -1019,7 +1016,7 @@ impl<'a, 'b> CompileClass<'a, 'b> {
let mut last_hole = Hole::None;
for byte_range in seq {
let key = SuffixCacheKey {
from_inst: from_inst,
from_inst,
start: byte_range.start,
end: byte_range.end,
};
Expand Down Expand Up @@ -1109,7 +1106,7 @@ impl SuffixCache {
}
}
*pos = self.dense.len();
self.dense.push(SuffixCacheEntry { key: key, pc: pc });
self.dense.push(SuffixCacheEntry { key, pc });
None
}

Expand All @@ -1120,8 +1117,8 @@ impl SuffixCache {
fn hash(&self, suffix: &SuffixCacheKey) -> usize {
// Basic FNV-1a hash as described:
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
const FNV_PRIME: u64 = 1099511628211;
let mut h = 14695981039346656037;
const FNV_PRIME: u64 = 1_099_511_628_211;
let mut h = 14_695_981_039_346_656_037;
h = (h ^ (suffix.from_inst as u64)).wrapping_mul(FNV_PRIME);
h = (h ^ (suffix.start as u64)).wrapping_mul(FNV_PRIME);
h = (h ^ (suffix.end as u64)).wrapping_mul(FNV_PRIME);
Expand Down
36 changes: 16 additions & 20 deletions src/dfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,10 @@ impl<'a> Fsm<'a> {
let mut cache = cache.borrow_mut();
let cache = &mut cache.dfa;
let mut dfa = Fsm {
prog: prog,
prog,
start: 0, // filled in below
at: at,
quit_after_match: quit_after_match,
at,
quit_after_match,
last_match_si: STATE_UNKNOWN,
last_cache_flush: at,
cache: &mut cache.inner,
Expand All @@ -484,10 +484,10 @@ impl<'a> Fsm<'a> {
let mut cache = cache.borrow_mut();
let cache = &mut cache.dfa_reverse;
let mut dfa = Fsm {
prog: prog,
prog,
start: 0, // filled in below
at: at,
quit_after_match: quit_after_match,
at,
quit_after_match,
last_match_si: STATE_UNKNOWN,
last_cache_flush: at,
cache: &mut cache.inner,
Expand Down Expand Up @@ -515,9 +515,9 @@ impl<'a> Fsm<'a> {
let mut cache = cache.borrow_mut();
let cache = &mut cache.dfa;
let mut dfa = Fsm {
prog: prog,
prog,
start: 0, // filled in below
at: at,
at,
quit_after_match: false,
last_match_si: STATE_UNKNOWN,
last_cache_flush: at,
Expand Down Expand Up @@ -1608,11 +1608,7 @@ struct StateMap {

impl StateMap {
fn new(num_byte_classes: usize) -> StateMap {
StateMap {
map: HashMap::new(),
states: vec![],
num_byte_classes: num_byte_classes,
}
StateMap { map: HashMap::new(), states: vec![], num_byte_classes }
}

fn len(&self) -> usize {
Expand Down Expand Up @@ -1648,7 +1644,7 @@ impl Transitions {
/// The number of byte classes corresponds to the stride. Every state will
/// have `num_byte_classes` slots for transitions.
fn new(num_byte_classes: usize) -> Transitions {
Transitions { table: vec![], num_byte_classes: num_byte_classes }
Transitions { table: vec![], num_byte_classes }
}

/// Returns the total number of states currently in this table.
Expand Down Expand Up @@ -1698,27 +1694,27 @@ impl Transitions {

impl StateFlags {
fn is_match(&self) -> bool {
self.0 & 0b0000000_1 > 0
self.0 & 0b0000_0001 > 0
}

fn set_match(&mut self) {
self.0 |= 0b0000000_1;
self.0 |= 0b0000_0001;
}

fn is_word(&self) -> bool {
self.0 & 0b000000_1_0 > 0
self.0 & 0b0000_0010 > 0
}

fn set_word(&mut self) {
self.0 |= 0b000000_1_0;
self.0 |= 0b0000_0010;
}

fn has_empty(&self) -> bool {
self.0 & 0b00000_1_00 > 0
self.0 & 0b0000_0100 > 0
}

fn set_empty(&mut self) {
self.0 |= 0b00000_1_00;
self.0 |= 0b0000_0100;
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,10 @@ impl ExecBuilder {
exprs.push(expr);
}
Ok(Parsed {
exprs: exprs,
exprs,
prefixes: prefixes.unwrap_or_else(Literals::empty),
suffixes: suffixes.unwrap_or_else(Literals::empty),
bytes: bytes,
bytes,
})
}

Expand All @@ -311,7 +311,7 @@ impl ExecBuilder {
match_type: MatchType::Nothing,
});
let pool = ExecReadOnly::new_pool(&ro);
return Ok(Exec { ro: ro, pool });
return Ok(Exec { ro, pool });
}
let parsed = self.parse()?;
let mut nfa = Compiler::new()
Expand Down Expand Up @@ -340,12 +340,12 @@ impl ExecBuilder {

let mut ro = ExecReadOnly {
res: self.options.pats,
nfa: nfa,
dfa: dfa,
dfa_reverse: dfa_reverse,
nfa,
dfa,
dfa_reverse,
suffixes: LiteralSearcher::suffixes(parsed.suffixes),
#[cfg(feature = "perf-literal")]
ac: ac,
ac,
match_type: MatchType::Nothing,
};
ro.match_type = ro.choose_match_type(self.match_type);
Expand Down
8 changes: 4 additions & 4 deletions src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl From<usize> for Ref<'static> {
/// If no such valid reference could be found, None is returned.
fn find_cap_ref(replacement: &[u8]) -> Option<CaptureRef<'_>> {
let mut i = 0;
let rep: &[u8] = replacement.as_ref();
let rep: &[u8] = replacement;
if rep.len() <= 1 || rep[0] != b'$' {
return None;
}
Expand All @@ -136,7 +136,7 @@ fn find_cap_ref(replacement: &[u8]) -> Option<CaptureRef<'_>> {
return find_cap_ref_braced(rep, i + 1);
}
let mut cap_end = i;
while rep.get(cap_end).map_or(false, is_valid_cap_letter) {
while rep.get(cap_end).copied().map_or(false, is_valid_cap_letter) {
cap_end += 1;
}
if cap_end == i {
Expand Down Expand Up @@ -183,8 +183,8 @@ 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.
fn is_valid_cap_letter(b: &u8) -> bool {
match *b {
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,
}
Expand Down
Loading

0 comments on commit 31f64cd

Please sign in to comment.