Skip to content

Don't check typedefs in impls in style checker #2824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 32 additions & 43 deletions ci/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
//!
//! The current style is:
//!
//! * No trailing whitespace
//! * No tabs
//! * 100-character lines
//! * Specific module layout:
//! 1. use directives
//! 2. typedefs
Expand All @@ -29,7 +26,6 @@
//! Things not verified:
//!
//! * alignment
//! * 4-space tabs
//! * leading colons on paths

use std::env;
Expand All @@ -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() {
Expand All @@ -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,

_ => {}
}
Expand Down Expand Up @@ -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");
}
Comment on lines -111 to -127
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed some checks as rustfmt would validate them.

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") {
Expand All @@ -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;
Expand All @@ -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 {
Expand Down