]>
git.proxmox.com Git - rustc.git/blob - src/liblibc/ci/style.rs
1 //! Simple script to verify the coding style of this library
5 //! The first argument to this script is the directory to run on, so running
6 //! this script should be as simple as:
15 //! The current style is:
17 //! * No trailing whitespace
19 //! * 80-character lines
20 //! * `extern` instead of `extern "C"`
21 //! * Specific module layout:
26 //! 5. f! { ... } functions
27 //! 6. extern functions
28 //! 7. modules + pub use
30 //! Things not verified:
34 //! * leading colons on paths
38 use std
::io
::prelude
::*;
42 ($e
:expr
) => (match $e
{
44 Err(e
) => panic
!("{} failed with {}", stringify
!($e
), e
),
49 let arg
= env
::args().skip(1).next().unwrap_or(".".to_string());
51 let mut errors
= Errors { errs: false }
;
52 walk(Path
::new(&arg
), &mut errors
);
55 panic
!("found some lint errors");
57 println
!("good style!");
61 fn walk(path
: &Path
, err
: &mut Errors
) {
62 for entry
in t
!(path
.read_dir()).map(|e
| t
!(e
)) {
63 let path
= entry
.path();
64 if t
!(entry
.file_type()).is_dir() {
69 let name
= entry
.file_name().into_string().unwrap();
71 n
if !n
.ends_with(".rs") => continue,
75 "macros.rs" => continue,
80 let mut contents
= String
::new();
81 t
!(t
!(fs
::File
::open(&path
)).read_to_string(&mut contents
));
83 check_style(&contents
, &path
, err
);
91 #[derive(Clone, Copy, PartialEq)]
103 fn check_style(file
: &str, path
: &Path
, err
: &mut Errors
) {
104 let mut state
= State
::Start
;
105 let mut s_macros
= 0;
106 let mut f_macros
= 0;
107 let mut prev_blank
= false;
109 for (i
, line
) in file
.lines().enumerate() {
112 err
.error(path
, i
, "double blank line");
118 if line
!= line
.trim_right() {
119 err
.error(path
, i
, "trailing whitespace");
121 if line
.contains("\t") {
122 err
.error(path
, i
, "tab character");
125 err
.error(path
, i
, "line longer than 80 chars");
127 if line
.contains("extern \"C\"") {
128 err
.error(path
, i
, "use `extern` instead of `extern \"C\"");
130 if line
.contains("#[cfg(") && !line
.contains(" if ") {
131 if state
!= State
::Structs
{
132 err
.error(path
, i
, "use cfg_if! and submodules \
137 let line
= line
.trim_left();
138 let is_pub
= line
.starts_with("pub ");
139 let line
= if is_pub {&line[4..]}
else {line}
;
141 let line_state
= if line
.starts_with("use ") {
147 } else if line
.starts_with("const ") {
149 } else if line
.starts_with("type ") {
151 } else if line
.starts_with("s! {") {
154 } else if line
.starts_with("f! {") {
156 State
::FunctionDefinitions
157 } else if line
.starts_with("extern ") {
159 } else if line
.starts_with("mod ") {
165 if state
as usize > line_state
as usize {
166 err
.error(path
, i
, &format
!("{} found after {} when \
168 line_state
.desc(), state
.desc()));
173 err
.error(path
, i
, "multiple f! macros in one module");
177 err
.error(path
, i
, "multiple s! macros in one module");
185 fn desc(&self) -> &str {
187 State
::Start
=> "start",
188 State
::Imports
=> "import",
189 State
::Typedefs
=> "typedef",
190 State
::Structs
=> "struct",
191 State
::Constants
=> "constant",
192 State
::FunctionDefinitions
=> "function definition",
193 State
::Functions
=> "extern function",
194 State
::Modules
=> "module",
200 fn error(&mut self, path
: &Path
, line
: usize, msg
: &str) {
202 println
!("{}:{} - {}", path
.display(), line
+ 1, msg
);