Skip to content

Commit

Permalink
Merge pull request exercism#1623 from remlse/clippy-warnings
Browse files Browse the repository at this point in the history
fix new clippy and shellcheck errors
  • Loading branch information
petertseng authored Feb 7, 2023
2 parents aa6b295 + e413f3c commit a43985e
Show file tree
Hide file tree
Showing 119 changed files with 198 additions and 348 deletions.
6 changes: 6 additions & 0 deletions _test/count_ignores.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ for e in "$repo"/exercises/*/*; do
done
want_ignores=$((total_tests - 1))
if [ "$total_ignores" != "$want_ignores" ]; then
# ShellCheck wants us to use printf,
# but there are no other uses of printf in this repo,
# so printf hasn't been tested to work yet.
# (We would not be opposed to using printf and removing this disable;
# we just haven't tested it to confirm it works yet).
# shellcheck disable=SC2028
echo "\033[1;31m$e: Has $total_tests tests and $total_ignores ignores (should be $want_ignores)\033[0m"
exitcode=1
fi
Expand Down
2 changes: 1 addition & 1 deletion bin/test-exercise
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ done
# run tests from within exercise directory
# (use subshell so we auto-reset to current pwd after)
(
cd $exercise
cd "$exercise"
if [ -n "$CLIPPY" ]; then
# Consider any Clippy to be an error in tests only.
# For now, not the example solutions since they have many Clippy warnings,
Expand Down
6 changes: 3 additions & 3 deletions concepts/destructuring/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct ArabianNights {
let teller = ArabianNights { name: "Scheherazade".into(), stories: 1001 };
{
let ArabianNights { name, stories } = teller;
println!("{} told {} stories", name, stories);
println!("{name} told {stories} stories");
}
```

Expand All @@ -42,7 +42,7 @@ The `..` operator can be used to ignore some fields when destructuring:

```rust
let ArabianNights { name, .. } = teller;
println!("{} survived by her wits", name);
println!("{name} survived by her wits");
```

## Conditional Destructuring
Expand All @@ -51,6 +51,6 @@ Destructuring structs and tuples is infallible. However, enums can also be destr

```rust
if let Some(foo) = foo {
println!("{:?}", foo);
println!("{foo:?}");
}
```
4 changes: 2 additions & 2 deletions concepts/fold/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Once the iterator drains, the final value of the accumulator is returned.
```rust
pub fn main() {
let even_sum = (1..=10).fold(0, |acc, num| if num % 2 == 0 { acc + num } else { acc });
println!("{:?}", even_sum);
println!("{even_sum:?}");
}
```

Expand All @@ -39,7 +39,7 @@ pub fn giant_grunts(initial: char) -> String {

pub fn main() {
let song = giant_grunts('F');
println!("{:?}", song);
println!("{song:?}");
}
```

Expand Down
2 changes: 1 addition & 1 deletion concepts/functions/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ In the following example the function takes in one parameter of type `i32`, bind

```rust
fn print_integer(value: i32) {
println!("{:?}", value);
println!("{value:?}");
}
```

Expand Down
4 changes: 2 additions & 2 deletions concepts/hashmap/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Other than the indexing operator, there are two other ways of accessing values s

```rust
if let Some(blue_score) = scores.get("Blue") {
println!("Blue scored: {} \n", blue_score);
println!("Blue scored: {blue_score} \n");
}
```

Expand All @@ -41,7 +41,7 @@ for vote in votes {
*count += 1;
}

println!("{:#?}", vote_counter);
println!("{vote_counter:#?}");
```

This API makes certain common access patterns very convenient; it has an entire concept (Entry API) dedicated to it.
Expand Down
4 changes: 2 additions & 2 deletions concepts/iterators/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ assert_eq!(None, iter.next());

// Loop over iterators with built-in methods, such as `map` and `collect`
let w: HashMap<_, _> = v.into_iter().map(|x| (x, x + 1)).collect();
println!("{:?}", w);
println!("{w:?}");
// => {1: 2, 0: 1, 2: 3}
```
```
4 changes: 2 additions & 2 deletions concepts/loops/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() {
let a = [10, 20, 30, 40, 50];

for x in a {
println!("the value is: {}", x);
println!("the value is: {x}");
}
}
```
Expand All @@ -46,7 +46,7 @@ Ranges are iterators too, which makes iterating through them with the `for` loop
```rust
fn main() {
for x in 0..10 {
println!("the value is: {}", x);
println!("the value is: {x}");
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions concepts/match-basics/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn ordinal(n: u32) -> String {
3 => "rd",
_ => "th",
};
format!("{}{}", n, ordinal)
format!("{n}{ordinal}")
}
```

Expand Down Expand Up @@ -105,7 +105,7 @@ impl<E: std::fmt::Display> OrLog for Result<(), E> {
// discard Ok results
Ok(()) => {},
// log errors
Err(err) => println!("ERROR: {}", err),
Err(err) => println!("ERROR: {err}"),
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions concepts/references/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ For example:
fn log(msg: String) { // msg takes ownership of the value passed to it
let formatted_datetime = String::new();
// code to format current datetime snipped
println!("{} at {}", msg, formatted_datetime);
println!("{msg} at {formatted_datetime}");
} // msg is dropped here

pub fn main() {
let my_string = "Something happened".to_string();
log(my_string); // passing my_string transfers ownership of its value to msg
//println!("{:?}", my_string); // uncommenting this line will error because my_string's value has been dropped while owned by msg
//println!("{my_string:?}"); // uncommenting this line will error because my_string's value has been dropped while owned by msg
}
```

Expand All @@ -30,13 +30,13 @@ for the duration of that function.
fn log(msg: &String) { //msg is defined as a reference with an ampersand
let formatted_datetime: String;
// code to format current datetime snipped
println!("{} at {}", msg, formatted_datetime);
println!("{msg} at {formatted_datetime}");
}

pub fn main() {
let my_string = "Something happened".to_string();
log(&my_string); // my_string is passed as a reference with an ampersand
println!("{:?}", my_string);
println!("{my_string:?}");
}
```

Expand Down Expand Up @@ -119,9 +119,9 @@ fn add_five(counter: &mut i32) { //counter is defined as a mutable reference

pub fn main() {
let mut my_count = 0; // my_count must be defined as mutable
println!("{:?}", my_count);
println!("{my_count:?}");
add_five(&mut my_count); // my_count is passed as a mutable reference
println!("{:?}", my_count);
println!("{my_count:?}");
}
```

Expand All @@ -138,11 +138,11 @@ fn add_five(counter: &mut i32) {

pub fn main() {
let mut my_count = 0;
println!("{:?}", my_count);
println!("{my_count:?}");
let mut my_count2 = &mut my_count; // first mutable reference to my_count
add_five(&mut my_count); // this errors because my_count's mutable borrow by my_count2 will be used on the next line
add_five(my_count2);
println!("{:?}", my_count);
println!("{my_count:?}");
}
```

Expand All @@ -155,11 +155,11 @@ fn add_five(counter: &mut i32) {

pub fn main() {
let mut my_count = 0;
println!("{:?}", my_count);
println!("{my_count:?}");
let mut my_count2 = &mut my_count; // first mutable reference to my_count
add_five(my_count2); // my_count2's borrow of my_count is not used afer this point
add_five(&mut my_count); // compiler allows second mutable borrow of my_count
println!("{:?}", my_count); // because it does not conflict with first mutable borrow
println!("{my_count:?}"); // because it does not conflict with first mutable borrow
}
```

Expand Down
4 changes: 2 additions & 2 deletions concepts/string-slices/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn main() {
}

fn say_hi(phrase: &String) {
println!("{:?}", phrase);
println!("{phrase:?}");
}

// mismatched types
Expand All @@ -58,7 +58,7 @@ pub fn main() {
}

fn say_hi(phrase: &str) {
println!("{:?}", phrase);
println!("{phrase:?}");
}
```

Expand Down
2 changes: 1 addition & 1 deletion concepts/string-vs-str/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ println!("n_chars = {}", n_chars); // 25
println!("example.len() = {}", example.len()); // 27

println!("final char: {}", example.chars().nth(n_chars-1).unwrap()); //
println!("final byte: {}", final_byte); // 164
println!("final byte: {final_byte}"); // 164
println!("char of final byte: {}", final_byte as char); // ¤
```

Expand Down
8 changes: 4 additions & 4 deletions concepts/strings/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A String can be created using the `String::new()` or `String::from()` associated
```rust
let my_string = String::from("The answer is");
let answer = 42.to_string();
println!("{} {}", my_string, answer);
println!("{my_string} {answer}");
```

### The `format!` Macro
Expand Down Expand Up @@ -45,7 +45,7 @@ fn main() {
let s3 = String::from("toe");

let s = s1 + "-" + &s2 + "-" + &s3;
println!("{}", s);
println!("{s}");
}
```

Expand All @@ -56,10 +56,10 @@ Since Rust strings are UTF-8 encoded individual characters are not all of the sa
```rust
let alphabet: String = ('a'..='z').collect();
for c in alphabet.chars() {
println!("{}", c);
println!("{c}");
}
let j = alphabet.chars().nth(9);
println!("{:?}", j); // prints Some('j')
println!("{j:?}"); // prints Some('j')
```

## More
Expand Down
8 changes: 2 additions & 6 deletions exercises/concept/assembly-line/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// This stub file contains items that aren't used yet; feel free to remove this module attribute
// to enable stricter warnings.
#![allow(unused)]

pub fn production_rate_per_hour(speed: u8) -> f64 {
unimplemented!("calculate hourly production rate at speed: {}", speed)
unimplemented!("calculate hourly production rate at speed: {speed}")
}

pub fn working_items_per_minute(speed: u8) -> u32 {
unimplemented!("calculate the amount of working items at speed: {}", speed)
unimplemented!("calculate the amount of working items at speed: {speed}")
}
5 changes: 1 addition & 4 deletions exercises/concept/csv-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ impl CsvRecordBuilder {

/// Adds an item to the list separated by a space and a comma.
pub fn add(&mut self, val: &str) {
unimplemented!(
"implement the `CsvRecordBuilder::add` method, adding {}",
val
)
unimplemented!("implement the `CsvRecordBuilder::add` method, adding {val}")
}

/// Consumes the builder and returns the comma separated list
Expand Down
16 changes: 3 additions & 13 deletions exercises/concept/lucians-luscious-lasagna/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
// This stub file contains items that aren't used yet; feel free to remove this module attribute
// to enable stricter warnings.
#![allow(unused)]

pub fn expected_minutes_in_oven() -> i32 {
unimplemented!("return expected minutes in the oven")
}

pub fn remaining_minutes_in_oven(actual_minutes_in_oven: i32) -> i32 {
unimplemented!(
"calculate remaining minutes in oven given actual minutes in oven: {}",
actual_minutes_in_oven
"calculate remaining minutes in oven given actual minutes in oven: {actual_minutes_in_oven}"
)
}

pub fn preparation_time_in_minutes(number_of_layers: i32) -> i32 {
unimplemented!(
"calculate preparation time in minutes for number of layers: {}",
number_of_layers
)
unimplemented!("calculate preparation time in minutes for number of layers: {number_of_layers}")
}

pub fn elapsed_time_in_minutes(number_of_layers: i32, actual_minutes_in_oven: i32) -> i32 {
unimplemented!(
"calculate elapsed time in minutes for number of layers {} and actual minutes in oven {}",
number_of_layers,
actual_minutes_in_oven
"calculate elapsed time in minutes for number of layers {number_of_layers} and actual minutes in oven {actual_minutes_in_oven}"
)
}
2 changes: 1 addition & 1 deletion exercises/concept/resistor-color/.meta/exemplar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn color_to_value(color: ResistorColor) -> usize {

pub fn value_to_color_string(value: usize) -> String {
ResistorColor::from_int(value)
.map(|color| format!("{:?}", color))
.map(|color| format!("{color:?}"))
.unwrap_or_else(|_| "value out of range".into())
}

Expand Down
9 changes: 3 additions & 6 deletions exercises/concept/resistor-color/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ pub enum ResistorColor {
Yellow,
}

pub fn color_to_value(_color: ResistorColor) -> u32 {
unimplemented!("convert a color into a numerical representation")
pub fn color_to_value(color: ResistorColor) -> u32 {
unimplemented!("convert color {color:?} into a numerical representation")
}

pub fn value_to_color_string(value: u32) -> String {
unimplemented!(
"convert the value {} into a string representation of color",
value
)
unimplemented!("convert the value {value} into a string representation of color")
}

pub fn colors() -> Vec<ResistorColor> {
Expand Down
6 changes: 1 addition & 5 deletions exercises/concept/role-playing-game/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// This stub file contains items that aren't used yet; feel free to remove this module attribute
// to enable stricter warnings.
#![allow(unused)]

pub struct Player {
pub health: u32,
pub mana: Option<u32>,
Expand All @@ -14,6 +10,6 @@ impl Player {
}

pub fn cast_spell(&mut self, mana_cost: u32) -> u32 {
unimplemented!("Cast a spell of cost {}", mana_cost)
unimplemented!("Cast a spell of cost {mana_cost}")
}
}
5 changes: 2 additions & 3 deletions exercises/concept/rpn-calculator/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ pub enum CalculatorInput {

pub fn evaluate(inputs: &[CalculatorInput]) -> Option<i32> {
unimplemented!(
"Given the inputs: {:?}, evaluate them as though they were a Reverse Polish notation expression",
inputs,
);
"Given the inputs: {inputs:?}, evaluate them as though they were a Reverse Polish notation expression"
);
}
```
5 changes: 2 additions & 3 deletions exercises/concept/rpn-calculator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub enum CalculatorInput {

pub fn evaluate(inputs: &[CalculatorInput]) -> Option<i32> {
unimplemented!(
"Given the inputs: {:?}, evaluate them as though they were a Reverse Polish notation expression",
inputs,
);
"Given the inputs: {inputs:?}, evaluate them as though they were a Reverse Polish notation expression"
);
}
Loading

0 comments on commit a43985e

Please sign in to comment.