From 9a193b0935eb86a8da91349febcef0654074278a Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Sun, 12 Jun 2022 15:20:33 +0900 Subject: [PATCH 1/2] Don't check typedefs in impls in style checker Signed-off-by: Yuki Okushi --- ci/style.rs | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/ci/style.rs b/ci/style.rs index b7e5d8ee8f6de..5b4054f129761 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; @@ -69,10 +65,7 @@ fn walk(path: &Path, err: &mut Errors) { match &name[..] { n if !n.ends_with(".rs") => continue, - "dox.rs" | "lib.rs" | - "ctypes.rs" | - "libc.rs" | "macros.rs" => continue, _ => {} @@ -105,26 +98,9 @@ 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")) @@ -137,6 +113,12 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) { 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(); @@ -154,7 +136,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; From ba9676c06937b3b6e47b579bdcc3b3a6ddfafce5 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Sun, 12 Jun 2022 15:21:00 +0900 Subject: [PATCH 2/2] Format style checker Signed-off-by: Yuki Okushi --- ci/style.rs | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/ci/style.rs b/ci/style.rs index 5b4054f129761..e21a40cf1d78a 100644 --- a/ci/style.rs +++ b/ci/style.rs @@ -34,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() { @@ -58,15 +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, - "lib.rs" | - "macros.rs" => continue, + "lib.rs" | "macros.rs" => continue, _ => {} } @@ -101,13 +102,13 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) { 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 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")) { @@ -123,7 +124,7 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) { 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") { @@ -149,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 {