From 302e2753d7da047919c3d4acfbae0ef34f3dd41b Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Fri, 26 Feb 2021 05:31:55 +0100 Subject: [PATCH 1/2] Go clippy-clean for the `rust/*` crates and enforce it This covers the default clippy warnings. Signed-off-by: Miguel Ojeda --- rust/compiler_builtins.rs | 4 ++++ rust/kernel/file_operations.rs | 8 ++++---- rust/kernel/lib.rs | 4 ++++ rust/kernel/miscdev.rs | 6 ++++++ rust/kernel/printk.rs | 7 ++++++- rust/module.rs | 10 +++++++--- 6 files changed, 31 insertions(+), 8 deletions(-) diff --git a/rust/compiler_builtins.rs b/rust/compiler_builtins.rs index f6dda3188f0be5..c8f765b1fd81e8 100644 --- a/rust/compiler_builtins.rs +++ b/rust/compiler_builtins.rs @@ -25,6 +25,10 @@ #![compiler_builtins] #![no_builtins] #![no_std] +#![deny(clippy::complexity)] +#![deny(clippy::correctness)] +#![deny(clippy::perf)] +#![deny(clippy::style)] macro_rules! define_panicking_intrinsics( ($reason: tt, { $($ident: ident, )* }) => { diff --git a/rust/kernel/file_operations.rs b/rust/kernel/file_operations.rs index 595f82689891d1..a66c58becef292 100644 --- a/rust/kernel/file_operations.rs +++ b/rust/kernel/file_operations.rs @@ -160,17 +160,17 @@ impl FileOperationsVtable { pub(crate) const VTABLE: bindings::file_operations = bindings::file_operations { open: Some(open_callback::), release: Some(release_callback::), - read: if let Some(_) = T::READ { + read: if T::READ.is_some() { Some(read_callback::) } else { None }, - write: if let Some(_) = T::WRITE { + write: if T::WRITE.is_some() { Some(write_callback::) } else { None }, - llseek: if let Some(_) = T::SEEK { + llseek: if T::SEEK.is_some() { Some(llseek_callback::) } else { None @@ -184,7 +184,7 @@ impl FileOperationsVtable { fasync: None, flock: None, flush: None, - fsync: if let Some(_) = T::FSYNC { + fsync: if T::FSYNC.is_some() { Some(fsync_callback::) } else { None diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index d5b436d251ffec..e40a7970422dd0 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -13,6 +13,10 @@ #![no_std] #![feature(allocator_api, alloc_error_handler)] +#![deny(clippy::complexity)] +#![deny(clippy::correctness)] +#![deny(clippy::perf)] +#![deny(clippy::style)] // Ensure conditional compilation based on the kernel configuration works; // otherwise we may silently break things like initcall handling. diff --git a/rust/kernel/miscdev.rs b/rust/kernel/miscdev.rs index 86914ab6fc4afc..af8f75e55fb91b 100644 --- a/rust/kernel/miscdev.rs +++ b/rust/kernel/miscdev.rs @@ -73,6 +73,12 @@ impl Registration { } } +impl Default for Registration { + fn default() -> Self { + Self::new() + } +} + // SAFETY: The only method is `register()`, which requires a (pinned) mutable // `Registration`, so it is safe to pass `&Registration` to multiple threads // because it offers no interior mutability. diff --git a/rust/kernel/printk.rs b/rust/kernel/printk.rs index e6cd679c977e5d..aef05c2c06e1fa 100644 --- a/rust/kernel/printk.rs +++ b/rust/kernel/printk.rs @@ -33,7 +33,6 @@ pub struct LogLineWriter { pos: usize, } -#[allow(clippy::new_without_default)] impl LogLineWriter { /// Creates a new [`LogLineWriter`]. pub fn new() -> LogLineWriter { @@ -49,6 +48,12 @@ impl LogLineWriter { } } +impl Default for LogLineWriter { + fn default() -> Self { + Self::new() + } +} + impl fmt::Write for LogLineWriter { fn write_str(&mut self, s: &str) -> fmt::Result { let copy_len = cmp::min(LOG_LINE_MAX - self.pos, s.as_bytes().len()); diff --git a/rust/module.rs b/rust/module.rs index b0b2abb3e476fe..792f6020d840ae 100644 --- a/rust/module.rs +++ b/rust/module.rs @@ -4,6 +4,11 @@ //! //! C header: [`include/linux/moduleparam.h`](../../../include/linux/moduleparam.h) +#![deny(clippy::complexity)] +#![deny(clippy::correctness)] +#![deny(clippy::perf)] +#![deny(clippy::style)] + use proc_macro::{token_stream, Delimiter, Group, TokenStream, TokenTree}; fn expect_ident(it: &mut token_stream::IntoIter) -> String { @@ -39,8 +44,7 @@ fn expect_group(it: &mut token_stream::IntoIter) -> Group { } fn expect_end(it: &mut token_stream::IntoIter) { - if let None = it.next() { - } else { + if it.next().is_some() { panic!("Expected end"); } } @@ -73,7 +77,7 @@ fn get_byte_string(it: &mut token_stream::IntoIter, expected_name: &str) -> Stri let byte_string = get_literal(it, expected_name); assert!(byte_string.starts_with("b\"")); - assert!(byte_string.ends_with("\"")); + assert!(byte_string.ends_with('\"')); byte_string[2..byte_string.len() - 1].to_string() } From 2cd2b9a63a8fe41e9b60c25c8722156ba77809ee Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Fri, 26 Feb 2021 05:33:00 +0100 Subject: [PATCH 2/2] Avoid passing `rustc` as second parameter to `clippy-driver` It turns out that is only a workaround intended for `cargo`. The official instructions since PR [1] describe the usage of `clippy-driver`, and they intentionally only cover replacing `rustc` with `clippy-driver` rather than wrapping it, as discussed in that PR. [1] https://github.com/rust-lang/rust-clippy/pull/6782 Signed-off-by: Miguel Ojeda --- Makefile | 11 ++++++----- rust/Makefile | 11 ++++++----- scripts/Makefile.build | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 7a15f49375708a..79c63c6fa9b41a 100644 --- a/Makefile +++ b/Makefile @@ -525,14 +525,15 @@ KBUILD_LDFLAGS := CLANG_FLAGS := ifeq ($(KBUILD_CLIPPY),1) - CLIPPY_QUIET_TAG := CLIPPY$(space) + RUSTC_OR_CLIPPY_QUIET := CLIPPY + RUSTC_OR_CLIPPY = $(CLIPPY_DRIVER) else - CLIPPY_QUIET_TAG := - CLIPPY_DRIVER := + RUSTC_OR_CLIPPY_QUIET := RUSTC + RUSTC_OR_CLIPPY = $(RUSTC) endif -export CLIPPY_QUIET_TAG +export RUSTC_OR_CLIPPY_QUIET RUSTC_OR_CLIPPY -export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC RUSTC CLIPPY_DRIVER BINDGEN +export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC RUSTC BINDGEN export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AWK INSTALLKERNEL export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD diff --git a/rust/Makefile b/rust/Makefile index 1b180c2eb0ce77..54d2dbcd1a4f67 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -90,9 +90,10 @@ $(objtree)/rust/exports_kernel_generated.h: $(objtree)/rust/kernel.o FORCE # `-Cpanic=unwind -Cforce-unwind-tables=y` overrides `rustc_flags` in order to # avoid the https://github.com/rust-lang/rust/issues/82320 rustc crash. -quiet_cmd_rustc_procmacro = RUSTC P $(CLIPPY_QUIET_TAG)$@ +quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@ cmd_rustc_procmacro = \ - $(CLIPPY_DRIVER) $(RUSTC) $(rustc_flags) --emit=dep-info,link --extern proc_macro \ + $(RUSTC_OR_CLIPPY) $(rustc_flags) \ + --emit=dep-info,link --extern proc_macro \ -Cpanic=unwind -Cforce-unwind-tables=y \ --crate-type proc-macro --out-dir $(objtree)/rust/ \ --crate-name $(patsubst lib%.so,%,$(notdir $@)) $<; \ @@ -102,11 +103,11 @@ quiet_cmd_rustc_procmacro = RUSTC P $(CLIPPY_QUIET_TAG)$@ $(objtree)/rust/libmodule.so: $(srctree)/rust/module.rs FORCE $(call if_changed_dep,rustc_procmacro) -quiet_cmd_rustc_library = RUSTC L $(if $(skip_clippy),,$(CLIPPY_QUIET_TAG))$@ +quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@ cmd_rustc_library = \ RUST_BINDINGS_FILE=$(abspath $(objtree)/rust/bindings_generated.rs) \ - $(if $(skip_clippy),,$(CLIPPY_DRIVER)) $(RUSTC) $(rustc_flags) \ - $(rustc_cross_flags) $(rustc_target_flags) \ + $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \ + $(rustc_flags) $(rustc_cross_flags) $(rustc_target_flags) \ --crate-type rlib --out-dir $(objtree)/rust/ -L $(objtree)/rust/ \ --crate-name $(patsubst %.o,%,$(notdir $@)) $<; \ mv $(objtree)/rust/$(patsubst %.o,%,$(notdir $@)).d $(depfile); \ diff --git a/scripts/Makefile.build b/scripts/Makefile.build index a0ddfeeb29e482..ede914000337dd 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -301,10 +301,10 @@ $(obj)/%.lst: $(src)/%.c FORCE rustc_cross_flags := --target=$(srctree)/arch/$(SRCARCH)/rust/target.json -quiet_cmd_rustc_o_rs = RUSTC $(CLIPPY_QUIET_TAG)$(quiet_modtag) $@ +quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ cmd_rustc_o_rs = \ RUST_MODFILE=$(modfile) \ - $(CLIPPY_DRIVER) $(RUSTC) $(rustc_flags) $(rustc_cross_flags) \ + $(RUSTC_OR_CLIPPY) $(rustc_flags) $(rustc_cross_flags) \ --extern alloc --extern kernel \ --crate-type rlib --out-dir $(obj) -L $(objtree)/rust/ \ --crate-name $(patsubst %.o,%,$(notdir $@)) $<; \