-
Notifications
You must be signed in to change notification settings - Fork 1k
/
style.rs
207 lines (185 loc) · 5.29 KB
/
style.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//! Simple script to verify the coding style of this library
//!
//! ## How to run
//!
//! The first argument to this script is the directory to run on, so running
//! this script should be as simple as:
//!
//! ```notrust
//! rustc ci/style.rs
//! ./style src
//! ```
//!
//! ## Guidelines
//!
//! The current style is:
//!
//! * Specific module layout:
//! 1. use directives
//! 2. typedefs
//! 3. structs
//! 4. constants
//! 5. f! { ... } functions
//! 6. extern functions
//! 7. modules + pub use
//!
//! Things not verified:
//!
//! * alignment
//! * leading colons on paths
use std::env;
use std::fs;
use std::io::prelude::*;
use std::path::Path;
macro_rules! t {
($e:expr) => {
match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {}", stringify!($e), e),
}
};
}
fn main() {
let arg = env::args().skip(1).next().unwrap_or(".".to_string());
let mut errors = Errors { errs: false };
walk(Path::new(&arg), &mut errors);
if errors.errs {
panic!("found some lint errors");
} else {
println!("good style!");
}
}
fn walk(path: &Path, err: &mut Errors) {
for entry in t!(path.read_dir()).map(|e| t!(e)) {
let path = entry.path();
if t!(entry.file_type()).is_dir() {
walk(&path, err);
continue;
}
let name = entry.file_name().into_string().unwrap();
match &name[..] {
n if !n.ends_with(".rs") => continue,
"lib.rs" | "macros.rs" => continue,
_ => {}
}
let mut contents = String::new();
t!(t!(fs::File::open(&path)).read_to_string(&mut contents));
check_style(&contents, &path, err);
}
}
struct Errors {
errs: bool,
}
#[derive(Clone, Copy, PartialEq)]
enum State {
Start,
Imports,
Typedefs,
Structs,
Constants,
FunctionDefinitions,
Functions,
Modules,
}
fn check_style(file: &str, path: &Path, err: &mut Errors) {
let mut state = State::Start;
let mut s_macros = 0;
let mut f_macros = 0;
let mut in_impl = false;
for (i, line) in file.lines().enumerate() {
if line.contains("#[cfg(")
&& line.contains(']')
&& !line.contains(" if ")
&& !(line.contains("target_endian") || line.contains("target_arch"))
{
if state != State::Structs {
err.error(path, i, "use cfg_if! and submodules instead of #[cfg]");
}
}
if line.contains("#[derive(") && (line.contains("Copy") || line.contains("Clone")) {
err.error(path, i, "impl ::Copy and ::Clone manually");
}
if line.contains("impl") {
in_impl = true;
}
if in_impl && line.starts_with('}') {
in_impl = false;
}
let orig_line = line;
let line = line.trim_start();
let is_pub = line.starts_with("pub ");
let line = if is_pub { &line[4..] } else { line };
let line_state = if line.starts_with("use ") {
if line.contains("c_void") {
continue;
}
if is_pub {
State::Modules
} else {
State::Imports
}
} else if line.starts_with("const ") {
State::Constants
} else if line.starts_with("type ") && !in_impl {
State::Typedefs
} else if line.starts_with("s! {") {
s_macros += 1;
State::Structs
} else if line.starts_with("s_no_extra_traits! {") {
// multiple macros of this type are allowed
State::Structs
} else if line.starts_with("s_paren! {") {
// multiple macros of this type are allowed
State::Structs
} else if line.starts_with("f! {") {
f_macros += 1;
State::FunctionDefinitions
} else if line.starts_with("extern ") && !orig_line.starts_with(" ") {
State::Functions
} else if line.starts_with("mod ") {
State::Modules
} else {
continue;
};
if state as usize > line_state as usize {
err.error(
path,
i,
&format!(
"{} found after {} when it belongs before",
line_state.desc(),
state.desc()
),
);
}
if f_macros == 2 {
f_macros += 1;
err.error(path, i, "multiple f! macros in one module");
}
if s_macros == 2 {
s_macros += 1;
err.error(path, i, "multiple s! macros in one module");
}
state = line_state;
}
}
impl State {
fn desc(&self) -> &str {
match *self {
State::Start => "start",
State::Imports => "import",
State::Typedefs => "typedef",
State::Structs => "struct",
State::Constants => "constant",
State::FunctionDefinitions => "function definition",
State::Functions => "extern function",
State::Modules => "module",
}
}
}
impl Errors {
fn error(&mut self, path: &Path, line: usize, msg: &str) {
self.errs = true;
println!("{}:{}: {}", path.display(), line + 1, msg);
}
}