Skip to content

Enable some lints and simplify CLIPPY=1 #566

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 8 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
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
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,14 @@ KBUILD_RUSTFLAGS := --emit=dep-info,obj,metadata --edition=2021 \
-Cforce-unwind-tables=n -Ccodegen-units=1 \
-Zbinary_dep_depinfo=y -Zsymbol-mangling-version=v0 \
-Dunsafe_op_in_unsafe_fn -Drust_2018_idioms \
-Wmissing_docs
KBUILD_CLIPPYFLAGS := -Dclippy::correctness -Dclippy::style \
-Dclippy::complexity -Dclippy::perf -Dclippy::float_arithmetic
-Dunreachable_pub -Dnon_ascii_idents \
-Wmissing_docs \
-Drustdoc::missing_crate_level_docs \
-Dclippy::correctness -Dclippy::style -Dclippy::suspicious \
-Dclippy::complexity -Dclippy::perf -Dclippy::float_arithmetic \
-Dclippy::let_unit_value -Dclippy::mut_mut \
-Dclippy::needless_bitwise_bool -Dclippy::needless_continue \
-Wclippy::dbg_macro
KBUILD_AFLAGS_KERNEL :=
KBUILD_CFLAGS_KERNEL :=
KBUILD_RUSTFLAGS_KERNEL :=
Expand All @@ -553,7 +558,7 @@ CLANG_FLAGS :=

ifeq ($(KBUILD_CLIPPY),1)
RUSTC_OR_CLIPPY_QUIET := CLIPPY
RUSTC_OR_CLIPPY = $(CLIPPY_DRIVER) $(KBUILD_CLIPPYFLAGS)
RUSTC_OR_CLIPPY = $(CLIPPY_DRIVER)
else
RUSTC_OR_CLIPPY_QUIET := RUSTC
RUSTC_OR_CLIPPY = $(RUSTC)
Expand Down
4 changes: 2 additions & 2 deletions drivers/android/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ impl<'a, 'b> AllocationView<'a, 'b> {
AllocationView { alloc, limit }
}

pub fn read<T>(&self, offset: usize) -> Result<T> {
pub(crate) fn read<T>(&self, offset: usize) -> Result<T> {
if offset.checked_add(size_of::<T>()).ok_or(Error::EINVAL)? > self.limit {
return Err(Error::EINVAL);
}
self.alloc.read(offset)
}

pub fn write<T>(&self, offset: usize, obj: &T) -> Result {
pub(crate) fn write<T>(&self, offset: usize, obj: &T) -> Result {
if offset.checked_add(size_of::<T>()).ok_or(Error::EINVAL)? > self.limit {
return Err(Error::EINVAL);
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/android/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use kernel::{

macro_rules! pub_no_prefix {
($prefix:ident, $($newname:ident),+) => {
$(pub const $newname: u32 = concat_idents!($prefix, $newname);)+
$(pub(crate) const $newname: u32 = concat_idents!($prefix, $newname);)+
};
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/android/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ impl DeliverToRead for Node {
}
}

pub struct NodeRef {
pub(crate) struct NodeRef {
pub(crate) node: Ref<Node>,
strong_count: usize,
weak_count: usize,
Expand Down
4 changes: 3 additions & 1 deletion rust/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
cmd_rustc_library = \
OBJTREE=$(abspath $(objtree)) \
$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
$(rust_flags) $(rust_cross_flags) $(rustc_target_flags) \
$(filter-out $(skip_flags),$(rust_flags) $(rust_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); \
Expand All @@ -324,6 +324,7 @@ $(objtree)/rust/compiler_builtins.o: $(srctree)/rust/compiler_builtins.rs \
$(call if_changed_dep,rustc_library)

$(objtree)/rust/alloc.o: private skip_clippy = 1
$(objtree)/rust/alloc.o: private skip_flags = -Dunreachable_pub
$(objtree)/rust/alloc.o: private rustc_target_flags = $(alloc-cfgs)
$(objtree)/rust/alloc.o: $(srctree)/rust/alloc/lib.rs \
$(objtree)/rust/compiler_builtins.o FORCE
Expand All @@ -346,6 +347,7 @@ $(objtree)/rust/kernel.o: $(srctree)/rust/kernel/lib.rs $(objtree)/rust/alloc.o
# Targets that need to expand twice
.SECONDEXPANSION:
$(objtree)/rust/core.o: private skip_clippy = 1
$(objtree)/rust/core.o: private skip_flags = -Dunreachable_pub
$(objtree)/rust/core.o: private rustc_target_flags = $(core-cfgs)
$(objtree)/rust/core.o: $$(RUST_LIB_SRC)/core/src/lib.rs FORCE
$(call if_changed_dep,rustc_library)
Expand Down
12 changes: 7 additions & 5 deletions rust/kernel/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::ptr;
use crate::bindings;
use crate::c_types;

pub struct KernelAllocator;
struct KernelAllocator;

unsafe impl GlobalAlloc for KernelAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
Expand All @@ -30,18 +30,20 @@ static ALLOCATOR: KernelAllocator = KernelAllocator;
// `rustc` only generates these for some crate types. Even then, we would need
// to extract the object file that has them from the archive. For the moment,
// let's generate them ourselves instead.
//
// Note that `#[no_mangle]` implies exported too, nowadays.
#[no_mangle]
pub fn __rust_alloc(size: usize, _align: usize) -> *mut u8 {
fn __rust_alloc(size: usize, _align: usize) -> *mut u8 {
unsafe { bindings::krealloc(core::ptr::null(), size, bindings::GFP_KERNEL) as *mut u8 }
}

#[no_mangle]
pub fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
unsafe { bindings::kfree(ptr as *const c_types::c_void) };
}

#[no_mangle]
pub fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: usize) -> *mut u8 {
fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: usize) -> *mut u8 {
unsafe {
bindings::krealloc(
ptr as *const c_types::c_void,
Expand All @@ -52,7 +54,7 @@ pub fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: u
}

#[no_mangle]
pub fn __rust_alloc_zeroed(size: usize, _align: usize) -> *mut u8 {
fn __rust_alloc_zeroed(size: usize, _align: usize) -> *mut u8 {
unsafe {
bindings::krealloc(
core::ptr::null(),
Expand Down
4 changes: 2 additions & 2 deletions rust/kernel/amba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//! C header: [`include/linux/amba/bus.h`](../../../../include/linux/amba/bus.h)

use crate::{
bindings, c_types, device, driver, from_kernel_result, io_mem::Resource, power, str::CStr,
to_result, types::PointerWrapper, Error, Result,
bindings, c_types, device, driver, error::from_kernel_result, io_mem::Resource, power,
str::CStr, to_result, types::PointerWrapper, Error, Result,
};
use core::{marker::PhantomData, ops::Deref};

Expand Down
1 change: 1 addition & 0 deletions rust/kernel/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
non_upper_case_globals,
non_snake_case,
improper_ctypes,
unreachable_pub,
unsafe_op_in_unsafe_fn
)]

Expand Down
6 changes: 3 additions & 3 deletions rust/kernel/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,7 @@ impl From<AllocError> for Error {
// # Invariant: `-bindings::MAX_ERRNO` fits in an `i16`.
crate::static_assert!(bindings::MAX_ERRNO <= -(i16::MIN as i32) as u32);

#[doc(hidden)]
pub fn from_kernel_result_helper<T>(r: Result<T>) -> T
pub(crate) fn from_kernel_result_helper<T>(r: Result<T>) -> T
where
T: From<i16>,
{
Expand Down Expand Up @@ -465,7 +464,6 @@ where
/// }
/// }
/// ```
#[macro_export]
macro_rules! from_kernel_result {
($($tt:tt)*) => {{
$crate::error::from_kernel_result_helper((|| {
Expand All @@ -474,6 +472,8 @@ macro_rules! from_kernel_result {
}};
}

pub(crate) use from_kernel_result;

/// Transform a kernel "error pointer" to a normal pointer.
///
/// Some kernel C API functions return an "error pointer" which optionally
Expand Down
3 changes: 1 addition & 2 deletions rust/kernel/file_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use alloc::boxed::Box;

use crate::{
bindings, c_types,
error::{Error, Result},
error::{from_kernel_result, Error, Result},
file::{File, FileRef},
from_kernel_result,
io_buffer::{IoBufferReader, IoBufferWriter},
iov_iter::IovIter,
sync::CondVar,
Expand Down
4 changes: 3 additions & 1 deletion rust/kernel/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//!
//! C header: [`include/linux/gpio/driver.h`](../../../../include/linux/gpio/driver.h)

use crate::{bindings, c_types, device, from_kernel_result, types::PointerWrapper, Error, Result};
use crate::{
bindings, c_types, device, error::from_kernel_result, types::PointerWrapper, Error, Result,
};
use core::{
cell::UnsafeCell,
marker::{PhantomData, PhantomPinned},
Expand Down
2 changes: 1 addition & 1 deletion rust/kernel/irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#![allow(dead_code)]

use crate::{bindings, c_types, from_kernel_result, types::PointerWrapper, Error, Result};
use crate::{bindings, c_types, error::from_kernel_result, types::PointerWrapper, Error, Result};
use core::ops::Deref;

type IrqHwNumber = bindings::irq_hw_number_t;
Expand Down
3 changes: 1 addition & 2 deletions rust/kernel/platdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

use crate::{
bindings, c_types,
error::{Error, Result},
from_kernel_result,
error::{from_kernel_result, Error, Result},
of::OfMatchTable,
str::CStr,
types::PointerWrapper,
Expand Down
2 changes: 1 addition & 1 deletion rust/kernel/power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#![allow(dead_code)]

use crate::{bindings, c_types, from_kernel_result, types::PointerWrapper, Result};
use crate::{bindings, c_types, error::from_kernel_result, types::PointerWrapper, Result};
use core::marker::PhantomData;

/// Corresponds to the kernel's `struct dev_pm_ops`.
Expand Down
9 changes: 6 additions & 3 deletions rust/kernel/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,20 @@ impl<T> CreatableLock for Mutex<T> {
}
}

pub struct EmptyGuardContext;

// SAFETY: The underlying kernel `struct mutex` object ensures mutual exclusion.
unsafe impl<T: ?Sized> Lock for Mutex<T> {
type Inner = T;
type GuardContext = ();
type GuardContext = EmptyGuardContext;

fn lock_noguard(&self) {
fn lock_noguard(&self) -> EmptyGuardContext {
// SAFETY: `mutex` points to valid memory.
unsafe { bindings::mutex_lock(self.mutex.get()) };
EmptyGuardContext
}

unsafe fn unlock(&self, _: &mut ()) {
unsafe fn unlock(&self, _: &mut EmptyGuardContext) {
// SAFETY: The safety requirements of the function ensure that the mutex is owned by the
// caller.
unsafe { bindings::mutex_unlock(self.mutex.get()) };
Expand Down
22 changes: 11 additions & 11 deletions rust/macros/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

use proc_macro::{token_stream, Group, TokenTree};

pub fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
if let Some(TokenTree::Ident(ident)) = it.next() {
Some(ident.to_string())
} else {
None
}
}

pub fn try_literal(it: &mut token_stream::IntoIter) -> Option<String> {
pub(crate) fn try_literal(it: &mut token_stream::IntoIter) -> Option<String> {
if let Some(TokenTree::Literal(literal)) = it.next() {
Some(literal.to_string())
} else {
None
}
}

pub fn try_byte_string(it: &mut token_stream::IntoIter) -> Option<String> {
pub(crate) fn try_byte_string(it: &mut token_stream::IntoIter) -> Option<String> {
try_literal(it).and_then(|byte_string| {
if byte_string.starts_with("b\"") && byte_string.ends_with('\"') {
Some(byte_string[2..byte_string.len() - 1].to_string())
Expand All @@ -28,49 +28,49 @@ pub fn try_byte_string(it: &mut token_stream::IntoIter) -> Option<String> {
})
}

pub fn expect_ident(it: &mut token_stream::IntoIter) -> String {
pub(crate) fn expect_ident(it: &mut token_stream::IntoIter) -> String {
try_ident(it).expect("Expected Ident")
}

pub fn expect_punct(it: &mut token_stream::IntoIter) -> char {
pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> char {
if let TokenTree::Punct(punct) = it.next().expect("Reached end of token stream for Punct") {
punct.as_char()
} else {
panic!("Expected Punct");
}
}

pub fn expect_literal(it: &mut token_stream::IntoIter) -> String {
pub(crate) fn expect_literal(it: &mut token_stream::IntoIter) -> String {
try_literal(it).expect("Expected Literal")
}

pub fn expect_group(it: &mut token_stream::IntoIter) -> Group {
pub(crate) fn expect_group(it: &mut token_stream::IntoIter) -> Group {
if let TokenTree::Group(group) = it.next().expect("Reached end of token stream for Group") {
group
} else {
panic!("Expected Group");
}
}

pub fn expect_byte_string(it: &mut token_stream::IntoIter) -> String {
pub(crate) fn expect_byte_string(it: &mut token_stream::IntoIter) -> String {
try_byte_string(it).expect("Expected byte string")
}

pub fn expect_end(it: &mut token_stream::IntoIter) {
pub(crate) fn expect_end(it: &mut token_stream::IntoIter) {
if it.next().is_some() {
panic!("Expected end");
}
}

pub fn get_literal(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
pub(crate) fn get_literal(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
assert_eq!(expect_ident(it), expected_name);
assert_eq!(expect_punct(it), ':');
let literal = expect_literal(it);
assert_eq!(expect_punct(it), ',');
literal
}

pub fn get_byte_string(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
pub(crate) fn get_byte_string(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
assert_eq!(expect_ident(it), expected_name);
assert_eq!(expect_punct(it), ':');
let byte_string = expect_byte_string(it);
Expand Down
2 changes: 1 addition & 1 deletion rust/macros/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl ModuleInfo {
}
}

pub fn module(ts: TokenStream) -> TokenStream {
pub(crate) fn module(ts: TokenStream) -> TokenStream {
let mut it = ts.into_iter();

let info = ModuleInfo::parse(&mut it);
Expand Down