Skip to content

Commit 8ffd85d

Browse files
committed
Auto merge of #376 - lukaslueg:clippy, r=BurntSushi
Fix clippy warnings This fixes all clippy warnings, mostly lifetime elisions and useless borrows. Clippy now passes on nightly; on stable/without clippy, no lint warnings are given. All tests still pass. I've not moved any code around, which is the reason for allowing `string_lit_as_bytes` in so many places. Up for review
2 parents 6d07ffe + fbb2ba7 commit 8ffd85d

19 files changed

+117
-110
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-single.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn main() {
4848
(regex!("Y"), "(c|t)"),
4949
];
5050
let mut seq = seq;
51-
for (re, replacement) in substs.into_iter() {
51+
for (re, replacement) in substs {
5252
seq = re.replace_all(&seq, replacement).into_owned();
5353
}
5454
println!("\n{}\n{}\n{}", ilen, clen, seq.len());

examples/shootout-regex-dna.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn main() {
5555
(regex!("Y"), "(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

src/compile.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ impl<'a, 'b> CompileClass<'a, 'b> {
833833
let mut utf8_seqs = self.c.utf8_seqs.take().unwrap();
834834
self.c.suffix_cache.clear();
835835

836-
for (i, ref range) in self.ranges.iter().enumerate() {
836+
for (i, range) in self.ranges.iter().enumerate() {
837837
let is_last_range = i + 1 == self.ranges.len();
838838
utf8_seqs.reset(range.start, range.end);
839839
let mut it = (&mut utf8_seqs).peekable();
@@ -916,7 +916,7 @@ impl<'a, 'b> CompileClass<'a, 'b> {
916916
}
917917
}
918918

919-
/// SuffixCache is a simple bounded hash map for caching suffix entries in
919+
/// `SuffixCache` is a simple bounded hash map for caching suffix entries in
920920
/// UTF-8 automata. For example, consider the Unicode range \u{0}-\u{FFFF}.
921921
/// The set of byte ranges looks like this:
922922
///
@@ -1050,6 +1050,9 @@ impl ByteClassSet {
10501050
}
10511051

10521052
fn u32_to_usize(n: u32) -> usize {
1053+
// In case usize is less than 32 bits, we need to guard against overflow.
1054+
// On most platforms this compiles to nothing.
1055+
// TODO Use `std::convert::TryFrom` once it's stable.
10531056
if (n as u64) > (::std::usize::MAX as u64) {
10541057
panic!("BUG: {} is too big to be pointer sized", n)
10551058
}

src/dfa.rs

Lines changed: 37 additions & 32 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+
/// `InstPtr` 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.
@@ -998,16 +1000,20 @@ impl<'a> Fsm<'a> {
9981000
}
9991001
}
10001002
}
1001-
let mut cache = true;
1002-
if b.is_eof() && self.prog.matches.len() > 1 {
1003-
// If we're processing the last byte of the input and we're
1004-
// matching a regex set, then make the next state contain the
1005-
// previous states transitions. We do this so that the main
1006-
// matching loop can extract all of the match instructions.
1007-
mem::swap(qcur, qnext);
1008-
// And don't cache this state because it's totally bunk.
1009-
cache = false;
1010-
}
1003+
1004+
let cache =
1005+
if b.is_eof() && self.prog.matches.len() > 1 {
1006+
// If we're processing the last byte of the input and we're
1007+
// matching a regex set, then make the next state contain the
1008+
// previous states transitions. We do this so that the main
1009+
// matching loop can extract all of the match instructions.
1010+
mem::swap(qcur, qnext);
1011+
// And don't cache this state because it's totally bunk.
1012+
false
1013+
} else {
1014+
true
1015+
};
1016+
10111017
// We've now built up the set of NFA states that ought to comprise the
10121018
// next DFA state, so try to find it in the cache, and if it doesn't
10131019
// exist, cache it.
@@ -1030,7 +1036,7 @@ impl<'a> Fsm<'a> {
10301036
next = self.start_ptr(next);
10311037
}
10321038
if next <= STATE_MAX && self.state(next).flags().is_match() {
1033-
next = STATE_MATCH | next;
1039+
next |= STATE_MATCH;
10341040
}
10351041
debug_assert!(next != STATE_UNKNOWN);
10361042
// And now store our state in the current state's next list.
@@ -1113,9 +1119,9 @@ impl<'a> Fsm<'a> {
11131119
NotWordBoundary if flags.not_word_boundary => {
11141120
self.cache.stack.push(inst.goto as InstPtr);
11151121
}
1116-
StartLine | EndLine | StartText | EndText => {}
1117-
WordBoundaryAscii | NotWordBoundaryAscii => {}
1118-
WordBoundary | NotWordBoundary => {}
1122+
StartLine | EndLine | StartText | EndText
1123+
| WordBoundaryAscii | NotWordBoundaryAscii
1124+
| WordBoundary | NotWordBoundary => {}
11191125
}
11201126
}
11211127
Save(ref inst) => self.cache.stack.push(inst.goto as InstPtr),
@@ -1167,12 +1173,12 @@ impl<'a> Fsm<'a> {
11671173
return Some(si);
11681174
}
11691175
// 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) {
1176+
if self.approximate_size() > self.prog.dfa_size_limit
1177+
&& !self.clear_cache_and_save(current_state)
1178+
{
11721179
// Ooops. DFA is giving up.
11731180
return None;
11741181
}
1175-
}
11761182
// Allocate room for our state and add it.
11771183
self.add_state(key)
11781184
}
@@ -1210,8 +1216,7 @@ impl<'a> Fsm<'a> {
12101216
let ip = usize_to_u32(ip);
12111217
match self.prog[ip as usize] {
12121218
Char(_) | Ranges(_) => unreachable!(),
1213-
Save(_) => {}
1214-
Split(_) => {}
1219+
Save(_) | Split(_) => {}
12151220
Bytes(_) => push_inst_ptr(&mut insts, &mut prev, ip),
12161221
EmptyLook(_) => {
12171222
state_flags.set_empty();
@@ -1301,7 +1306,7 @@ impl<'a> Fsm<'a> {
13011306
self.cache.trans.clear();
13021307
self.cache.states.clear();
13031308
self.cache.compiled.clear();
1304-
for s in self.cache.start_states.iter_mut() {
1309+
for s in &mut self.cache.start_states {
13051310
*s = STATE_UNKNOWN;
13061311
}
13071312
// The unwraps are OK because we just cleared the cache and therefore
@@ -1411,9 +1416,9 @@ impl<'a> Fsm<'a> {
14111416
let mut empty_flags = EmptyFlags::default();
14121417
let mut state_flags = StateFlags::default();
14131418
empty_flags.start = at == 0;
1414-
empty_flags.end = text.len() == 0;
1419+
empty_flags.end = text.is_empty();
14151420
empty_flags.start_line = at == 0 || text[at - 1] == b'\n';
1416-
empty_flags.end_line = text.len() == 0;
1421+
empty_flags.end_line = text.is_empty();
14171422

14181423
let is_word_last = at > 0 && Byte::byte(text[at - 1]).is_ascii_word();
14191424
let is_word = at < text.len() && Byte::byte(text[at]).is_ascii_word();
@@ -1440,9 +1445,9 @@ impl<'a> Fsm<'a> {
14401445
let mut empty_flags = EmptyFlags::default();
14411446
let mut state_flags = StateFlags::default();
14421447
empty_flags.start = at == text.len();
1443-
empty_flags.end = text.len() == 0;
1448+
empty_flags.end = text.is_empty();
14441449
empty_flags.start_line = at == text.len() || text[at] == b'\n';
1445-
empty_flags.end_line = text.len() == 0;
1450+
empty_flags.end_line = text.is_empty();
14461451

14471452
let is_word_last =
14481453
at < text.len() && Byte::byte(text[at]).is_ascii_word();

0 commit comments

Comments
 (0)