Skip to content

Commit

Permalink
Fix clippy::branches_sharing_code lint violations
Browse files Browse the repository at this point in the history
  • Loading branch information
lopopolo committed Jun 20, 2021
1 parent f2cebec commit fad6fd5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 34 deletions.
9 changes: 3 additions & 6 deletions artichoke-backend/src/convert/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,16 @@ mod tests {
if !unsafe { sys::mrb_sys_value_is_true(value) } {
return false;
}
if unsafe { sys::mrb_sys_value_is_nil(value) } {
return false;
}
} else {
if !unsafe { sys::mrb_sys_value_is_false(value) } {
return false;
}
if unsafe { sys::mrb_sys_value_is_true(value) } {
return false;
}
if unsafe { sys::mrb_sys_value_is_nil(value) } {
return false;
}
}
if unsafe { sys::mrb_sys_value_is_nil(value) } {
return false;
}
true
}
Expand Down
8 changes: 3 additions & 5 deletions artichoke-backend/src/extn/core/regexp/trampoline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@ pub fn initialize(

pub fn escape(interp: &mut Artichoke, mut pattern: Value) -> Result<Value, Error> {
let pattern_vec;
let pattern = if matches!(pattern.ruby_type(), Ruby::Symbol) {
if matches!(pattern.ruby_type(), Ruby::Symbol) {
let symbol = unsafe { Symbol::unbox_from_value(&mut pattern, interp)? };
pattern_vec = symbol.bytes(interp).to_vec();
pattern_vec.as_slice()
} else {
// Safety:
//
// Convert the bytes to an owned vec to prevent the underlying `RString`
// backing `pattern` from being freed during a garbage collection.
pattern_vec = unsafe { implicitly_convert_to_string(interp, &mut pattern)?.to_vec() };
pattern_vec.as_slice()
};
let pattern = Regexp::escape(pattern)?;
}
let pattern = Regexp::escape(&pattern_vec)?;
Ok(interp.convert_mut(pattern))
}

Expand Down
4 changes: 1 addition & 3 deletions artichoke-backend/src/load_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ pub fn normalize_slashes(path: PathBuf) -> Result<Vec<u8>, ConvertBytesError> {
*byte = b'/';
}
}
Ok(path)
} else {
Ok(path)
}
Ok(path)
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions spinoso-string/src/chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl<'a> Iterator for ConventionallyUtf8<'a> {
type Item = &'a [u8];

#[inline]
#[allow(clippy::branches_sharing_code)] // for clarity.
fn next(&mut self) -> Option<Self::Item> {
if let Some(slice) = self.invalid_bytes.next() {
return Some(slice);
Expand Down
34 changes: 14 additions & 20 deletions spinoso-time/src/time/chrono/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,25 @@ impl Time {
// [2.6.3] > t = Time.at(10, -10e6); [t.to_i, t.nsec]
// => [0, 0]
// ```

// full seconds given via `sub_second_nanos` are carried over into
// `seconds`.
let overflow = sub_second_nanos / i64::from(NANOS_IN_SECOND);
let timestamp = seconds.checked_add(overflow)?;

// Only the `sub_second_nanos` that fit within the range bounded by
// `0..nanos_in_second` are storeed in the `sub_second_nanos` field
// on the `Time` struct.
//
// `sub_second_nanos` will be in the range of (-nanos in second, 0] if
// `sub_second_nanos < 0`, [0, nanos in second) otherwise.
let sub_second_nanos = sub_second_nanos % i64::from(NANOS_IN_SECOND);

let (timestamp, sub_second_nanos) = if sub_second_nanos > 0 {
// full seconds given via `sub_second_nanos` are carried over into
// `seconds`.
let overflow = sub_second_nanos / i64::from(NANOS_IN_SECOND);
let timestamp = seconds.checked_add(overflow)?;

// Only the `sub_second_nanos` that fit within the range bounded by
// `0..nanos_in_second` are storeed in the `sub_second_nanos` field
// on the `Time` struct.
let sub_second_nanos = sub_second_nanos % i64::from(NANOS_IN_SECOND);
let sub_second_nanos = u32::try_from(sub_second_nanos).ok()?;

(timestamp, sub_second_nanos)
} else {
// full seconds given via `sub_second_nanos` are carried over into
// `seconds`.
let overflow = sub_second_nanos / i64::from(NANOS_IN_SECOND);
let timestamp = seconds.checked_add(overflow)?;

// Only the `sub_second_nanos` that fit within the range bounded by
// `0..nanos_in_second` are storeed in the `sub_second_nanos` field
// on the `Time` struct.
//
// `sub_second_nanos` will be in the range of (-nanos in second, 0].
let sub_second_nanos = sub_second_nanos % i64::from(NANOS_IN_SECOND);
// Unchecked addition is OK here because the modulus operation above
// ensures that the magnitude of `sub_second_nanos` is less than
// `NANOS_IN_SECOND`.
Expand Down

0 comments on commit fad6fd5

Please sign in to comment.