@@ -8,6 +8,9 @@ use std::path::{Path, PathBuf};
8
8
// This allows rust analyzer to analyze rustc internals and show proper information inside clippy
9
9
// code. See https://github.com/rust-analyzer/rust-analyzer/issues/3517 and https://github.com/rust-lang/rust-clippy/issues/5514 for details
10
10
11
+ const RUSTC_PATH_SECTION : & str = "[target.'cfg(NOT_A_PLATFORM)'.dependencies]" ;
12
+ const DEPENDENCIES_SECTION : & str = "[dependencies]" ;
13
+
11
14
const CLIPPY_PROJECTS : & [ ClippyProjectInfo ] = & [
12
15
ClippyProjectInfo :: new ( "root" , "Cargo.toml" , "src/driver.rs" ) ,
13
16
ClippyProjectInfo :: new ( "clippy_lints" , "clippy_lints/Cargo.toml" , "clippy_lints/src/lib.rs" ) ,
@@ -43,6 +46,8 @@ pub fn setup_rustc_src(rustc_path: &str) {
43
46
return ;
44
47
}
45
48
}
49
+
50
+ println ! ( "info: the source paths can be removed again with `cargo dev remove intellij`" ) ;
46
51
}
47
52
48
53
fn check_and_get_rustc_dir ( rustc_path : & str ) -> Result < PathBuf , ( ) > {
@@ -51,26 +56,26 @@ fn check_and_get_rustc_dir(rustc_path: &str) -> Result<PathBuf, ()> {
51
56
if path. is_relative ( ) {
52
57
match path. canonicalize ( ) {
53
58
Ok ( absolute_path) => {
54
- println ! ( "note : the rustc path was resolved to: `{}`" , absolute_path. display( ) ) ;
59
+ println ! ( "info : the rustc path was resolved to: `{}`" , absolute_path. display( ) ) ;
55
60
path = absolute_path;
56
61
} ,
57
62
Err ( err) => {
58
- println ! ( "error: unable to get the absolute path of rustc ({})" , err) ;
63
+ eprintln ! ( "error: unable to get the absolute path of rustc ({})" , err) ;
59
64
return Err ( ( ) ) ;
60
65
} ,
61
66
} ;
62
67
}
63
68
64
69
let path = path. join ( "compiler" ) ;
65
- println ! ( "note : looking for compiler sources at: {}" , path. display( ) ) ;
70
+ println ! ( "info : looking for compiler sources at: {}" , path. display( ) ) ;
66
71
67
72
if !path. exists ( ) {
68
- println ! ( "error: the given path does not exist" ) ;
73
+ eprintln ! ( "error: the given path does not exist" ) ;
69
74
return Err ( ( ) ) ;
70
75
}
71
76
72
77
if !path. is_dir ( ) {
73
- println ! ( "error: the given path is a file and not a directory" ) ;
78
+ eprintln ! ( "error: the given path is a file and not a directory" ) ;
74
79
return Err ( ( ) ) ;
75
80
}
76
81
@@ -82,7 +87,7 @@ fn inject_deps_into_project(rustc_source_dir: &Path, project: &ClippyProjectInfo
82
87
let lib_content = read_project_file ( project. lib_rs_file , "lib.rs" , project. name ) ?;
83
88
84
89
if inject_deps_into_manifest ( rustc_source_dir, project. cargo_file , & cargo_content, & lib_content) . is_err ( ) {
85
- println ! (
90
+ eprintln ! (
86
91
"error: unable to inject dependencies into {} with the Cargo file {}" ,
87
92
project. name, project. cargo_file
88
93
) ;
@@ -98,7 +103,7 @@ fn inject_deps_into_project(rustc_source_dir: &Path, project: &ClippyProjectInfo
98
103
fn read_project_file ( file_path : & str , file_name : & str , project : & str ) -> Result < String , ( ) > {
99
104
let path = Path :: new ( file_path) ;
100
105
if !path. exists ( ) {
101
- println ! (
106
+ eprintln ! (
102
107
"error: unable to find the `{}` file for the project {}" ,
103
108
file_name, project
104
109
) ;
@@ -123,19 +128,19 @@ fn inject_deps_into_manifest(
123
128
cargo_toml : & str ,
124
129
lib_rs : & str ,
125
130
) -> std:: io:: Result < ( ) > {
126
- // do not inject deps if we have aleady done so
127
- if cargo_toml. contains ( "[target.'cfg(NOT_A_PLATFORM)'.dependencies]" ) {
131
+ // do not inject deps if we have already done so
132
+ if cargo_toml. contains ( RUSTC_PATH_SECTION ) {
128
133
eprintln ! (
129
- "warn: dependencies are already setup inside {}, skipping file. " ,
134
+ "warn: dependencies are already setup inside {}, skipping file" ,
130
135
manifest_path
131
136
) ;
132
137
return Ok ( ( ) ) ;
133
138
}
134
139
135
140
let extern_crates = lib_rs
136
141
. lines ( )
137
- // get the deps
138
- . filter ( |line| line. starts_with ( "extern crate" ) )
142
+ // only take dependencies starting with `rustc_`
143
+ . filter ( |line| line. starts_with ( "extern crate rustc_ " ) )
139
144
// we have something like "extern crate foo;", we only care about the "foo"
140
145
// ↓ ↓
141
146
// extern crate rustc_middle;
@@ -168,7 +173,56 @@ fn inject_deps_into_manifest(
168
173
let mut file = File :: create ( manifest_path) ?;
169
174
file. write_all ( new_manifest. as_bytes ( ) ) ?;
170
175
171
- println ! ( "note : successfully setup dependencies inside {}" , manifest_path) ;
176
+ println ! ( "info : successfully setup dependencies inside {}" , manifest_path) ;
172
177
173
178
Ok ( ( ) )
174
179
}
180
+
181
+ pub fn remove_rustc_src ( ) {
182
+ 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) ;
187
+ }
188
+ }
189
+
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 ) ?;
192
+ let section_start = if let Some ( section_start) = cargo_content. find ( RUSTC_PATH_SECTION ) {
193
+ section_start
194
+ } else {
195
+ println ! (
196
+ "info: dependencies could not be found in `{}` for {}, skipping file" ,
197
+ project. cargo_file, project. name
198
+ ) ;
199
+ return Ok ( ( ) ) ;
200
+ } ;
201
+
202
+ let end_point = if let Some ( end_point) = cargo_content. find ( DEPENDENCIES_SECTION ) {
203
+ end_point
204
+ } else {
205
+ eprintln ! (
206
+ "error: the end of the rustc dependencies section could not be found in `{}`" ,
207
+ project. cargo_file
208
+ ) ;
209
+ return Err ( ( ) ) ;
210
+ } ;
211
+
212
+ cargo_content. replace_range ( section_start..end_point, "" ) ;
213
+
214
+ match File :: create ( project. cargo_file ) {
215
+ Ok ( mut file) => {
216
+ file. write_all ( cargo_content. as_bytes ( ) ) . unwrap ( ) ;
217
+ println ! ( "info: successfully removed dependencies inside {}" , project. cargo_file) ;
218
+ Ok ( ( ) )
219
+ } ,
220
+ Err ( err) => {
221
+ eprintln ! (
222
+ "error: unable to open file `{}` to remove rustc dependencies for {} ({})" ,
223
+ project. cargo_file, project. name, err
224
+ ) ;
225
+ Err ( ( ) )
226
+ } ,
227
+ }
228
+ }
0 commit comments