1
- use crate :: utils:: { cargo_clippy_path , cargo_cmd, run_exit_on_err} ;
2
- use std:: fs ;
3
- use std:: process :: { self , Command } ;
1
+ use crate :: utils:: { ErrAction , cargo_cmd, expect_action , run_exit_on_err} ;
2
+ use std:: process :: Command ;
3
+ use std:: { env , fs } ;
4
4
5
- pub fn run < ' a > ( path : & str , edition : & str , args : impl Iterator < Item = & ' a String > ) {
6
- let is_file = match fs:: metadata ( path) {
7
- Ok ( metadata) => metadata. is_file ( ) ,
8
- Err ( e) => {
9
- eprintln ! ( "Failed to read {path}: {e:?}" ) ;
10
- process:: exit ( 1 ) ;
11
- } ,
12
- } ;
5
+ #[ cfg( not( windows) ) ]
6
+ static CARGO_CLIPPY_EXE : & str = "cargo-clippy" ;
7
+ #[ cfg( windows) ]
8
+ static CARGO_CLIPPY_EXE : & str = "cargo-clippy.exe" ;
13
9
10
+ pub fn run < ' a > ( path : & str , edition : & str , args : impl Iterator < Item = & ' a String > ) {
11
+ let is_file = expect_action ( fs:: metadata ( path) , ErrAction :: Read , path) . is_file ( ) ;
14
12
if is_file {
15
13
run_exit_on_err (
16
14
"cargo run" ,
@@ -25,10 +23,17 @@ pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>
25
23
. env ( "RUSTC_ICE" , "0" ) ,
26
24
) ;
27
25
} else {
26
+ // Ideally this would just be `cargo run`, but the working directory needs to be
27
+ // set to clippy's directory when building, and the target project's directory
28
+ // when running clippy. `cargo` can only set a single working directory for both
29
+ // when using `run`.
28
30
run_exit_on_err ( "cargo build" , cargo_cmd ( ) . arg ( "build" ) ) ;
31
+
32
+ let mut exe = env:: current_exe ( ) . expect ( "failed to get current executable name" ) ;
33
+ exe. set_file_name ( CARGO_CLIPPY_EXE ) ;
29
34
run_exit_on_err (
30
35
"cargo clippy" ,
31
- Command :: new ( cargo_clippy_path ( ) )
36
+ Command :: new ( exe )
32
37
. arg ( "clippy" )
33
38
. args ( args)
34
39
// Prevent rustc from creating `rustc-ice-*` files the console output is enough.
0 commit comments