@@ -22,19 +22,29 @@ fn markup(source: &[u8], map: Vec<Ran>) -> Vec<u8> {
22
22
for m in & map {
23
23
// deal with the element
24
24
if m. start <= i && i < m. end && i == m. start {
25
- output. extend ( format ! ( "/*@start: {}*/" , m. name) . as_bytes ( ) ) ;
25
+ output. extend ( format ! ( "/*{}*/" , m. name) . as_bytes ( ) ) ;
26
26
}
27
27
if m. end == i {
28
- match m. suggestion . as_str ( ) {
29
- "None" => output. extend ( format ! ( "/*@end: {}*/" , m. name) . as_bytes ( ) ) ,
30
- _ => output. extend (
31
- format ! (
32
- "/*@end: {} suggestion: {} note: {}*/" ,
33
- m. name, m. suggestion, m. note
34
- )
35
- . as_bytes ( ) ,
36
- ) ,
37
- }
28
+ output. extend (
29
+ format ! (
30
+ "/*\n {}{}{}*/" ,
31
+ m. name,
32
+ if m. suggestion == "None" {
33
+ "" . to_string( )
34
+ } else {
35
+ format!(
36
+ "\n suggestion: {}" ,
37
+ m. suggestion. replace( "\\ n" , "\n " ) . replace( '\"' , "" )
38
+ )
39
+ } ,
40
+ if m. note == "None" {
41
+ "" . to_string( )
42
+ } else {
43
+ format!( "\n note: {}" , m. note. replace( "\\ n" , "\n " ) . replace( '\"' , "" ) )
44
+ }
45
+ )
46
+ . as_bytes ( ) ,
47
+ )
38
48
}
39
49
}
40
50
output. push ( * c) ;
@@ -45,121 +55,119 @@ fn markup(source: &[u8], map: Vec<Ran>) -> Vec<u8> {
45
55
// Run cargo clippy to generate warnings from "foo.rs" into temporary "foo.rs.1" files
46
56
fn main ( ) {
47
57
remove_previously_generated_files ( ) ;
48
- let mut command = Command :: new ( "cargo" )
49
- . args ( [ "clippy" , "--message-format=json" ] )
58
+ let args = vec ! [ "clippy" , "--message-format=json" ] ;
59
+ if std:: env:: args ( ) . len ( ) > 1 && args[ 1 ] == "--fix" {
60
+ vec ! [ "clippy" , "--message-format=json" , "--fix" , "--allow-dirty" , "--broken-code" ] ;
61
+ }
62
+ if let Ok ( mut command) = Command :: new ( "cargo" )
63
+ . args ( args)
50
64
. stdout ( Stdio :: piped ( ) )
51
65
. spawn ( )
52
- . unwrap ( ) ;
53
- // if let Some(reader)
54
- let reader = std:: io:: BufReader :: new ( command. stdout . take ( ) . unwrap ( ) ) ;
55
-
56
- let mut map = HashMap :: new ( ) ;
57
- for message in cargo_metadata:: Message :: parse_stream ( reader) {
58
- if let Message :: CompilerMessage ( msg) = message. expect ( "something wrong with the message" ) {
59
- // dbg!(&msg);
60
- for s in msg. message . spans {
61
- let x = s. byte_start as usize ;
62
- let y = s. byte_end as usize ;
63
- let r = Ran {
64
- // name: msg.message.message.clone(),
65
- name : format ! (
66
- "#[{:?}({})" ,
67
- msg. message. level,
68
- msg. message
69
- . code
70
- . clone( )
71
- . expect( "something wrong with the message code" )
72
- . code
73
- ) ,
74
- start : x,
75
- end : y,
76
- suggestion : format ! ( "{:?}" , s. suggested_replacement) ,
77
- note : format ! ( "{:?}" , sub_messages( & msg. message. children) ) ,
78
- } ;
79
- let filename = s. file_name ;
80
- if !map. contains_key ( & filename) {
81
- let v = vec ! [ r] ;
82
- map. insert ( filename, v) ;
83
- } else {
84
- if let Some ( v) = map. get_mut ( & filename) {
85
- v. push ( r) ;
86
- } else {
87
- println ! ( "something wrong with the filename" ) ;
66
+ {
67
+ if let Some ( take) = command. stdout . take ( ) {
68
+ let reader = std:: io:: BufReader :: new ( take) ;
69
+ let mut map: HashMap < String , Vec < Ran > > = HashMap :: new ( ) ;
70
+ for message in cargo_metadata:: Message :: parse_stream ( reader) . flatten ( ) {
71
+ if let Message :: CompilerMessage ( msg) = message {
72
+ for s in msg. message . spans {
73
+ if let Ok ( x) = usize:: try_from ( s. byte_start ) {
74
+ if let Ok ( y) = usize:: try_from ( s. byte_end ) {
75
+ if let Some ( message_code) = & msg. message . code {
76
+ let r = Ran {
77
+ name : format ! (
78
+ "#[{:?}({})" ,
79
+ msg. message. level,
80
+ message_code. clone( ) . code
81
+ ) ,
82
+ start : x,
83
+ end : y,
84
+ suggestion : format ! ( "{:?}" , s. suggested_replacement) ,
85
+ note : format ! ( "{:?}" , sub_messages( & msg. message. children) ) ,
86
+ } ;
87
+ dbg ! ( & r) ;
88
+ let filename = s. file_name ;
89
+ match map. get_mut ( & filename) {
90
+ Some ( v) => v. push ( r) ,
91
+ None => {
92
+ let v = vec ! [ r] ;
93
+ map. insert ( filename, v) ;
94
+ }
95
+ }
96
+ }
97
+ }
98
+ }
88
99
}
89
100
}
90
101
}
102
+ for file in map. keys ( ) {
103
+ if let Ok ( source) = read_to_string ( file) {
104
+ if let Some ( v) = map. get ( file) {
105
+ let output = markup ( source. as_bytes ( ) , v. to_vec ( ) ) ;
106
+ let path = PathBuf :: from ( file) ;
107
+ if let Some ( p_path) = path. parent ( ) {
108
+ if let Some ( stem) = path. file_stem ( ) {
109
+ let file_name = p_path. join ( PathBuf :: from ( format ! (
110
+ "{}.rs.diagnostics" ,
111
+ stem. to_string_lossy( )
112
+ ) ) ) ;
113
+ println ! ( "Marked warning(s) into {:?}" , & file_name) ;
114
+ if let Some ( p) = file_name. parent ( ) {
115
+ if !p. exists ( ) {
116
+ std:: fs:: create_dir ( p) . ok ( ) ;
117
+ }
118
+ }
119
+ if let Ok ( content) = std:: str:: from_utf8 ( & output) {
120
+ std:: fs:: write ( & file_name, content) . ok ( ) ;
121
+ }
122
+ }
123
+ }
124
+ }
125
+ }
126
+ }
127
+ command. wait ( ) . ok ( ) ;
91
128
}
92
129
}
93
- for file in map. keys ( ) {
94
- let source = read_to_string ( file)
95
- . ok ( )
96
- . expect ( "sth wrong with reading the file into a string" ) ;
97
- let v = map
98
- . get ( file)
99
- . expect ( "sth wrong with getting a file from the map" ) ;
100
- let output = markup ( source. as_bytes ( ) , v. to_vec ( ) ) ;
101
- let path = PathBuf :: from ( file) ;
102
- let file_name = path
103
- . parent ( )
104
- . expect ( "something wrong with the parent of the path" )
105
- . join ( format ! (
106
- "{}.rs.diagnostics" ,
107
- path. file_stem( )
108
- . expect( "something wrong with the file stem" )
109
- . to_string_lossy( )
110
- ) ) ;
111
- println ! ( "Marked warning(s) into {:?}" , & file_name) ;
112
- if !file_name
113
- . parent ( )
114
- . expect ( "sth wrong with the parent of the file_name" )
115
- . exists ( )
116
- {
117
- std:: fs:: create_dir (
118
- file_name
119
- . parent ( )
120
- . expect ( "something with creating the folder" ) ,
121
- )
122
- . ok ( ) ;
123
- }
124
- std:: fs:: write (
125
- & file_name,
126
- std:: str:: from_utf8 ( & output) . expect ( "sth wrong with converting output into utf8 str" ) ,
127
- )
128
- . ok ( ) ;
129
- }
130
- let _output = command. wait ( ) . expect ( "Couldn't get cargo's exit status" ) ;
131
130
}
132
131
133
- fn sub_messages ( children : & Vec < Diagnostic > ) -> String {
132
+ fn sub_messages ( children : & [ Diagnostic ] ) -> String {
134
133
children
135
- . into_iter ( )
136
- . map ( |x| x. message . to_owned ( ) )
134
+ . iter ( )
135
+ . map ( |x| {
136
+ if let Some ( rendered) = & x. rendered {
137
+ format ! ( "{}: {}" , & x. message, & rendered)
138
+ } else {
139
+ x. message . to_owned ( )
140
+ }
141
+ } )
137
142
. collect :: < Vec < String > > ( )
138
143
. join ( "\n " )
139
144
}
140
145
141
146
fn remove_previously_generated_files ( ) {
142
- let command = Command :: new ( "find" )
147
+ if let Ok ( command) = Command :: new ( "find" )
143
148
. args ( [ "." , "-name" , "*.rs.diagnostics" ] )
144
149
. stdout ( Stdio :: piped ( ) )
145
150
. spawn ( )
146
- . expect ( "rm diagnostics files failed" ) ;
147
- let output = command
148
- . wait_with_output ( )
149
- . expect ( "failed to aquire programm output" )
150
- . stdout ;
151
- if !output. is_empty ( ) {
152
- println ! ( "Removed previously generated warning files" )
151
+ {
152
+ if let Ok ( output) = command. wait_with_output ( ) {
153
+ if !output. stdout . is_empty ( ) {
154
+ println ! ( "Removed previously generated warning files" )
155
+ }
156
+ if let Ok ( s) = String :: from_utf8 ( output. stdout ) {
157
+ s. split ( '\n' ) . for_each ( |tmp| {
158
+ if let Ok ( mut command) = Command :: new ( "rm" )
159
+ . args ( [ "-f" , tmp] )
160
+ . stdout ( Stdio :: piped ( ) )
161
+ . spawn ( )
162
+ {
163
+ if let Ok ( w) = command. wait ( ) {
164
+ if !w. success ( ) {
165
+ println ! ( "wait not successful" ) ;
166
+ }
167
+ }
168
+ }
169
+ } ) ;
170
+ }
171
+ }
153
172
}
154
- String :: from_utf8 ( output)
155
- . expect ( "programm output was not valid utf-8" )
156
- . split ( '\n' )
157
- . for_each ( |tmp| {
158
- let mut command = Command :: new ( "rm" )
159
- . args ( [ "-f" , tmp] )
160
- . stdout ( Stdio :: piped ( ) )
161
- . spawn ( )
162
- . expect ( "failed to remove the file" ) ;
163
- command. wait ( ) . expect ( "problem with file deletion" ) ;
164
- } ) ;
165
173
}
0 commit comments