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 } ;
44
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" ;
139
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 ( ) ;
1412 if is_file {
1513 run_exit_on_err (
1614 "cargo run" ,
@@ -25,10 +23,17 @@ pub fn run<'a>(path: &str, edition: &str, args: impl Iterator<Item = &'a String>
2523 . env ( "RUSTC_ICE" , "0" ) ,
2624 ) ;
2725 } 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`.
2830 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 ) ;
2934 run_exit_on_err (
3035 "cargo clippy" ,
31- Command :: new ( cargo_clippy_path ( ) )
36+ Command :: new ( exe )
3237 . arg ( "clippy" )
3338 . args ( args)
3439 // Prevent rustc from creating `rustc-ice-*` files the console output is enough.
0 commit comments