diff --git a/ci/style.rs b/ci/style.rs index b7e5d8ee8f6de..e21a40cf1d78a 100644 --- a/ci/style.rs +++ b/ci/style.rs @@ -14,9 +14,6 @@ //! //! The current style is: //! -//! * No trailing whitespace -//! * No tabs -//! * 100-character lines //! * Specific module layout: //! 1. use directives //! 2. typedefs @@ -29,7 +26,6 @@ //! Things not verified: //! //! * alignment -//! * 4-space tabs //! * leading colons on paths use std::env; @@ -38,10 +34,12 @@ 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), - }) + ($e:expr) => { + match $e { + Ok(e) => e, + Err(e) => panic!("{} failed with {}", stringify!($e), e), + } + }; } fn main() { @@ -62,18 +60,14 @@ fn walk(path: &Path, err: &mut Errors) { let path = entry.path(); if t!(entry.file_type()).is_dir() { walk(&path, err); - continue + continue; } let name = entry.file_name().into_string().unwrap(); match &name[..] { n if !n.ends_with(".rs") => continue, - "dox.rs" | - "lib.rs" | - "ctypes.rs" | - "libc.rs" | - "macros.rs" => continue, + "lib.rs" | "macros.rs" => continue, _ => {} } @@ -105,43 +99,32 @@ 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 prev_blank = false; + let mut in_impl = false; for (i, line) in file.lines().enumerate() { - if line == "" { - if prev_blank { - err.error(path, i, "double blank line"); - } - prev_blank = true; - } else { - prev_blank = false; - } - if line != line.trim_end() { - err.error(path, i, "trailing whitespace"); - } - if line.contains("\t") { - err.error(path, i, "tab character"); - } - if line.len() > 100 && !(line.contains("https://") || line.contains("http://")) { - err.error(path, i, "line longer than 100 chars"); - } - if line.contains("#[cfg(") && line.contains(']') && !line.contains(" if ") - && !(line.contains("target_endian") || - line.contains("target_arch")) + 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]"); + 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 = if is_pub { &line[4..] } else { line }; let line_state = if line.starts_with("use ") { if line.contains("c_void") { @@ -154,7 +137,7 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) { } } else if line.starts_with("const ") { State::Constants - } else if line.starts_with("type ") { + } else if line.starts_with("type ") && !in_impl { State::Typedefs } else if line.starts_with("s! {") { s_macros += 1; @@ -167,13 +150,19 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) { } else if line.starts_with("mod ") { State::Modules } else { - continue + 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())); + err.error( + path, + i, + &format!( + "{} found after {} when it belongs before", + line_state.desc(), + state.desc() + ), + ); } if f_macros == 2 {