bevy/tools/ci/src/main.rs
Félix Lescaudey de Maneville f000c2b951 Clippy improvements (#4665)
# Objective

Follow up to my previous MR #3718 to add new clippy warnings to bevy:

- [x] [~~option_if_let_else~~](https://rust-lang.github.io/rust-clippy/master/#option_if_let_else) (reverted)
- [x] [redundant_else](https://rust-lang.github.io/rust-clippy/master/#redundant_else)
- [x] [match_same_arms](https://rust-lang.github.io/rust-clippy/master/#match_same_arms)
- [x] [semicolon_if_nothing_returned](https://rust-lang.github.io/rust-clippy/master/#semicolon_if_nothing_returned)
- [x] [explicit_iter_loop](https://rust-lang.github.io/rust-clippy/master/#explicit_iter_loop)
- [x] [map_flatten](https://rust-lang.github.io/rust-clippy/master/#map_flatten)

There is one commit per clippy warning, and the matching flags are added to the CI execution.

To test the CI execution you may run `cargo run -p ci -- clippy` at the root.

I choose the add the flags in the `ci` tool crate to avoid having them in every `lib.rs` but I guess it could become an issue with suprise warnings coming up after a commit/push


Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2022-05-31 01:38:07 +00:00

128 lines
4.4 KiB
Rust

use xshell::{cmd, Shell};
use bitflags::bitflags;
bitflags! {
struct Check: u32 {
const FORMAT = 0b00000001;
const CLIPPY = 0b00000010;
const COMPILE_FAIL = 0b00000100;
const TEST = 0b00001000;
const DOC_TEST = 0b00010000;
const DOC_CHECK = 0b00100000;
const BENCH_CHECK = 0b01000000;
const EXAMPLE_CHECK = 0b10000000;
const COMPILE_CHECK = 0b100000000;
}
}
const CLIPPY_FLAGS: [&str; 8] = [
"-Aclippy::type_complexity",
"-Wclippy::doc_markdown",
"-Wclippy::redundant_else",
"-Wclippy::match_same_arms",
"-Wclippy::semicolon_if_nothing_returned",
"-Wclippy::explicit_iter_loop",
"-Wclippy::map_flatten",
"-Dwarnings",
];
fn main() {
// When run locally, results may differ from actual CI runs triggered by
// .github/workflows/ci.yml
// - Official CI runs latest stable
// - Local runs use whatever the default Rust is locally
let what_to_run = match std::env::args().nth(1).as_deref() {
Some("format") => Check::FORMAT,
Some("clippy") => Check::CLIPPY,
Some("compile-fail") => Check::COMPILE_FAIL,
Some("test") => Check::TEST,
Some("doc-test") => Check::DOC_TEST,
Some("doc-check") => Check::DOC_CHECK,
Some("bench-check") => Check::BENCH_CHECK,
Some("example-check") => Check::EXAMPLE_CHECK,
Some("lints") => Check::FORMAT | Check::CLIPPY,
Some("doc") => Check::DOC_TEST | Check::DOC_CHECK,
Some("compile") => {
Check::COMPILE_FAIL | Check::BENCH_CHECK | Check::EXAMPLE_CHECK | Check::COMPILE_CHECK
}
_ => Check::all(),
};
let sh = Shell::new().unwrap();
if what_to_run.contains(Check::FORMAT) {
// See if any code needs to be formatted
cmd!(sh, "cargo fmt --all -- --check")
.run()
.expect("Please run 'cargo fmt --all' to format your code.");
}
if what_to_run.contains(Check::CLIPPY) {
// See if clippy has any complaints.
// - Type complexity must be ignored because we use huge templates for queries
cmd!(
sh,
"cargo clippy --workspace --all-targets --all-features -- {CLIPPY_FLAGS...}"
)
.run()
.expect("Please fix clippy errors in output above.");
}
if what_to_run.contains(Check::COMPILE_FAIL) {
// Run UI tests (they do not get executed with the workspace tests)
// - See crates/bevy_ecs_compile_fail_tests/README.md
let _subdir = sh.push_dir("crates/bevy_ecs_compile_fail_tests");
cmd!(sh, "cargo test --target-dir ../../target")
.run()
.expect("Compiler errors of the ECS compile fail tests seem to be different than expected! Check locally and compare rust versions.");
}
if what_to_run.contains(Check::TEST) {
// Run tests (except doc tests and without building examples)
cmd!(sh, "cargo test --workspace --lib --bins --tests --benches")
.run()
.expect("Please fix failing tests in output above.");
}
if what_to_run.contains(Check::DOC_TEST) {
// Run doc tests
cmd!(sh, "cargo test --workspace --doc")
.run()
.expect("Please fix failing doc-tests in output above.");
}
if what_to_run.contains(Check::DOC_CHECK) {
// Check that building docs work and does not emit warnings
std::env::set_var("RUSTDOCFLAGS", "-D warnings");
cmd!(
sh,
"cargo doc --workspace --all-features --no-deps --document-private-items"
)
.run()
.expect("Please fix doc warnings in output above.");
}
if what_to_run.contains(Check::COMPILE_FAIL) {
let _subdir = sh.push_dir("benches");
// Check that benches are building
cmd!(sh, "cargo check --benches --target-dir ../target")
.run()
.expect("Failed to check the benches.");
}
if what_to_run.contains(Check::EXAMPLE_CHECK) {
// Build examples and check they compile
cmd!(sh, "cargo check --workspace --examples")
.run()
.expect("Please fix failing doc-tests in output above.");
}
if what_to_run.contains(Check::COMPILE_CHECK) {
// Build examples and check they compile
cmd!(sh, "cargo check --workspace")
.run()
.expect("Please fix failing doc-tests in output above.");
}
}