@@ -5,8 +5,8 @@ use std::path::{Path, PathBuf};
5
5
6
6
// This module takes an absolute path to a rustc repo and alters the dependencies to point towards
7
7
// 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
10
10
11
11
const RUSTC_PATH_SECTION : & str = "[target.'cfg(NOT_A_PLATFORM)'.dependencies]" ;
12
12
const DEPENDENCIES_SECTION : & str = "[dependencies]" ;
@@ -75,16 +75,16 @@ fn check_and_get_rustc_dir(rustc_path: &str) -> Result<PathBuf, ()> {
75
75
}
76
76
77
77
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" ) ;
79
79
return Err ( ( ) ) ;
80
80
}
81
81
82
82
Ok ( path)
83
83
}
84
84
85
85
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 ) ?;
88
88
89
89
if inject_deps_into_manifest ( rustc_source_dir, project. cargo_file , & cargo_content, & lib_content) . is_err ( ) {
90
90
eprintln ! (
@@ -100,23 +100,17 @@ fn inject_deps_into_project(rustc_source_dir: &Path, project: &ClippyProjectInfo
100
100
/// `clippy_dev` expects to be executed in the root directory of Clippy. This function
101
101
/// loads the given file or returns an error. Having it in this extra function ensures
102
102
/// 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 , ( ) > {
104
104
let path = Path :: new ( file_path) ;
105
105
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) ;
110
107
return Err ( ( ) ) ;
111
108
}
112
109
113
110
match fs:: read_to_string ( path) {
114
111
Ok ( content) => Ok ( content) ,
115
112
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) ;
120
114
Err ( ( ) )
121
115
} ,
122
116
}
@@ -142,8 +136,8 @@ fn inject_deps_into_manifest(
142
136
// only take dependencies starting with `rustc_`
143
137
. filter ( |line| line. starts_with ( "extern crate rustc_" ) )
144
138
// we have something like "extern crate foo;", we only care about the "foo"
145
- // ↓ ↓
146
139
// extern crate rustc_middle;
140
+ // ^^^^^^^^^^^^
147
141
. map ( |s| & s[ 13 ..( s. len ( ) - 1 ) ] ) ;
148
142
149
143
let new_deps = extern_crates. map ( |dep| {
@@ -180,23 +174,24 @@ fn inject_deps_into_manifest(
180
174
181
175
pub fn remove_rustc_src ( ) {
182
176
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) ;
187
178
}
188
179
}
189
180
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
+ } ;
192
187
let section_start = if let Some ( section_start) = cargo_content. find ( RUSTC_PATH_SECTION ) {
193
188
section_start
194
189
} else {
195
190
println ! (
196
191
"info: dependencies could not be found in `{}` for {}, skipping file" ,
197
192
project. cargo_file, project. name
198
193
) ;
199
- return Ok ( ( ) ) ;
194
+ return true ;
200
195
} ;
201
196
202
197
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<(), ()>
206
201
"error: the end of the rustc dependencies section could not be found in `{}`" ,
207
202
project. cargo_file
208
203
) ;
209
- return Err ( ( ) ) ;
204
+ return false ;
210
205
} ;
211
206
212
207
cargo_content. replace_range ( section_start..end_point, "" ) ;
@@ -215,14 +210,14 @@ fn remove_rustc_src_from_project(project: &ClippyProjectInfo) -> Result<(), ()>
215
210
Ok ( mut file) => {
216
211
file. write_all ( cargo_content. as_bytes ( ) ) . unwrap ( ) ;
217
212
println ! ( "info: successfully removed dependencies inside {}" , project. cargo_file) ;
218
- Ok ( ( ) )
213
+ true
219
214
} ,
220
215
Err ( err) => {
221
216
eprintln ! (
222
217
"error: unable to open file `{}` to remove rustc dependencies for {} ({})" ,
223
218
project. cargo_file, project. name, err
224
219
) ;
225
- Err ( ( ) )
220
+ false
226
221
} ,
227
222
}
228
223
}
0 commit comments