mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 00:00:03 +03:00
Compare commits
4 commits
a2d1cb3b22
...
47976caa69
Author | SHA1 | Date | |
---|---|---|---|
47976caa69 | |||
f1abd8577c | |||
423b50b068 | |||
bedf0789f2 |
|
@ -1,5 +1,3 @@
|
||||||
#![allow(clippy::comparison_chain)]
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
enum CreationError {
|
enum CreationError {
|
||||||
Negative,
|
Negative,
|
||||||
|
|
|
@ -18,12 +18,11 @@ fn main() {
|
||||||
// Here, both answers work.
|
// Here, both answers work.
|
||||||
// `.into()` converts a type into an expected type.
|
// `.into()` converts a type into an expected type.
|
||||||
// If it is called where `String` is expected, it will convert `&str` to `String`.
|
// If it is called where `String` is expected, it will convert `&str` to `String`.
|
||||||
// But if is called where `&str` is expected, then `&str` is kept `&str` since no
|
|
||||||
// conversion is needed.
|
|
||||||
string("nice weather".into());
|
string("nice weather".into());
|
||||||
|
// But if it is called where `&str` is expected, then `&str` is kept `&str` since no conversion is needed.
|
||||||
|
// If you remove the `#[allow(…)]` line, then Clippy will tell you to remove `.into()` below since it is a useless conversion.
|
||||||
|
#[allow(clippy::useless_conversion)]
|
||||||
string_slice("nice weather".into());
|
string_slice("nice weather".into());
|
||||||
// ^^^^^^^ the compiler recommends removing the `.into()`
|
|
||||||
// call because it is a useless conversion.
|
|
||||||
|
|
||||||
string(format!("Interpolation {}", "Station"));
|
string(format!("Interpolation {}", "Station"));
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#![allow(clippy::comparison_chain)]
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
enum CreationError {
|
enum CreationError {
|
||||||
|
@ -11,12 +11,10 @@ struct PositiveNonzeroInteger(u64);
|
||||||
|
|
||||||
impl PositiveNonzeroInteger {
|
impl PositiveNonzeroInteger {
|
||||||
fn new(value: i64) -> Result<Self, CreationError> {
|
fn new(value: i64) -> Result<Self, CreationError> {
|
||||||
if value == 0 {
|
match value.cmp(&0) {
|
||||||
Err(CreationError::Zero)
|
Ordering::Less => Err(CreationError::Negative),
|
||||||
} else if value < 0 {
|
Ordering::Equal => Err(CreationError::Zero),
|
||||||
Err(CreationError::Negative)
|
Ordering::Greater => Ok(Self(value as u64)),
|
||||||
} else {
|
|
||||||
Ok(Self(value as u64))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ fn factorial_fold(num: u64) -> u64 {
|
||||||
// -> 1 * 2 is calculated, then the result 2 is multiplied by
|
// -> 1 * 2 is calculated, then the result 2 is multiplied by
|
||||||
// the second element 3 so the result 6 is returned.
|
// the second element 3 so the result 6 is returned.
|
||||||
// And so on…
|
// And so on…
|
||||||
|
#[allow(clippy::unnecessary_fold)]
|
||||||
(2..=num).fold(1, |acc, x| acc * x)
|
(2..=num).fold(1, |acc, x| acc * x)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ pub trait RunnableExercise {
|
||||||
|
|
||||||
// Compile, check and run the exercise or its solution (depending on `bin_name´).
|
// Compile, check and run the exercise or its solution (depending on `bin_name´).
|
||||||
// The output is written to the `output` buffer after clearing it.
|
// The output is written to the `output` buffer after clearing it.
|
||||||
fn run(
|
fn run<const FORCE_STRICT_CLIPPY: bool>(
|
||||||
&self,
|
&self,
|
||||||
bin_name: &str,
|
bin_name: &str,
|
||||||
mut output: Option<&mut Vec<u8>>,
|
mut output: Option<&mut Vec<u8>>,
|
||||||
|
@ -115,7 +115,7 @@ pub trait RunnableExercise {
|
||||||
let mut clippy_cmd = cmd_runner.cargo("clippy", bin_name, output.as_deref_mut());
|
let mut clippy_cmd = cmd_runner.cargo("clippy", bin_name, output.as_deref_mut());
|
||||||
|
|
||||||
// `--profile test` is required to also check code with `[cfg(test)]`.
|
// `--profile test` is required to also check code with `[cfg(test)]`.
|
||||||
if self.strict_clippy() {
|
if FORCE_STRICT_CLIPPY || self.strict_clippy() {
|
||||||
clippy_cmd.args(["--profile", "test", "--", "-D", "warnings"]);
|
clippy_cmd.args(["--profile", "test", "--", "-D", "warnings"]);
|
||||||
} else {
|
} else {
|
||||||
clippy_cmd.args(["--profile", "test"]);
|
clippy_cmd.args(["--profile", "test"]);
|
||||||
|
@ -131,7 +131,7 @@ pub trait RunnableExercise {
|
||||||
/// The output is written to the `output` buffer after clearing it.
|
/// The output is written to the `output` buffer after clearing it.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn run_exercise(&self, output: Option<&mut Vec<u8>>, cmd_runner: &CmdRunner) -> Result<bool> {
|
fn run_exercise(&self, output: Option<&mut Vec<u8>>, cmd_runner: &CmdRunner) -> Result<bool> {
|
||||||
self.run(self.name(), output, cmd_runner)
|
self.run::<false>(self.name(), output, cmd_runner)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compile, check and run the exercise's solution.
|
/// Compile, check and run the exercise's solution.
|
||||||
|
@ -142,7 +142,7 @@ pub trait RunnableExercise {
|
||||||
bin_name.push_str(name);
|
bin_name.push_str(name);
|
||||||
bin_name.push_str("_sol");
|
bin_name.push_str("_sol");
|
||||||
|
|
||||||
self.run(&bin_name, output, cmd_runner)
|
self.run::<true>(&bin_name, output, cmd_runner)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue