Skip to content

Commit 8e969cd

Browse files
xFrednetflip1995
andcommitted
Updated several clippy_dev messages and types (PR suggestions)
Co-authored-by: Philipp Krones <[email protected]>
1 parent f0fa363 commit 8e969cd

File tree

3 files changed

+41
-71
lines changed

3 files changed

+41
-71
lines changed

clippy_dev/src/setup/git_hook.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const HOOK_SOURCE_FILE: &str = "util/etc/pre-commit.sh";
1010
const HOOK_TARGET_FILE: &str = ".git/hooks/pre-commit";
1111

1212
pub fn install_hook(force_override: bool) {
13-
if check_precondition(force_override).is_err() {
13+
if !check_precondition(force_override) {
1414
return;
1515
}
1616

@@ -25,52 +25,55 @@ pub fn install_hook(force_override: bool) {
2525
// include the `execute` permission.
2626
match fs::copy(HOOK_SOURCE_FILE, HOOK_TARGET_FILE) {
2727
Ok(_) => {
28-
println!("note: the hook can be removed with `cargo dev remove git-hook`");
29-
println!("Git hook successfully installed :)");
28+
println!("info: the hook can be removed with `cargo dev remove git-hook`");
29+
println!("git hook successfully installed");
3030
},
31-
Err(err) => println!(
31+
Err(err) => eprintln!(
3232
"error: unable to copy `{}` to `{}` ({})",
3333
HOOK_SOURCE_FILE, HOOK_TARGET_FILE, err
3434
),
3535
}
3636
}
3737

38-
fn check_precondition(force_override: bool) -> Result<(), ()> {
38+
fn check_precondition(force_override: bool) -> bool {
3939
// Make sure that we can find the git repository
4040
let git_path = Path::new(REPO_GIT_DIR);
4141
if !git_path.exists() || !git_path.is_dir() {
42-
println!("error: clippy_dev was unable to find the `.git` directory");
43-
return Err(());
42+
eprintln!("error: clippy_dev was unable to find the `.git` directory");
43+
return false;
4444
}
4545

4646
// Make sure that we don't override an existing hook by accident
4747
let path = Path::new(HOOK_TARGET_FILE);
4848
if path.exists() {
49-
if force_override || super::ask_yes_no_question("Do you want to override the existing pre-commit hook it?") {
49+
if force_override {
5050
return delete_git_hook_file(path);
5151
}
52-
return Err(());
52+
53+
eprintln!("error: there is already a pre-commit hook installed");
54+
println!("info: use the `--force-override` flag to override the existing hook");
55+
return false;
5356
}
5457

55-
Ok(())
58+
true
5659
}
5760

5861
pub fn remove_hook() {
5962
let path = Path::new(HOOK_TARGET_FILE);
6063
if path.exists() {
61-
if delete_git_hook_file(path).is_ok() {
62-
println!("Git hook successfully removed :)");
64+
if delete_git_hook_file(path) {
65+
println!("git hook successfully removed");
6366
}
6467
} else {
65-
println!("No pre-commit hook was found. You're good to go :)");
68+
println!("no pre-commit hook was found");
6669
}
6770
}
6871

69-
fn delete_git_hook_file(path: &Path) -> Result<(), ()> {
70-
if fs::remove_file(path).is_err() {
71-
println!("error: unable to delete existing pre-commit git hook");
72-
Err(())
72+
fn delete_git_hook_file(path: &Path) -> bool {
73+
if let Err(err) = fs::remove_file(path) {
74+
eprintln!("error: unable to delete existing pre-commit git hook ({})", err);
75+
false
7376
} else {
74-
Ok(())
77+
true
7578
}
7679
}

clippy_dev/src/setup/intellij.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::path::{Path, PathBuf};
55

66
// This module takes an absolute path to a rustc repo and alters the dependencies to point towards
77
// the respective rustc subcrates instead of using extern crate xyz.
8-
// This allows rust analyzer to analyze rustc internals and show proper information inside clippy
9-
// code. See https://github.com/rust-analyzer/rust-analyzer/issues/3517 and https://github.com/rust-lang/rust-clippy/issues/5514 for details
8+
// This allows IntelliJ to analyze rustc internals and show proper information inside Clippy
9+
// code. See https://github.com/rust-lang/rust-clippy/issues/5514 for details
1010

1111
const RUSTC_PATH_SECTION: &str = "[target.'cfg(NOT_A_PLATFORM)'.dependencies]";
1212
const DEPENDENCIES_SECTION: &str = "[dependencies]";
@@ -75,16 +75,16 @@ fn check_and_get_rustc_dir(rustc_path: &str) -> Result<PathBuf, ()> {
7575
}
7676

7777
if !path.is_dir() {
78-
eprintln!("error: the given path is a file and not a directory");
78+
eprintln!("error: the given path is not a directory");
7979
return Err(());
8080
}
8181

8282
Ok(path)
8383
}
8484

8585
fn inject_deps_into_project(rustc_source_dir: &Path, project: &ClippyProjectInfo) -> Result<(), ()> {
86-
let cargo_content = read_project_file(project.cargo_file, "Cargo.toml", project.name)?;
87-
let lib_content = read_project_file(project.lib_rs_file, "lib.rs", project.name)?;
86+
let cargo_content = read_project_file(project.cargo_file)?;
87+
let lib_content = read_project_file(project.lib_rs_file)?;
8888

8989
if inject_deps_into_manifest(rustc_source_dir, project.cargo_file, &cargo_content, &lib_content).is_err() {
9090
eprintln!(
@@ -100,23 +100,17 @@ fn inject_deps_into_project(rustc_source_dir: &Path, project: &ClippyProjectInfo
100100
/// `clippy_dev` expects to be executed in the root directory of Clippy. This function
101101
/// loads the given file or returns an error. Having it in this extra function ensures
102102
/// that the error message looks nice.
103-
fn read_project_file(file_path: &str, file_name: &str, project: &str) -> Result<String, ()> {
103+
fn read_project_file(file_path: &str) -> Result<String, ()> {
104104
let path = Path::new(file_path);
105105
if !path.exists() {
106-
eprintln!(
107-
"error: unable to find the `{}` file for the project {}",
108-
file_name, project
109-
);
106+
eprintln!("error: unable to find the file `{}`", file_path);
110107
return Err(());
111108
}
112109

113110
match fs::read_to_string(path) {
114111
Ok(content) => Ok(content),
115112
Err(err) => {
116-
println!(
117-
"error: the `{}` file for the project {} could not be read ({})",
118-
file_name, project, err
119-
);
113+
eprintln!("error: the file `{}` could not be read ({})", file_path, err);
120114
Err(())
121115
},
122116
}
@@ -142,8 +136,8 @@ fn inject_deps_into_manifest(
142136
// only take dependencies starting with `rustc_`
143137
.filter(|line| line.starts_with("extern crate rustc_"))
144138
// we have something like "extern crate foo;", we only care about the "foo"
145-
// ↓ ↓
146139
// extern crate rustc_middle;
140+
// ^^^^^^^^^^^^
147141
.map(|s| &s[13..(s.len() - 1)]);
148142

149143
let new_deps = extern_crates.map(|dep| {
@@ -180,23 +174,24 @@ fn inject_deps_into_manifest(
180174

181175
pub fn remove_rustc_src() {
182176
for project in CLIPPY_PROJECTS {
183-
// We don't care about the result here as we want to go through all
184-
// dependencies either way. Any info and error message will be issued by
185-
// the removal code itself.
186-
let _ = remove_rustc_src_from_project(project);
177+
remove_rustc_src_from_project(project);
187178
}
188179
}
189180

190-
fn remove_rustc_src_from_project(project: &ClippyProjectInfo) -> Result<(), ()> {
191-
let mut cargo_content = read_project_file(project.cargo_file, "Cargo.toml", project.name)?;
181+
fn remove_rustc_src_from_project(project: &ClippyProjectInfo) -> bool {
182+
let mut cargo_content = if let Ok(content) = read_project_file(project.cargo_file) {
183+
content
184+
} else {
185+
return false;
186+
};
192187
let section_start = if let Some(section_start) = cargo_content.find(RUSTC_PATH_SECTION) {
193188
section_start
194189
} else {
195190
println!(
196191
"info: dependencies could not be found in `{}` for {}, skipping file",
197192
project.cargo_file, project.name
198193
);
199-
return Ok(());
194+
return true;
200195
};
201196

202197
let end_point = if let Some(end_point) = cargo_content.find(DEPENDENCIES_SECTION) {
@@ -206,7 +201,7 @@ fn remove_rustc_src_from_project(project: &ClippyProjectInfo) -> Result<(), ()>
206201
"error: the end of the rustc dependencies section could not be found in `{}`",
207202
project.cargo_file
208203
);
209-
return Err(());
204+
return false;
210205
};
211206

212207
cargo_content.replace_range(section_start..end_point, "");
@@ -215,14 +210,14 @@ fn remove_rustc_src_from_project(project: &ClippyProjectInfo) -> Result<(), ()>
215210
Ok(mut file) => {
216211
file.write_all(cargo_content.as_bytes()).unwrap();
217212
println!("info: successfully removed dependencies inside {}", project.cargo_file);
218-
Ok(())
213+
true
219214
},
220215
Err(err) => {
221216
eprintln!(
222217
"error: unable to open file `{}` to remove rustc dependencies for {} ({})",
223218
project.cargo_file, project.name, err
224219
);
225-
Err(())
220+
false
226221
},
227222
}
228223
}

clippy_dev/src/setup/mod.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,2 @@
1-
use std::io::{self, Write};
21
pub mod git_hook;
32
pub mod intellij;
4-
5-
/// This function will asked the user the given question and wait for user input
6-
/// either `true` for yes and `false` for no.
7-
fn ask_yes_no_question(question: &str) -> bool {
8-
// This code was proudly stolen from rusts bootstrapping tool.
9-
10-
fn ask_with_result(question: &str) -> io::Result<bool> {
11-
let mut input = String::new();
12-
Ok(loop {
13-
print!("{}: [y/N] ", question);
14-
io::stdout().flush()?;
15-
input.clear();
16-
io::stdin().read_line(&mut input)?;
17-
break match input.trim().to_lowercase().as_str() {
18-
"y" | "yes" => true,
19-
"n" | "no" | "" => false,
20-
_ => {
21-
println!("error: unrecognized option '{}'", input.trim());
22-
println!("note: press Ctrl+C to exit");
23-
continue;
24-
},
25-
};
26-
})
27-
}
28-
29-
ask_with_result(question).unwrap_or_default()
30-
}

0 commit comments

Comments
 (0)