Skip to content

Commit 09cb4c7

Browse files
authored
Fix clippy warnings in example solutions (#1747)
1 parent d855eb6 commit 09cb4c7

30 files changed

Lines changed: 121 additions & 66 deletions

File tree

docs/archived/exercise-concepts/luhn.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ Canonical
4747

4848
```rust
4949
pub fn is_valid(candidate: &str) -> bool {
50-
if candidate.chars().filter(|c| c.is_digit(10)).take(2).count() <= 1
51-
|| candidate.chars().any(|c| !c.is_digit(10) && c != ' ')
50+
if candidate.chars().filter(|c| c.is_ascii_digit()).take(2).count() <= 1
51+
|| candidate.chars().any(|c| !c.is_ascii_digit() && c != ' ')
5252
{
5353
return false;
5454
}

exercises/concept/csv-builder/.meta/exemplar.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ impl CsvRecordBuilder {
1616
self.content.push(',');
1717
}
1818

19-
if val.contains(",") || val.contains(r#"""#) || val.contains("\n") {
19+
if val.contains(',') || val.contains('"') || val.contains('\n') {
2020
self.content.push('"');
21-
self.content.push_str(&val.replace(r#"""#, r#""""#));
21+
self.content.push_str(&val.replace('"', r#""""#));
2222
self.content.push('"');
2323
} else {
2424
self.content.push_str(val);
@@ -30,3 +30,9 @@ impl CsvRecordBuilder {
3030
self.content
3131
}
3232
}
33+
34+
impl Default for CsvRecordBuilder {
35+
fn default() -> Self {
36+
Self::new()
37+
}
38+
}

exercises/practice/acronym/.meta/example.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub fn abbreviate(phrase: &str) -> String {
22
phrase
33
.split(|c: char| c.is_whitespace() || (c != '\'' && !c.is_alphanumeric()))
4-
.flat_map(|word| split_camel(word))
4+
.flat_map(split_camel)
55
.filter_map(|word| word.chars().next())
66
.collect::<String>()
77
.to_uppercase()

exercises/practice/affine-cipher/.meta/example.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn decode(ciphertext: &str, a: i32, b: i32) -> Result<String, AffineCipherEr
4343
/// Encodes a single char with the key (`a`, `b`). The key is assumed to be valid (i.e. `a` should
4444
/// be coprime to 26).
4545
fn encode_char(ch: char, a: i32, b: i32) -> char {
46-
if ch.is_digit(10) {
46+
if ch.is_ascii_digit() {
4747
ch
4848
} else {
4949
let index = (ch as i32) - ('a' as i32);
@@ -54,7 +54,7 @@ fn encode_char(ch: char, a: i32, b: i32) -> char {
5454

5555
/// Decodes a single char using `inv` (the modular multiplicative inverse of `a`) and `b`.
5656
fn decode_char(ch: char, inv: i32, b: i32) -> char {
57-
if ch.is_digit(10) {
57+
if ch.is_ascii_digit() {
5858
ch
5959
} else {
6060
let index = (ch as i32) - ('a' as i32);
@@ -82,7 +82,7 @@ fn modular_multiplicative_inverse(a: i32) -> Option<i32> {
8282
if rs.0 == 1 {
8383
// ts.0 gives a number such that (s * 26 + t * a) % 26 == 1. Since (s * 26) % 26 == 0,
8484
// we can further reduce this to (t * a) % 26 == 1. In other words, t is the MMI of a.
85-
Some(ts.0 as i32)
85+
Some(ts.0)
8686
} else {
8787
None
8888
}

exercises/practice/atbash-cipher/.meta/example.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ fn ascii(ch: char) -> u8 {
33
}
44

55
fn get_transpose(ch: char) -> char {
6-
if ch.is_digit(10) {
6+
if ch.is_ascii_digit() {
77
ch
88
} else {
99
(ascii('z') - ascii(ch) + ascii('a')) as char

exercises/practice/book-store/.meta/example.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::cmp::Ordering;
33
use std::collections::hash_map::DefaultHasher;
44
use std::collections::{BTreeSet, HashSet};
55
use std::hash::{Hash, Hasher};
6-
use std::mem;
76

87
type Book = u32;
98
type Price = u32;
@@ -125,7 +124,7 @@ impl Iterator for DecomposeGroups {
125124
// then move the last item from the most populous group into a new group, alone,
126125
// and return
127126
let return_value = self.next.clone();
128-
if let Some(groups) = mem::replace(&mut self.next, None) {
127+
if let Some(groups) = self.next.take() {
129128
if !(groups.is_empty() || groups.iter().all(|g| g.0.borrow().len() == 1)) {
130129
let mut hypothetical;
131130
for mpg_book in groups[0].0.borrow().iter() {
@@ -169,7 +168,7 @@ impl DecomposeGroups {
169168
let mut book_groups = Vec::new();
170169
'nextbook: for book in books {
171170
for Group(book_group) in book_groups.iter() {
172-
if !book_group.borrow().contains(&book) {
171+
if !book_group.borrow().contains(book) {
173172
book_group.borrow_mut().insert(*book);
174173
continue 'nextbook;
175174
}

exercises/practice/bowling/.meta/example.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Frame {
4343
return self.bonus_score() <= 10;
4444
}
4545

46-
if let Some(first) = self.bonus.get(0) {
46+
if let Some(first) = self.bonus.first() {
4747
if *first == 10 {
4848
self.bonus_score() <= 20
4949
} else {
@@ -139,3 +139,9 @@ impl BowlingGame {
139139
self.frames.len() == 10 && self.frames.iter().all(|f| f.is_complete())
140140
}
141141
}
142+
143+
impl Default for BowlingGame {
144+
fn default() -> Self {
145+
Self::new()
146+
}
147+
}

exercises/practice/custom-set/.meta/example.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ pub struct CustomSet<T> {
55

66
impl<T: Ord + Clone> PartialEq for CustomSet<T> {
77
fn eq(&self, other: &Self) -> bool {
8-
self.collection.iter().all(|x| other.contains(&x))
9-
&& other.collection.iter().all(|x| self.contains(&x))
8+
self.collection.iter().all(|x| other.contains(x))
9+
&& other.collection.iter().all(|x| self.contains(x))
1010
}
1111
}
1212

exercises/practice/decimal/.meta/Cargo-example.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ name = "decimal"
44
version = "0.1.0"
55

66
[dependencies]
7-
num-bigint = "0.1.40"
8-
num-traits = "0.1.40"
7+
num-bigint = "0.4.4"
8+
num-traits = "0.2.16"

exercises/practice/decimal/.meta/example.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Decimal {
6363
fn equalize_precision(one: &mut Decimal, two: &mut Decimal) {
6464
fn expand(lower_precision: &mut Decimal, higher_precision: &Decimal) {
6565
let precision_difference =
66-
(higher_precision.decimal_index - lower_precision.decimal_index) as usize;
66+
higher_precision.decimal_index - lower_precision.decimal_index;
6767

6868
lower_precision.digits =
6969
&lower_precision.digits * pow(BigInt::from(10_usize), precision_difference);
@@ -102,7 +102,9 @@ macro_rules! auto_impl_decimal_ops {
102102
fn $func_name(mut self, mut rhs: Self) -> Self {
103103
Decimal::equalize_precision(&mut self, &mut rhs);
104104
Decimal::new(
105+
#[allow(clippy::redundant_closure_call)]
105106
$digits_operation(self.digits, rhs.digits),
107+
#[allow(clippy::redundant_closure_call)]
106108
$index_operation(self.decimal_index, rhs.decimal_index),
107109
)
108110
}
@@ -125,6 +127,7 @@ macro_rules! auto_impl_decimal_cow {
125127
impl $trait for Decimal {
126128
fn $func_name(&self, other: &Self) -> $return_type {
127129
if self.decimal_index == other.decimal_index {
130+
#[allow(clippy::redundant_closure_call)]
128131
$digits_operation(&self.digits, &other.digits)
129132
} else {
130133
// if we're here, the decimal indexes are unmatched.

0 commit comments

Comments
 (0)