@@ -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
1111const RUSTC_PATH_SECTION : & str = "[target.'cfg(NOT_A_PLATFORM)'.dependencies]" ;
1212const 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
8585fn 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
181175pub 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}
0 commit comments