| 107 | |
| 108 | impl Exercise { |
| 109 | pub fn compile(&self) -> Result<CompiledExercise, ExerciseOutput> { |
| 110 | let cmd = match self.mode { |
| 111 | Mode::Compile => Command::new("rustc") |
| 112 | .args(&[self.path.to_str().unwrap(), "-o", &temp_file()]) |
| 113 | .args(RUSTC_COLOR_ARGS) |
| 114 | .output(), |
| 115 | Mode::Test => Command::new("rustc") |
| 116 | .args(&["--test", self.path.to_str().unwrap(), "-o", &temp_file()]) |
| 117 | .args(RUSTC_COLOR_ARGS) |
| 118 | .output(), |
| 119 | Mode::Clippy => { |
| 120 | let cargo_toml = format!( |
| 121 | r#"[package] |
| 122 | name = "{}" |
| 123 | version = "0.0.1" |
| 124 | edition = "2018" |
| 125 | [[bin]] |
| 126 | name = "{}" |
| 127 | path = "{}.rs""#, |
| 128 | self.name, self.name, self.name |
| 129 | ); |
| 130 | let cargo_toml_error_msg = if env::var("NO_EMOJI").is_ok() { |
| 131 | "Failed to write Clippy Cargo.toml file." |
| 132 | } else { |
| 133 | "Failed to write 📎 Clippy 📎 Cargo.toml file." |
| 134 | }; |
| 135 | fs::write(CLIPPY_CARGO_TOML_PATH, cargo_toml).expect(cargo_toml_error_msg); |
| 136 | // To support the ability to run the clippy exercises, build |
| 137 | // an executable, in addition to running clippy. With a |
| 138 | // compilation failure, this would silently fail. But we expect |
| 139 | // clippy to reflect the same failure while compiling later. |
| 140 | Command::new("rustc") |
| 141 | .args(&[self.path.to_str().unwrap(), "-o", &temp_file()]) |
| 142 | .args(RUSTC_COLOR_ARGS) |
| 143 | .output() |
| 144 | .expect("Failed to compile!"); |
| 145 | // Due to an issue with Clippy, a cargo clean is required to catch all lints. |
| 146 | // See https://github.com/rust-lang/rust-clippy/issues/2604 |
| 147 | // This is already fixed on Clippy's master branch. See this issue to track merging into Cargo: |
| 148 | // https://github.com/rust-lang/rust-clippy/issues/3837 |
| 149 | Command::new("cargo") |
| 150 | .args(&["clean", "--manifest-path", CLIPPY_CARGO_TOML_PATH]) |
| 151 | .args(RUSTC_COLOR_ARGS) |
| 152 | .output() |
| 153 | .expect("Failed to run 'cargo clean'"); |
| 154 | Command::new("cargo") |
| 155 | .args(&["clippy", "--manifest-path", CLIPPY_CARGO_TOML_PATH]) |
| 156 | .args(RUSTC_COLOR_ARGS) |
| 157 | .args(&["--", "-D", "warnings","-D","clippy::float_cmp"]) |
| 158 | .output() |
| 159 | } |
| 160 | } |
| 161 | .expect("Failed to run 'compile' command."); |
| 162 | |
| 163 | if cmd.status.success() { |
| 164 | Ok(CompiledExercise { |
| 165 | exercise: self, |
| 166 | _handle: FileHandle, |