Skip to content

Commit e3a5419

Browse files
committed
Use new stabilised let_chains feature of Rust
1 parent e5227a7 commit e3a5419

File tree

9 files changed

+58
-79
lines changed

9 files changed

+58
-79
lines changed

fuzzcheck/src/fuzzer.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,8 @@ where
525525
if let Some(input) = self.state.pool_storage.get_mut(idx.0).map(
526526
#[no_coverage]
527527
|x| &mut x.input,
528-
) {
529-
if input.generation == generation {
530-
input.unmutate(&self.state.mutator, unmutate_token);
531-
}
528+
) && input.generation == generation {
529+
input.unmutate(&self.state.mutator, unmutate_token);
532530
}
533531

534532
Ok(())

fuzzcheck/src/mutators/arc.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,16 @@ impl<T: Clone + 'static, M: Mutator<T>> Mutator<Arc<T>> for ArcMutator<M> {
119119
subvalue_provider: &dyn crate::SubValueProvider,
120120
max_cplx: f64,
121121
) -> Option<(Self::UnmutateToken, f64)> {
122-
if self.rng.u8(..CROSSOVER_RATE) == 0 {
123-
if let Some((subvalue, subcplx)) = step.crossover_step.get_next_subvalue(subvalue_provider, max_cplx) {
124-
if self.mutator.is_valid(subvalue) {
125-
let replacer = subvalue.clone();
126-
let old_value = value.as_ref().clone();
127-
// TODO: something more efficient
128-
*value = Arc::new(replacer);
129-
return Some((UnmutateToken::Replace(old_value), subcplx));
130-
}
131-
}
122+
if self.rng.u8(..CROSSOVER_RATE) == 0
123+
&& let Some((subvalue, subcplx)) = step.crossover_step.get_next_subvalue(subvalue_provider, max_cplx)
124+
&& self.mutator.is_valid(subvalue) {
125+
let replacer = subvalue.clone();
126+
let old_value = value.as_ref().clone();
127+
// TODO: something more efficient
128+
*value = Arc::new(replacer);
129+
return Some((UnmutateToken::Replace(old_value), subcplx));
132130
}
131+
133132
let mut v = value.as_ref().clone();
134133
if let Some((t, cplx)) =
135134
self.mutator

fuzzcheck/src/mutators/boxed.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,12 @@ impl<T: Clone + 'static, M: Mutator<T>> Mutator<Box<T>> for BoxMutator<M> {
114114
subvalue_provider: &dyn crate::SubValueProvider,
115115
max_cplx: f64,
116116
) -> Option<(Self::UnmutateToken, f64)> {
117-
if self.rng.u8(..CROSSOVER_RATE) == 0 {
118-
if let Some((subvalue, subcplx)) = step.crossover_step.get_next_subvalue(subvalue_provider, max_cplx) {
119-
if self.mutator.is_valid(subvalue) {
120-
let mut replacer = subvalue.clone();
121-
std::mem::swap(value.as_mut(), &mut replacer);
122-
return Some((UnmutateToken::Replace(replacer), subcplx));
123-
}
124-
}
117+
if self.rng.u8(..CROSSOVER_RATE) == 0
118+
&& let Some((subvalue, subcplx)) = step.crossover_step.get_next_subvalue(subvalue_provider, max_cplx)
119+
&& self.mutator.is_valid(subvalue) {
120+
let mut replacer = subvalue.clone();
121+
std::mem::swap(value.as_mut(), &mut replacer);
122+
return Some((UnmutateToken::Replace(replacer), subcplx));
125123
}
126124
if let Some((t, cplx)) = self
127125
.mutator

fuzzcheck/src/mutators/fixed_len_vector.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,12 @@ impl<T: Clone + 'static, M: Mutator<T>> Mutator<Vec<T>> for FixedLenVecMutator<T
331331
let old_el_cplx = self.mutators[choice].complexity(&value[choice], &cache.inner[choice]);
332332
let current_cplx = self.complexity(value, cache);
333333
let max_el_cplx = current_cplx - old_el_cplx - self.inherent_complexity;
334-
if let Some((el, new_el_cplx)) = step.get_next_subvalue(subvalue_provider, max_el_cplx) {
335-
if self.mutators[choice].is_valid(el) {
336-
let mut el = el.clone();
337-
std::mem::swap(&mut value[choice], &mut el);
338-
let cplx = cache.sum_cplx - old_el_cplx + new_el_cplx + self.inherent_complexity;
339-
let token = UnmutateVecToken::ReplaceElement(choice, el);
340-
return Some((token, cplx));
341-
}
334+
if let Some((el, new_el_cplx)) = step.get_next_subvalue(subvalue_provider, max_el_cplx) && self.mutators[choice].is_valid(el) {
335+
let mut el = el.clone();
336+
std::mem::swap(&mut value[choice], &mut el);
337+
let cplx = cache.sum_cplx - old_el_cplx + new_el_cplx + self.inherent_complexity;
338+
let token = UnmutateVecToken::ReplaceElement(choice, el);
339+
return Some((token, cplx));
342340
}
343341
}
344342
let current_cplx = self.complexity(value, cache);

fuzzcheck/src/mutators/rc.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,14 @@ impl<T: Clone + 'static, M: Mutator<T>> Mutator<Rc<T>> for RcMutator<M> {
118118
subvalue_provider: &dyn crate::SubValueProvider,
119119
max_cplx: f64,
120120
) -> Option<(Self::UnmutateToken, f64)> {
121-
if self.rng.u8(..CROSSOVER_RATE) == 0 {
122-
if let Some((subvalue, subcplx)) = step.crossover_step.get_next_subvalue(subvalue_provider, max_cplx) {
123-
if self.mutator.is_valid(subvalue) {
124-
let replacer = subvalue.clone();
125-
let old_value = value.as_ref().clone();
126-
// TODO: something more efficient
127-
*value = Rc::new(replacer);
128-
return Some((UnmutateToken::Replace(old_value), subcplx));
129-
}
130-
}
121+
if self.rng.u8(..CROSSOVER_RATE) == 0
122+
&& let Some((subvalue, subcplx)) = step.crossover_step.get_next_subvalue(subvalue_provider, max_cplx)
123+
&& self.mutator.is_valid(subvalue) {
124+
let replacer = subvalue.clone();
125+
let old_value = value.as_ref().clone();
126+
// TODO: something more efficient
127+
*value = Rc::new(replacer);
128+
return Some((UnmutateToken::Replace(old_value), subcplx));
131129
}
132130
let mut v = value.as_ref().clone();
133131
if let Some((t, cplx)) =

fuzzcheck/src/mutators/recursive.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,12 @@ where
311311

312312
let mut visit_subvalues = #[no_coverage]
313313
|subvalue: &dyn Any, cplx: f64| {
314-
if let Some(sub_self_value) = subvalue.downcast_ref::<T>() {
315-
if let Some(subcache) = self.mutator.validate_value(sub_self_value) {
316-
let subcplx = self.mutator.complexity(sub_self_value, &subcache);
317-
assert_eq!(cplx, subcplx);
318-
sub_self_values.push((sub_self_value as *const _, subcplx));
319-
}
314+
if let Some(sub_self_value) = subvalue.downcast_ref::<T>()
315+
&& let Some(subcache) = self.mutator.validate_value(sub_self_value)
316+
{
317+
let subcplx = self.mutator.complexity(sub_self_value, &subcache);
318+
assert_eq!(cplx, subcplx);
319+
sub_self_values.push((sub_self_value as *const _, subcplx));
320320
}
321321
};
322322

fuzzcheck/src/mutators/tuples.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -609,14 +609,10 @@ mod tuple1 {
609609
subvalue_provider: &dyn crate::SubValueProvider,
610610
max_cplx: f64,
611611
) -> Option<(Self::UnmutateToken, f64)> {
612-
if self.rng.u8(..CROSSOVER_RATE) == 0 {
613-
if let Some((subvalue, subcplx)) = step.crossover_step.get_next_subvalue(subvalue_provider, max_cplx) {
614-
if self.mutator_0.is_valid(subvalue) {
615-
let mut replacer = subvalue.clone();
616-
std::mem::swap(value.0, &mut replacer);
617-
return Some((UnmutateTuple1Token::Replace(replacer), subcplx));
618-
}
619-
}
612+
if self.rng.u8(..CROSSOVER_RATE) == 0 && let Some((subvalue, subcplx)) = step.crossover_step.get_next_subvalue(subvalue_provider, max_cplx) && self.mutator_0.is_valid(subvalue) {
613+
let mut replacer = subvalue.clone();
614+
std::mem::swap(value.0, &mut replacer);
615+
return Some((UnmutateTuple1Token::Replace(replacer), subcplx));
620616
}
621617
if let Some((token, cplx)) =
622618
self.mutator_0

fuzzcheck/src/sensors_and_pools/maximise_each_counter_pool.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,10 @@ where
167167
let pool_counter = self.highest_counts[index];
168168
if pool_counter < counter {
169169
state.push((index, counter));
170-
} else if pool_counter == counter {
171-
if let Some(candidate_key) = self.best_input_for_counter[index] {
172-
if self.inputs[candidate_key].cplx > complexity {
173-
state.push((index, counter));
174-
}
175-
}
170+
} else if pool_counter == counter
171+
&& let Some(candidate_key) = self.best_input_for_counter[index]
172+
&& self.inputs[candidate_key].cplx > complexity {
173+
state.push((index, counter));
176174
}
177175
}
178176
if state.is_empty() {

fuzzcheck/src/sensors_and_pools/most_n_diverse_pool.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,9 @@ impl MostNDiversePool {
9595
if let Some(worst_input) = self.worst_input_idx.map(
9696
#[no_coverage]
9797
|idx| &self.inputs[idx],
98-
) {
99-
if (*nbr_new_counters > worst_input.nbr_unique_counters)
100-
|| (*nbr_new_counters == worst_input.nbr_unique_counters && worst_input.cplx > input_complexity)
101-
{
102-
return true;
103-
}
98+
) && ((*nbr_new_counters > worst_input.nbr_unique_counters) || (*nbr_new_counters == worst_input.nbr_unique_counters && worst_input.cplx > input_complexity))
99+
{
100+
return true;
104101
}
105102

106103
let mut common_unique_counters = FixedBitSet::with_capacity(counters.len());
@@ -180,19 +177,16 @@ where
180177
if let Some(worst_input) = self.worst_input_idx.map(
181178
#[no_coverage]
182179
|idx| &mut self.inputs[idx],
183-
) {
184-
if (nbr_new_counters > worst_input.nbr_unique_counters)
185-
|| (nbr_new_counters == worst_input.nbr_unique_counters && worst_input.cplx > complexity)
186-
{
187-
let worst_input_data = worst_input.pool_idx;
188-
*worst_input = new_input;
189-
self.recompute_state_from_inputs_vec();
190-
return vec![CorpusDelta {
191-
path: PathBuf::new().join(&self.name),
192-
add: true,
193-
remove: vec![worst_input_data],
194-
}];
195-
}
180+
) && ((nbr_new_counters > worst_input.nbr_unique_counters)
181+
|| (nbr_new_counters == worst_input.nbr_unique_counters && worst_input.cplx > complexity)) {
182+
let worst_input_data = worst_input.pool_idx;
183+
*worst_input = new_input;
184+
self.recompute_state_from_inputs_vec();
185+
return vec![CorpusDelta {
186+
path: PathBuf::new().join(&self.name),
187+
add: true,
188+
remove: vec![worst_input_data],
189+
}];
196190
}
197191

198192
let mut common_unique_counters = FixedBitSet::with_capacity(counters.len());

0 commit comments

Comments
 (0)