Skip to content

Commit 94f8213

Browse files
committed
Fix clippy warnings
1 parent 610c2ec commit 94f8213

30 files changed

+221
-140
lines changed

examples/shootout-regex-dna-bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn main() {
5555
(regex!("Y"), &b"(c|t)"[..]),
5656
];
5757
let mut seq = seq;
58-
for (re, replacement) in substs.into_iter() {
58+
for (re, replacement) in substs {
5959
seq = re.replace_all(&seq, replacement).into_owned();
6060
}
6161

examples/shootout-regex-dna-replace.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ macro_rules! regex {
99
}}
1010
}
1111

12+
#[cfg_attr(not(feature = "cargo-clippy"), allow(unknown_lints))]
13+
#[allow(regex_macro)]
1214
fn main() {
1315
let mut seq = String::with_capacity(50 * (1 << 20));
1416
io::stdin().read_to_string(&mut seq).unwrap();

examples/shootout-regex-dna-single.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use std::io::{self, Read};
1111

1212
macro_rules! regex { ($re:expr) => { ::regex::Regex::new($re).unwrap() } }
1313

14+
#[cfg_attr(not(feature = "cargo-clippy"), allow(unknown_lints))]
15+
#[allow(trivial_regex)]
1416
fn main() {
1517
let mut seq = String::with_capacity(50 * (1 << 20));
1618
io::stdin().read_to_string(&mut seq).unwrap();
@@ -48,7 +50,7 @@ fn main() {
4850
(regex!("Y"), "(c|t)"),
4951
];
5052
let mut seq = seq;
51-
for (re, replacement) in substs.into_iter() {
53+
for (re, replacement) in substs {
5254
seq = re.replace_all(&seq, replacement).into_owned();
5355
}
5456
println!("\n{}\n{}\n{}", ilen, clen, seq.len());

examples/shootout-regex-dna.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use std::thread;
1313

1414
macro_rules! regex { ($re:expr) => { ::regex::Regex::new($re).unwrap() } }
1515

16+
#[cfg_attr(not(feature = "cargo-clippy"), allow(unknown_lints))]
17+
#[allow(trivial_regex)]
1618
fn main() {
1719
let mut seq = String::with_capacity(51 * (1 << 20));
1820
io::stdin().read_to_string(&mut seq).unwrap();
@@ -55,7 +57,7 @@ fn main() {
5557
(regex!("Y"), "(c|t)"),
5658
];
5759
let mut seq = seq;
58-
for (re, replacement) in substs.into_iter() {
60+
for (re, replacement) in substs {
5961
seq = re.replace_all(&seq, replacement).into_owned();
6062
}
6163

src/compile.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ impl Compiler {
569569
greedy: bool,
570570
min: u32,
571571
) -> Result {
572-
let min = u32_to_usize(min);
572+
let min = min as usize;
573573
let patch_concat = try!(self.c_concat(iter::repeat(expr).take(min)));
574574
let patch_rep = try!(self.c_repeat_zero_or_more(expr, greedy));
575575
self.fill(patch_concat.hole, patch_rep.entry);
@@ -583,7 +583,7 @@ impl Compiler {
583583
min: u32,
584584
max: u32,
585585
) -> Result {
586-
let (min, max) = (u32_to_usize(min), u32_to_usize(max));
586+
let (min, max) = (min as usize, max as usize);
587587
let patch_concat = try!(self.c_concat(iter::repeat(expr).take(min)));
588588
let initial_entry = patch_concat.entry;
589589
if min == max {
@@ -713,6 +713,12 @@ impl Compiler {
713713
}
714714
}
715715

716+
impl Default for Compiler {
717+
fn default() -> Self {
718+
Self::new()
719+
}
720+
}
721+
716722
#[derive(Debug)]
717723
enum Hole {
718724
None,
@@ -833,7 +839,7 @@ impl<'a, 'b> CompileClass<'a, 'b> {
833839
let mut utf8_seqs = self.c.utf8_seqs.take().unwrap();
834840
self.c.suffix_cache.clear();
835841

836-
for (i, ref range) in self.ranges.iter().enumerate() {
842+
for (i, range) in self.ranges.iter().enumerate() {
837843
let is_last_range = i + 1 == self.ranges.len();
838844
utf8_seqs.reset(range.start, range.end);
839845
let mut it = (&mut utf8_seqs).peekable();
@@ -916,7 +922,7 @@ impl<'a, 'b> CompileClass<'a, 'b> {
916922
}
917923
}
918924

919-
/// SuffixCache is a simple bounded hash map for caching suffix entries in
925+
/// `SuffixCache` is a simple bounded hash map for caching suffix entries in
920926
/// UTF-8 automata. For example, consider the Unicode range \u{0}-\u{FFFF}.
921927
/// The set of byte ranges looks like this:
922928
///
@@ -1049,13 +1055,6 @@ impl ByteClassSet {
10491055
}
10501056
}
10511057

1052-
fn u32_to_usize(n: u32) -> usize {
1053-
if (n as u64) > (::std::usize::MAX as u64) {
1054-
panic!("BUG: {} is too big to be pointer sized", n)
1055-
}
1056-
n as usize
1057-
}
1058-
10591058
#[cfg(test)]
10601059
mod tests {
10611060
use super::ByteClassSet;

src/dfa.rs

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use sparse::SparseSet;
5858
/// Return true if and only if the given program can be executed by a DFA.
5959
///
6060
/// Generally, a DFA is always possible. A pathological case where it is not
61-
/// possible is if the number of NFA states exceeds u32::MAX, in which case,
61+
/// possible is if the number of NFA states exceeds `u32::MAX`, in which case,
6262
/// this function will return false.
6363
///
6464
/// This function will also return false if the given program has any Unicode
@@ -104,7 +104,7 @@ pub struct Cache {
104104
qnext: SparseSet,
105105
}
106106

107-
/// CacheInner is logically just a part of Cache, but groups together fields
107+
/// `CacheInner` is logically just a part of Cache, but groups together fields
108108
/// that aren't passed as function parameters throughout search. (This split
109109
/// is mostly an artifact of the borrow checker. It is happily paid.)
110110
#[derive(Clone, Debug)]
@@ -162,8 +162,8 @@ struct CacheInner {
162162
/// It is laid out in row-major order, with states as rows and byte class
163163
/// transitions as columns.
164164
///
165-
/// The transition table is responsible for producing valid StatePtrs. A
166-
/// StatePtr points to the start of a particular row in this table. When
165+
/// The transition table is responsible for producing valid `StatePtrs`. A
166+
/// `StatePtr` points to the start of a particular row in this table. When
167167
/// indexing to find the next state this allows us to avoid a multiplication
168168
/// when computing an index into the table.
169169
#[derive(Clone)]
@@ -252,7 +252,7 @@ impl<T> Result<T> {
252252
}
253253
}
254254

255-
/// State is a DFA state. It contains an ordered set of NFA states (not
255+
/// `State` is a DFA state. It contains an ordered set of NFA states (not
256256
/// necessarily complete) and a smattering of flags.
257257
///
258258
/// The flags are packed into the first byte of data.
@@ -271,7 +271,7 @@ struct State{
271271
data: Box<[u8]>,
272272
}
273273

274-
/// InstPtr is a 32 bit pointer into a sequence of opcodes (i.e., it indexes
274+
/// `InstPt`r is a 32 bit pointer into a sequence of opcodes (i.e., it indexes
275275
/// an NFA state).
276276
///
277277
/// Throughout this library, this is usually set to `usize`, but we force a
@@ -322,7 +322,8 @@ impl State {
322322
}
323323
}
324324

325-
/// StatePtr is a 32 bit pointer to the start of a row in the transition table.
325+
/// `StatePtr` is a 32 bit pointer to the start of a row in the transition
326+
/// table.
326327
///
327328
/// It has many special values. There are two types of special values:
328329
/// sentinels and flags.
@@ -345,7 +346,8 @@ impl State {
345346
///
346347
/// The other type of state pointer is a state pointer with special flag bits.
347348
/// There are two flags: a start flag and a match flag. The lower bits of both
348-
/// kinds always contain a "valid" StatePtr (indicated by the STATE_MAX mask).
349+
/// kinds always contain a "valid" `StatePtr` (indicated by the `STATE_MAX`
350+
/// mask).
349351
///
350352
/// The start flag means that the state is a start state, and therefore may be
351353
/// subject to special prefix scanning optimizations.
@@ -482,7 +484,7 @@ impl<'a> Fsm<'a> {
482484
Some(STATE_DEAD) => return Result::NoMatch(at),
483485
Some(si) => si,
484486
};
485-
debug_assert!(dfa.start != STATE_UNKNOWN);
487+
debug_assert_ne!(dfa.start, STATE_UNKNOWN);
486488
dfa.exec_at(&mut cache.qcur, &mut cache.qnext, text)
487489
}
488490

@@ -515,7 +517,7 @@ impl<'a> Fsm<'a> {
515517
Some(STATE_DEAD) => return Result::NoMatch(at),
516518
Some(si) => si,
517519
};
518-
debug_assert!(dfa.start != STATE_UNKNOWN);
520+
debug_assert_ne!(dfa.start, STATE_UNKNOWN);
519521
dfa.exec_at_reverse(&mut cache.qcur, &mut cache.qnext, text)
520522
}
521523

@@ -527,7 +529,7 @@ impl<'a> Fsm<'a> {
527529
text: &[u8],
528530
at: usize,
529531
) -> Result<usize> {
530-
debug_assert!(matches.len() == prog.matches.len());
532+
debug_assert_eq!(matches.len(), prog.matches.len());
531533
let mut cache = cache.borrow_mut();
532534
let mut cache = &mut cache.dfa;
533535
let mut dfa = Fsm {
@@ -549,14 +551,14 @@ impl<'a> Fsm<'a> {
549551
Some(STATE_DEAD) => return Result::NoMatch(at),
550552
Some(si) => si,
551553
};
552-
debug_assert!(dfa.start != STATE_UNKNOWN);
554+
debug_assert_ne!(dfa.start, STATE_UNKNOWN);
553555
let result = dfa.exec_at(&mut cache.qcur, &mut cache.qnext, text);
554556
if result.is_match() {
555557
if matches.len() == 1 {
556558
matches[0] = true;
557559
} else {
558-
debug_assert!(dfa.last_match_si != STATE_UNKNOWN);
559-
debug_assert!(dfa.last_match_si != STATE_DEAD);
560+
debug_assert_ne!(dfa.last_match_si, STATE_UNKNOWN);
561+
debug_assert_ne!(dfa.last_match_si, STATE_DEAD);
560562
for ip in dfa.state(dfa.last_match_si).inst_ptrs() {
561563
if let Inst::Match(slot) = dfa.prog[ip] {
562564
matches[slot] = true;
@@ -728,7 +730,7 @@ impl<'a> Fsm<'a> {
728730
Some(STATE_DEAD) => return result.set_non_match(at),
729731
Some(si) => si,
730732
};
731-
debug_assert!(next_si != STATE_UNKNOWN);
733+
debug_assert_ne!(next_si, STATE_UNKNOWN);
732734
if next_si & STATE_MATCH > 0 {
733735
next_si &= !STATE_MATCH;
734736
result = Result::Match(at - 1);
@@ -752,7 +754,7 @@ impl<'a> Fsm<'a> {
752754
Some(STATE_DEAD) => return result.set_non_match(text.len()),
753755
Some(si) => si & !STATE_START,
754756
};
755-
debug_assert!(prev_si != STATE_UNKNOWN);
757+
debug_assert_ne!(prev_si, STATE_UNKNOWN);
756758
if prev_si & STATE_MATCH > 0 {
757759
prev_si &= !STATE_MATCH;
758760
self.last_match_si = prev_si;
@@ -833,7 +835,7 @@ impl<'a> Fsm<'a> {
833835
Some(STATE_DEAD) => return result.set_non_match(at),
834836
Some(si) => si,
835837
};
836-
debug_assert!(next_si != STATE_UNKNOWN);
838+
debug_assert_ne!(next_si, STATE_UNKNOWN);
837839
if next_si & STATE_MATCH > 0 {
838840
next_si &= !STATE_MATCH;
839841
result = Result::Match(at + 1);
@@ -854,7 +856,7 @@ impl<'a> Fsm<'a> {
854856
Some(STATE_DEAD) => return result.set_non_match(0),
855857
Some(si) => si,
856858
};
857-
debug_assert!(prev_si != STATE_UNKNOWN);
859+
debug_assert_ne!(prev_si, STATE_UNKNOWN);
858860
if prev_si & STATE_MATCH > 0 {
859861
prev_si &= !STATE_MATCH;
860862
self.last_match_si = prev_si;
@@ -998,16 +1000,19 @@ impl<'a> Fsm<'a> {
9981000
}
9991001
}
10001002
}
1001-
let mut cache = true;
1002-
if b.is_eof() && self.prog.matches.len() > 1 {
1003+
1004+
let cache = if b.is_eof() && self.prog.matches.len() > 1 {
10031005
// If we're processing the last byte of the input and we're
10041006
// matching a regex set, then make the next state contain the
10051007
// previous states transitions. We do this so that the main
10061008
// matching loop can extract all of the match instructions.
10071009
mem::swap(qcur, qnext);
10081010
// And don't cache this state because it's totally bunk.
1009-
cache = false;
1010-
}
1011+
false
1012+
} else {
1013+
true
1014+
};
1015+
10111016
// We've now built up the set of NFA states that ought to comprise the
10121017
// next DFA state, so try to find it in the cache, and if it doesn't
10131018
// exist, cache it.
@@ -1030,9 +1035,9 @@ impl<'a> Fsm<'a> {
10301035
next = self.start_ptr(next);
10311036
}
10321037
if next <= STATE_MAX && self.state(next).flags().is_match() {
1033-
next = STATE_MATCH | next;
1038+
next |= STATE_MATCH;
10341039
}
1035-
debug_assert!(next != STATE_UNKNOWN);
1040+
debug_assert_ne!(next, STATE_UNKNOWN);
10361041
// And now store our state in the current state's next list.
10371042
if cache {
10381043
let cls = self.byte_class(b);
@@ -1113,9 +1118,9 @@ impl<'a> Fsm<'a> {
11131118
NotWordBoundary if flags.not_word_boundary => {
11141119
self.cache.stack.push(inst.goto as InstPtr);
11151120
}
1116-
StartLine | EndLine | StartText | EndText => {}
1117-
WordBoundaryAscii | NotWordBoundaryAscii => {}
1118-
WordBoundary | NotWordBoundary => {}
1121+
StartLine | EndLine | StartText | EndText
1122+
| WordBoundaryAscii | NotWordBoundaryAscii
1123+
| WordBoundary | NotWordBoundary => {}
11191124
}
11201125
}
11211126
Save(ref inst) => self.cache.stack.push(inst.goto as InstPtr),
@@ -1167,11 +1172,10 @@ impl<'a> Fsm<'a> {
11671172
return Some(si);
11681173
}
11691174
// If the cache has gotten too big, wipe it.
1170-
if self.approximate_size() > self.prog.dfa_size_limit {
1171-
if !self.clear_cache_and_save(current_state) {
1172-
// Ooops. DFA is giving up.
1173-
return None;
1174-
}
1175+
if self.approximate_size() > self.prog.dfa_size_limit
1176+
&& !self.clear_cache_and_save(current_state) {
1177+
// Ooops. DFA is giving up.
1178+
return None;
11751179
}
11761180
// Allocate room for our state and add it.
11771181
self.add_state(key)
@@ -1210,8 +1214,7 @@ impl<'a> Fsm<'a> {
12101214
let ip = usize_to_u32(ip);
12111215
match self.prog[ip as usize] {
12121216
Char(_) | Ranges(_) => unreachable!(),
1213-
Save(_) => {}
1214-
Split(_) => {}
1217+
Save(_) | Split(_) => {}
12151218
Bytes(_) => push_inst_ptr(&mut insts, &mut prev, ip),
12161219
EmptyLook(_) => {
12171220
state_flags.set_empty();
@@ -1301,7 +1304,7 @@ impl<'a> Fsm<'a> {
13011304
self.cache.trans.clear();
13021305
self.cache.states.clear();
13031306
self.cache.compiled.clear();
1304-
for s in self.cache.start_states.iter_mut() {
1307+
for s in &mut self.cache.start_states {
13051308
*s = STATE_UNKNOWN;
13061309
}
13071310
// The unwraps are OK because we just cleared the cache and therefore
@@ -1374,7 +1377,7 @@ impl<'a> Fsm<'a> {
13741377
// matches are delayed by one byte, start states can never be match
13751378
// states.
13761379
let flagi = {
1377-
(((empty_flags.start as u8) << 0) |
1380+
((empty_flags.start as u8) |
13781381
((empty_flags.end as u8) << 1) |
13791382
((empty_flags.start_line as u8) << 2) |
13801383
((empty_flags.end_line as u8) << 3) |
@@ -1411,9 +1414,9 @@ impl<'a> Fsm<'a> {
14111414
let mut empty_flags = EmptyFlags::default();
14121415
let mut state_flags = StateFlags::default();
14131416
empty_flags.start = at == 0;
1414-
empty_flags.end = text.len() == 0;
1417+
empty_flags.end = text.is_empty();
14151418
empty_flags.start_line = at == 0 || text[at - 1] == b'\n';
1416-
empty_flags.end_line = text.len() == 0;
1419+
empty_flags.end_line = text.is_empty();
14171420

14181421
let is_word_last = at > 0 && Byte::byte(text[at - 1]).is_ascii_word();
14191422
let is_word = at < text.len() && Byte::byte(text[at]).is_ascii_word();
@@ -1440,9 +1443,9 @@ impl<'a> Fsm<'a> {
14401443
let mut empty_flags = EmptyFlags::default();
14411444
let mut state_flags = StateFlags::default();
14421445
empty_flags.start = at == text.len();
1443-
empty_flags.end = text.len() == 0;
1446+
empty_flags.end = text.is_empty();
14441447
empty_flags.start_line = at == text.len() || text[at] == b'\n';
1445-
empty_flags.end_line = text.len() == 0;
1448+
empty_flags.end_line = text.is_empty();
14461449

14471450
let is_word_last =
14481451
at < text.len() && Byte::byte(text[at]).is_ascii_word();
@@ -1499,10 +1502,8 @@ impl<'a> Fsm<'a> {
14991502
self.cache.states.push(state.clone());
15001503
self.cache.compiled.insert(state, si);
15011504
// Transition table and set of states and map should all be in sync.
1502-
debug_assert!(self.cache.states.len()
1503-
== self.cache.trans.num_states());
1504-
debug_assert!(self.cache.states.len()
1505-
== self.cache.compiled.len());
1505+
debug_assert_eq!(self.cache.states.len(), self.cache.trans.num_states());
1506+
debug_assert_eq!(self.cache.states.len(), self.cache.compiled.len());
15061507
Some(si)
15071508
}
15081509

0 commit comments

Comments
 (0)