summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMario Reder <mreder1289@gmail.com>2020-02-14 15:25:03 +0100
committerMario Reder <mreder1289@gmail.com>2020-02-26 14:07:07 +0100
commit1e2fd9c92f8cd6e389525ca1a999fca4c90b5921 (patch)
treeaace341dbef4b15e37ddf260d6be857a11cca104 /src
parent7e8530b21ff355c9b52e07eb3ba25278746a6932 (diff)
feat: Add clippy lints
- adds a new 'clippy' category for exercises - clippy exercises should throw no warnings - install script now also installs clippy is related to https://github.com/rust-lang/rust-clippy/issues/2604
Diffstat (limited to 'src')
-rw-r--r--src/exercise.rs32
-rw-r--r--src/run.rs1
-rw-r--r--src/verify.rs2
3 files changed, 34 insertions, 1 deletions
diff --git a/src/exercise.rs b/src/exercise.rs
index d72eeb5..30b1864 100644
--- a/src/exercise.rs
+++ b/src/exercise.rs
@@ -1,7 +1,7 @@
use regex::Regex;
use serde::Deserialize;
use std::fmt::{self, Display, Formatter};
-use std::fs::{remove_file, File};
+use std::fs::{self, remove_file, File};
use std::io::Read;
use std::path::PathBuf;
use std::process::{self, Command};
@@ -9,6 +9,7 @@ use std::process::{self, Command};
const RUSTC_COLOR_ARGS: &[&str] = &["--color", "always"];
const I_AM_DONE_REGEX: &str = r"(?m)^\s*///?\s*I\s+AM\s+NOT\s+DONE";
const CONTEXT: usize = 2;
+const CLIPPY_CARGO_TOML_PATH: &str = "./exercises/clippy/Cargo.toml";
fn temp_file() -> String {
format!("./temp_{}", process::id())
@@ -19,6 +20,7 @@ fn temp_file() -> String {
pub enum Mode {
Compile,
Test,
+ Clippy,
}
#[derive(Deserialize)]
@@ -83,6 +85,34 @@ impl Exercise {
.args(&["--test", self.path.to_str().unwrap(), "-o", &temp_file()])
.args(RUSTC_COLOR_ARGS)
.output(),
+ Mode::Clippy => {
+ let cargo_toml = format!(
+ r#"[package]
+name = "{}"
+version = "0.0.1"
+edition = "2018"
+[[bin]]
+name = "{}"
+path = "{}.rs""#,
+ self.name, self.name, self.name
+ );
+ fs::write(CLIPPY_CARGO_TOML_PATH, cargo_toml)
+ .expect("Failed to write 📎 Clippy 📎 Cargo.toml file.");
+ // Due to an issue with Clippy, a cargo clean is required to catch all lints.
+ // See https://github.com/rust-lang/rust-clippy/issues/2604
+ // This is already fixed on master branch. See this issue to track merging into Cargo:
+ // https://github.com/rust-lang/rust-clippy/issues/3837
+ Command::new("cargo")
+ .args(&["clean", "--manifest-path", CLIPPY_CARGO_TOML_PATH])
+ .args(RUSTC_COLOR_ARGS)
+ .output()
+ .expect("Failed to run 'cargo clean'");
+ Command::new("cargo")
+ .args(&["clippy", "--manifest-path", CLIPPY_CARGO_TOML_PATH])
+ .args(RUSTC_COLOR_ARGS)
+ .args(&["--", "-D", "warnings"])
+ .output()
+ }
}
.expect("Failed to run 'compile' command.");
diff --git a/src/run.rs b/src/run.rs
index cfde7ab..ebb0ae6 100644
--- a/src/run.rs
+++ b/src/run.rs
@@ -6,6 +6,7 @@ pub fn run(exercise: &Exercise) -> Result<(), ()> {
match exercise.mode {
Mode::Test => test(exercise)?,
Mode::Compile => compile_and_run(exercise)?,
+ Mode::Clippy => compile_and_run(exercise)?,
}
Ok(())
}
diff --git a/src/verify.rs b/src/verify.rs
index 3d14896..229aa6d 100644
--- a/src/verify.rs
+++ b/src/verify.rs
@@ -7,6 +7,7 @@ pub fn verify<'a>(start_at: impl IntoIterator<Item = &'a Exercise>) -> Result<()
let compile_result = match exercise.mode {
Mode::Test => compile_and_test(&exercise, RunMode::Interactive),
Mode::Compile => compile_only(&exercise),
+ Mode::Clippy => compile_only(&exercise),
};
if !compile_result.unwrap_or(false) {
return Err(exercise);
@@ -99,6 +100,7 @@ fn prompt_for_completion(exercise: &Exercise) -> bool {
let success_msg = match exercise.mode {
Mode::Compile => "The code is compiling!",
Mode::Test => "The code is compiling, and the tests pass!",
+ Mode::Clippy => "The code is compiling, and 📎 Clippy 📎 is happy!",
};
println!("");