11use std:: {
2- cell:: RefCell ,
3- io:: Write ,
4- rc:: Rc ,
5- sync:: { atomic:: AtomicU16 , Arc , Once , OnceLock } ,
2+ cell:: RefCell , io:: Write , path:: Path , rc:: Rc , sync:: { atomic:: AtomicU16 , Arc , Once , OnceLock }
63} ;
74
85use hemtt_common:: config:: { LintConfig , ProjectConfig , RuntimeArguments } ;
@@ -63,8 +60,10 @@ impl LintRunner<LintData> for Runner {
6360 ) -> Vec < std:: sync:: Arc < dyn Code > > {
6461 static CLEANUP_PATH : Once = Once :: new ( ) ;
6562 CLEANUP_PATH . call_once ( || {
66- let _ = std:: fs:: create_dir_all ( ".hemttout" ) ;
67- let _ = std:: fs:: remove_file ( PATH ) ;
63+ if Path :: new ( ".hemttout" ) . exists ( ) {
64+ let _ = std:: fs:: remove_file ( PATH ) ;
65+ let _ = std:: fs:: File :: create ( PATH ) ;
66+ }
6867 } ) ;
6968 let Some ( processed) = processed else {
7069 return vec ! [ ] ;
@@ -76,16 +75,20 @@ impl LintRunner<LintData> for Runner {
7675 subclasses : IndexMap :: new ( ) ,
7776 } ) ) ;
7877 check ( & target. 0 , & root) ;
79- let mut file = match std:: fs:: OpenOptions :: new ( )
80- . append ( true )
81- . create ( true )
82- . open ( PATH )
83- {
84- Ok ( file) => file,
85- Err ( e) => {
86- eprintln ! ( "Failed to open {PATH}: {e}" ) ;
87- return vec ! [ ] ;
88- }
78+ let mut file = if Path :: new ( PATH ) . exists ( ) {
79+ Some ( match std:: fs:: OpenOptions :: new ( )
80+ . append ( true )
81+ . create ( true )
82+ . open ( PATH )
83+ {
84+ Ok ( file) => file,
85+ Err ( e) => {
86+ eprintln ! ( "Failed to open {PATH}: {e}" ) ;
87+ return vec ! [ ] ;
88+ }
89+ } )
90+ } else {
91+ None
8992 } ;
9093 ClassNode :: check_unused ( & root, & mut Vec :: new ( ) , processed, config, & mut file, runtime)
9194 }
@@ -104,7 +107,7 @@ impl ClassNode {
104107 reported : & mut Vec < Class > ,
105108 processed : & Processed ,
106109 config : & LintConfig ,
107- file : & mut std:: fs:: File ,
110+ file : & mut Option < std:: fs:: File > ,
108111 runtime : & hemtt_common:: config:: RuntimeArguments ,
109112 ) -> Codes {
110113 let mut codes: Codes = Vec :: new ( ) ;
@@ -121,15 +124,17 @@ impl ClassNode {
121124 . mapping ( name. span . start )
122125 . expect ( "start position exists" )
123126 . original ( ) ;
124- writeln ! (
125- file,
126- "{} - {}:{}:{}" ,
127- name. as_str( ) ,
128- pos. path( ) . as_str( ) . trim_start_matches( '/' ) ,
129- pos. start( ) . 1 . 0 ,
130- pos. start( ) . 1 . 1 + 1 ,
131- )
132- . expect ( "Failed to write to file" ) ;
127+ if let Some ( file) = file {
128+ writeln ! (
129+ file,
130+ "{} - {}:{}:{}" ,
131+ name. as_str( ) ,
132+ pos. path( ) . as_str( ) . trim_start_matches( '/' ) ,
133+ pos. start( ) . 1 . 0 ,
134+ pos. start( ) . 1 . 1 + 1 ,
135+ )
136+ . expect ( "Failed to write to file" ) ;
137+ }
133138 }
134139 for subclass in cfg. borrow ( ) . subclasses . values ( ) {
135140 let inner_codes = Self :: check_unused ( subclass, reported, processed, config, file, runtime) ;
0 commit comments