Skip to content

Commit 0ccca0d

Browse files
committed
Fix clippy
1 parent 8a02610 commit 0ccca0d

File tree

22 files changed

+32
-35
lines changed

22 files changed

+32
-35
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
#![allow(
105105
missing_docs,
106106
trivial_casts,
107+
clippy::needless_continue,
107108
clippy::cargo_common_metadata,
108109
clippy::cast_possible_truncation,
109110
clippy::cast_possible_wrap,

src/problem_0043_multiply_strings/fold.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ impl Solution {
1313
let mut acc_rev = acc.into_iter();
1414
let mut carry = 0;
1515

16-
for ch in std::iter::repeat(0)
17-
.take(count)
16+
for ch in std::iter::repeat_n(0, count)
1817
.chain(num1.as_bytes().iter().rev().map(|x| x - b'0'))
1918
{
2019
let caculate = x * ch + acc_rev.next().unwrap_or_default() + carry;

src/problem_0068_text_justification/iterative.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ impl Solution {
2424
let base = space / slot;
2525
let rest = space % slot;
2626

27-
let mut iter = std::iter::repeat(base + 1)
28-
.take(rest)
29-
.chain(std::iter::repeat(base).take(slot - rest));
27+
let mut iter = std::iter::repeat_n(base + 1, rest)
28+
.chain(std::iter::repeat_n(base, slot - rest));
3029

3130
let mut part = String::with_capacity(max_width);
3231
for s in &words[start..idx] {

src/problem_0095_unique_binary_search_trees_ii/recursive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl Solution {
1010
fn helper(start: i32, end: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
1111
if start == end {
1212
return vec![None];
13-
};
13+
}
1414

1515
let mut result = Vec::new();
1616
for val in start..end {

src/problem_0096_unique_binary_search_trees/recursive.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ use std::collections::HashMap;
55
impl Solution {
66
pub fn num_trees(n: i32) -> i32 {
77
fn helper(start: i32, end: i32, cache: &mut HashMap<i32, i32>) -> i32 {
8-
if start == (start + end) / 2 {
8+
if start == i32::midpoint(start, end) {
99
return 1;
10-
};
10+
}
1111
if let Some(cache) = cache.get(&(end - start)) {
1212
return *cache;
1313
}
1414

1515
let mut result = 0;
16-
for val in start..(start + end) / 2 {
16+
for val in start..i32::midpoint(start, end) {
1717
let left_tree = helper(start, val, cache);
1818
let right_tree = helper(val + 1, end, cache);
1919
result += 2 * left_tree * right_tree;
2020
}
2121
if (start + end) % 2 != 0 {
22-
result += helper(start, (start + end) / 2, cache)
23-
* helper((start + end) / 2 + 1, end, cache);
22+
result += helper(start, i32::midpoint(start, end), cache)
23+
* helper(i32::midpoint(start, end) + 1, end, cache);
2424
}
2525
cache.insert(end - start, result);
2626

src/problem_0098_validate_binary_search_tree/recursive.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ impl Solution {
99
pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
1010
#[allow(clippy::ref_option)]
1111
fn helper(node: &Option<Rc<RefCell<TreeNode>>>, last: &mut Option<i32>) -> bool {
12-
node.as_ref().map_or(true, |node| {
12+
node.as_ref().is_none_or(|node| {
1313
helper(&node.borrow().left, last)
14-
&& std::mem::replace(last, Some(node.borrow().val))
15-
.map_or(true, |x| x < node.borrow().val)
14+
&& last
15+
.replace(node.borrow().val)
16+
.is_none_or(|x| x < node.borrow().val)
1617
&& helper(&node.borrow().right, last)
1718
})
1819
}

src/problem_0101_symmetric_tree/recursive.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ impl Solution {
2121
_ => false,
2222
}
2323
}
24-
root.map_or(true, |root| {
25-
helper(&root.borrow().left, &root.borrow().right)
26-
})
24+
root.is_none_or(|root| helper(&root.borrow().left, &root.borrow().right))
2725
}
2826
}
2927

src/problem_0162_find_peak_element/binary_search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ impl Solution {
55
let (mut left, mut right) = (0, nums.len() - 1);
66

77
while left < right {
8-
let mid = (left + right) / 2;
8+
let mid = usize::midpoint(left, right);
99
if nums[mid] < nums[mid + 1] {
1010
left = mid + 1;
1111
} else {

src/problem_0441_arranging_coins/math.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub struct Solution;
22

33
impl Solution {
44
pub fn arrange_coins(n: i32) -> i32 {
5-
((-1.0 + 8.0f64.mul_add(f64::from(n), 1.0).sqrt()) / 2.0) as i32
5+
f64::midpoint(-1.0, 8.0f64.mul_add(f64::from(n), 1.0).sqrt()) as i32
66
}
77
}
88

src/problem_0481_magical_string/queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl Solution {
1515
}
1616
count += x;
1717
prev = 1 + i32::from(prev == 1);
18-
queue.extend(std::iter::repeat(prev).take(x as usize));
18+
queue.extend(std::iter::repeat_n(prev, x as usize));
1919
result += if count > n { 1 } else { x } * i32::from(prev == 1);
2020
}
2121

0 commit comments

Comments
 (0)