14
14
//!
15
15
//! The current style is:
16
16
//!
17
- //! * No trailing whitespace
18
- //! * No tabs
19
- //! * 100-character lines
20
17
//! * Specific module layout:
21
18
//! 1. use directives
22
19
//! 2. typedefs
29
26
//! Things not verified:
30
27
//!
31
28
//! * alignment
32
- //! * 4-space tabs
33
29
//! * leading colons on paths
34
30
35
31
use std:: env;
@@ -38,10 +34,12 @@ use std::io::prelude::*;
38
34
use std:: path:: Path ;
39
35
40
36
macro_rules! t {
41
- ( $e: expr) => ( match $e {
42
- Ok ( e) => e,
43
- Err ( e) => panic!( "{} failed with {}" , stringify!( $e) , e) ,
44
- } )
37
+ ( $e: expr) => {
38
+ match $e {
39
+ Ok ( e) => e,
40
+ Err ( e) => panic!( "{} failed with {}" , stringify!( $e) , e) ,
41
+ }
42
+ } ;
45
43
}
46
44
47
45
fn main ( ) {
@@ -62,18 +60,14 @@ fn walk(path: &Path, err: &mut Errors) {
62
60
let path = entry. path ( ) ;
63
61
if t ! ( entry. file_type( ) ) . is_dir ( ) {
64
62
walk ( & path, err) ;
65
- continue
63
+ continue ;
66
64
}
67
65
68
66
let name = entry. file_name ( ) . into_string ( ) . unwrap ( ) ;
69
67
match & name[ ..] {
70
68
n if !n. ends_with ( ".rs" ) => continue ,
71
69
72
- "dox.rs" |
73
- "lib.rs" |
74
- "ctypes.rs" |
75
- "libc.rs" |
76
- "macros.rs" => continue ,
70
+ "lib.rs" | "macros.rs" => continue ,
77
71
78
72
_ => { }
79
73
}
@@ -105,43 +99,32 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) {
105
99
let mut state = State :: Start ;
106
100
let mut s_macros = 0 ;
107
101
let mut f_macros = 0 ;
108
- let mut prev_blank = false ;
102
+ let mut in_impl = false ;
109
103
110
104
for ( i, line) in file. lines ( ) . enumerate ( ) {
111
- if line == "" {
112
- if prev_blank {
113
- err. error ( path, i, "double blank line" ) ;
114
- }
115
- prev_blank = true ;
116
- } else {
117
- prev_blank = false ;
118
- }
119
- if line != line. trim_end ( ) {
120
- err. error ( path, i, "trailing whitespace" ) ;
121
- }
122
- if line. contains ( "\t " ) {
123
- err. error ( path, i, "tab character" ) ;
124
- }
125
- if line. len ( ) > 100 && !( line. contains ( "https://" ) || line. contains ( "http://" ) ) {
126
- err. error ( path, i, "line longer than 100 chars" ) ;
127
- }
128
- if line. contains ( "#[cfg(" ) && line. contains ( ']' ) && !line. contains ( " if " )
129
- && !( line. contains ( "target_endian" ) ||
130
- line. contains ( "target_arch" ) )
105
+ if line. contains ( "#[cfg(" )
106
+ && line. contains ( ']' )
107
+ && !line. contains ( " if " )
108
+ && !( line. contains ( "target_endian" ) || line. contains ( "target_arch" ) )
131
109
{
132
110
if state != State :: Structs {
133
- err. error ( path, i, "use cfg_if! and submodules \
134
- instead of #[cfg]") ;
111
+ err. error ( path, i, "use cfg_if! and submodules instead of #[cfg]" ) ;
135
112
}
136
113
}
137
114
if line. contains ( "#[derive(" ) && ( line. contains ( "Copy" ) || line. contains ( "Clone" ) ) {
138
115
err. error ( path, i, "impl ::Copy and ::Clone manually" ) ;
139
116
}
117
+ if line. contains ( "impl" ) {
118
+ in_impl = true ;
119
+ }
120
+ if in_impl && line. starts_with ( '}' ) {
121
+ in_impl = false ;
122
+ }
140
123
141
124
let orig_line = line;
142
125
let line = line. trim_start ( ) ;
143
126
let is_pub = line. starts_with ( "pub " ) ;
144
- let line = if is_pub { & line[ 4 ..] } else { line} ;
127
+ let line = if is_pub { & line[ 4 ..] } else { line } ;
145
128
146
129
let line_state = if line. starts_with ( "use " ) {
147
130
if line. contains ( "c_void" ) {
@@ -154,7 +137,7 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) {
154
137
}
155
138
} else if line. starts_with ( "const " ) {
156
139
State :: Constants
157
- } else if line. starts_with ( "type " ) {
140
+ } else if line. starts_with ( "type " ) && !in_impl {
158
141
State :: Typedefs
159
142
} else if line. starts_with ( "s! {" ) {
160
143
s_macros += 1 ;
@@ -167,13 +150,19 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) {
167
150
} else if line. starts_with ( "mod " ) {
168
151
State :: Modules
169
152
} else {
170
- continue
153
+ continue ;
171
154
} ;
172
155
173
156
if state as usize > line_state as usize {
174
- err. error ( path, i, & format ! ( "{} found after {} when \
175
- it belongs before",
176
- line_state. desc( ) , state. desc( ) ) ) ;
157
+ err. error (
158
+ path,
159
+ i,
160
+ & format ! (
161
+ "{} found after {} when it belongs before" ,
162
+ line_state. desc( ) ,
163
+ state. desc( )
164
+ ) ,
165
+ ) ;
177
166
}
178
167
179
168
if f_macros == 2 {
0 commit comments