Skip to content

Commit 795c733

Browse files
authored
Merge pull request #566 from ojeda/lints
Enable some lints and simplify `CLIPPY=1`
2 parents 50fb815 + a8c7ecf commit 795c733

17 files changed

+54
-41
lines changed

Makefile

+9-4
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,14 @@ KBUILD_RUSTFLAGS := --emit=dep-info,obj,metadata --edition=2021 \
538538
-Cforce-unwind-tables=n -Ccodegen-units=1 \
539539
-Zbinary_dep_depinfo=y -Zsymbol-mangling-version=v0 \
540540
-Dunsafe_op_in_unsafe_fn -Drust_2018_idioms \
541-
-Wmissing_docs
542-
KBUILD_CLIPPYFLAGS := -Dclippy::correctness -Dclippy::style \
543-
-Dclippy::complexity -Dclippy::perf -Dclippy::float_arithmetic
541+
-Dunreachable_pub -Dnon_ascii_idents \
542+
-Wmissing_docs \
543+
-Drustdoc::missing_crate_level_docs \
544+
-Dclippy::correctness -Dclippy::style -Dclippy::suspicious \
545+
-Dclippy::complexity -Dclippy::perf -Dclippy::float_arithmetic \
546+
-Dclippy::let_unit_value -Dclippy::mut_mut \
547+
-Dclippy::needless_bitwise_bool -Dclippy::needless_continue \
548+
-Wclippy::dbg_macro
544549
KBUILD_AFLAGS_KERNEL :=
545550
KBUILD_CFLAGS_KERNEL :=
546551
KBUILD_RUSTFLAGS_KERNEL :=
@@ -553,7 +558,7 @@ CLANG_FLAGS :=
553558

554559
ifeq ($(KBUILD_CLIPPY),1)
555560
RUSTC_OR_CLIPPY_QUIET := CLIPPY
556-
RUSTC_OR_CLIPPY = $(CLIPPY_DRIVER) $(KBUILD_CLIPPYFLAGS)
561+
RUSTC_OR_CLIPPY = $(CLIPPY_DRIVER)
557562
else
558563
RUSTC_OR_CLIPPY_QUIET := RUSTC
559564
RUSTC_OR_CLIPPY = $(RUSTC)

drivers/android/allocation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ impl<'a, 'b> AllocationView<'a, 'b> {
156156
AllocationView { alloc, limit }
157157
}
158158

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

166-
pub fn write<T>(&self, offset: usize, obj: &T) -> Result {
166+
pub(crate) fn write<T>(&self, offset: usize, obj: &T) -> Result {
167167
if offset.checked_add(size_of::<T>()).ok_or(Error::EINVAL)? > self.limit {
168168
return Err(Error::EINVAL);
169169
}

drivers/android/defs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use kernel::{
99

1010
macro_rules! pub_no_prefix {
1111
($prefix:ident, $($newname:ident),+) => {
12-
$(pub const $newname: u32 = concat_idents!($prefix, $newname);)+
12+
$(pub(crate) const $newname: u32 = concat_idents!($prefix, $newname);)+
1313
};
1414
}
1515

drivers/android/node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ impl DeliverToRead for Node {
395395
}
396396
}
397397

398-
pub struct NodeRef {
398+
pub(crate) struct NodeRef {
399399
pub(crate) node: Ref<Node>,
400400
strong_count: usize,
401401
weak_count: usize,

rust/Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
303303
cmd_rustc_library = \
304304
OBJTREE=$(abspath $(objtree)) \
305305
$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
306-
$(rust_flags) $(rust_cross_flags) $(rustc_target_flags) \
306+
$(filter-out $(skip_flags),$(rust_flags) $(rust_cross_flags) $(rustc_target_flags)) \
307307
--crate-type rlib --out-dir $(objtree)/rust/ -L $(objtree)/rust/ \
308308
--crate-name $(patsubst %.o,%,$(notdir $@)) $<; \
309309
mv $(objtree)/rust/$(patsubst %.o,%,$(notdir $@)).d $(depfile); \
@@ -324,6 +324,7 @@ $(objtree)/rust/compiler_builtins.o: $(srctree)/rust/compiler_builtins.rs \
324324
$(call if_changed_dep,rustc_library)
325325

326326
$(objtree)/rust/alloc.o: private skip_clippy = 1
327+
$(objtree)/rust/alloc.o: private skip_flags = -Dunreachable_pub
327328
$(objtree)/rust/alloc.o: private rustc_target_flags = $(alloc-cfgs)
328329
$(objtree)/rust/alloc.o: $(srctree)/rust/alloc/lib.rs \
329330
$(objtree)/rust/compiler_builtins.o FORCE
@@ -346,6 +347,7 @@ $(objtree)/rust/kernel.o: $(srctree)/rust/kernel/lib.rs $(objtree)/rust/alloc.o
346347
# Targets that need to expand twice
347348
.SECONDEXPANSION:
348349
$(objtree)/rust/core.o: private skip_clippy = 1
350+
$(objtree)/rust/core.o: private skip_flags = -Dunreachable_pub
349351
$(objtree)/rust/core.o: private rustc_target_flags = $(core-cfgs)
350352
$(objtree)/rust/core.o: $$(RUST_LIB_SRC)/core/src/lib.rs FORCE
351353
$(call if_changed_dep,rustc_library)

rust/kernel/allocator.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::ptr;
88
use crate::bindings;
99
use crate::c_types;
1010

11-
pub struct KernelAllocator;
11+
struct KernelAllocator;
1212

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

3840
#[no_mangle]
39-
pub fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
41+
fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
4042
unsafe { bindings::kfree(ptr as *const c_types::c_void) };
4143
}
4244

4345
#[no_mangle]
44-
pub fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: usize) -> *mut u8 {
46+
fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: usize) -> *mut u8 {
4547
unsafe {
4648
bindings::krealloc(
4749
ptr as *const c_types::c_void,
@@ -52,7 +54,7 @@ pub fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: u
5254
}
5355

5456
#[no_mangle]
55-
pub fn __rust_alloc_zeroed(size: usize, _align: usize) -> *mut u8 {
57+
fn __rust_alloc_zeroed(size: usize, _align: usize) -> *mut u8 {
5658
unsafe {
5759
bindings::krealloc(
5860
core::ptr::null(),

rust/kernel/amba.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//! C header: [`include/linux/amba/bus.h`](../../../../include/linux/amba/bus.h)
66
77
use crate::{
8-
bindings, c_types, device, driver, from_kernel_result, io_mem::Resource, power, str::CStr,
9-
to_result, types::PointerWrapper, Error, Result,
8+
bindings, c_types, device, driver, error::from_kernel_result, io_mem::Resource, power,
9+
str::CStr, to_result, types::PointerWrapper, Error, Result,
1010
};
1111
use core::{marker::PhantomData, ops::Deref};
1212

rust/kernel/bindings.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
non_upper_case_globals,
1515
non_snake_case,
1616
improper_ctypes,
17+
unreachable_pub,
1718
unsafe_op_in_unsafe_fn
1819
)]
1920

rust/kernel/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,7 @@ impl From<AllocError> for Error {
427427
// # Invariant: `-bindings::MAX_ERRNO` fits in an `i16`.
428428
crate::static_assert!(bindings::MAX_ERRNO <= -(i16::MIN as i32) as u32);
429429

430-
#[doc(hidden)]
431-
pub fn from_kernel_result_helper<T>(r: Result<T>) -> T
430+
pub(crate) fn from_kernel_result_helper<T>(r: Result<T>) -> T
432431
where
433432
T: From<i16>,
434433
{
@@ -465,7 +464,6 @@ where
465464
/// }
466465
/// }
467466
/// ```
468-
#[macro_export]
469467
macro_rules! from_kernel_result {
470468
($($tt:tt)*) => {{
471469
$crate::error::from_kernel_result_helper((|| {
@@ -474,6 +472,8 @@ macro_rules! from_kernel_result {
474472
}};
475473
}
476474

475+
pub(crate) use from_kernel_result;
476+
477477
/// Transform a kernel "error pointer" to a normal pointer.
478478
///
479479
/// Some kernel C API functions return an "error pointer" which optionally

rust/kernel/file_operations.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ use alloc::boxed::Box;
1111

1212
use crate::{
1313
bindings, c_types,
14-
error::{Error, Result},
14+
error::{from_kernel_result, Error, Result},
1515
file::{File, FileRef},
16-
from_kernel_result,
1716
io_buffer::{IoBufferReader, IoBufferWriter},
1817
iov_iter::IovIter,
1918
sync::CondVar,

rust/kernel/gpio.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
//!
55
//! C header: [`include/linux/gpio/driver.h`](../../../../include/linux/gpio/driver.h)
66
7-
use crate::{bindings, c_types, device, from_kernel_result, types::PointerWrapper, Error, Result};
7+
use crate::{
8+
bindings, c_types, device, error::from_kernel_result, types::PointerWrapper, Error, Result,
9+
};
810
use core::{
911
cell::UnsafeCell,
1012
marker::{PhantomData, PhantomPinned},

rust/kernel/irq.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
#![allow(dead_code)]
1111

12-
use crate::{bindings, c_types, from_kernel_result, types::PointerWrapper, Error, Result};
12+
use crate::{bindings, c_types, error::from_kernel_result, types::PointerWrapper, Error, Result};
1313
use core::ops::Deref;
1414

1515
type IrqHwNumber = bindings::irq_hw_number_t;

rust/kernel/platdev.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
99
use crate::{
1010
bindings, c_types,
11-
error::{Error, Result},
12-
from_kernel_result,
11+
error::{from_kernel_result, Error, Result},
1312
of::OfMatchTable,
1413
str::CStr,
1514
types::PointerWrapper,

rust/kernel/power.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
#![allow(dead_code)]
88

9-
use crate::{bindings, c_types, from_kernel_result, types::PointerWrapper, Result};
9+
use crate::{bindings, c_types, error::from_kernel_result, types::PointerWrapper, Result};
1010
use core::marker::PhantomData;
1111

1212
/// Corresponds to the kernel's `struct dev_pm_ops`.

rust/kernel/sync/mutex.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,20 @@ impl<T> CreatableLock for Mutex<T> {
8686
}
8787
}
8888

89+
pub struct EmptyGuardContext;
90+
8991
// SAFETY: The underlying kernel `struct mutex` object ensures mutual exclusion.
9092
unsafe impl<T: ?Sized> Lock for Mutex<T> {
9193
type Inner = T;
92-
type GuardContext = ();
94+
type GuardContext = EmptyGuardContext;
9395

94-
fn lock_noguard(&self) {
96+
fn lock_noguard(&self) -> EmptyGuardContext {
9597
// SAFETY: `mutex` points to valid memory.
9698
unsafe { bindings::mutex_lock(self.mutex.get()) };
99+
EmptyGuardContext
97100
}
98101

99-
unsafe fn unlock(&self, _: &mut ()) {
102+
unsafe fn unlock(&self, _: &mut EmptyGuardContext) {
100103
// SAFETY: The safety requirements of the function ensure that the mutex is owned by the
101104
// caller.
102105
unsafe { bindings::mutex_unlock(self.mutex.get()) };

rust/macros/helpers.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

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

5-
pub fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
5+
pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
66
if let Some(TokenTree::Ident(ident)) = it.next() {
77
Some(ident.to_string())
88
} else {
99
None
1010
}
1111
}
1212

13-
pub fn try_literal(it: &mut token_stream::IntoIter) -> Option<String> {
13+
pub(crate) fn try_literal(it: &mut token_stream::IntoIter) -> Option<String> {
1414
if let Some(TokenTree::Literal(literal)) = it.next() {
1515
Some(literal.to_string())
1616
} else {
1717
None
1818
}
1919
}
2020

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

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

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

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

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

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

59-
pub fn expect_end(it: &mut token_stream::IntoIter) {
59+
pub(crate) fn expect_end(it: &mut token_stream::IntoIter) {
6060
if it.next().is_some() {
6161
panic!("Expected end");
6262
}
6363
}
6464

65-
pub fn get_literal(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
65+
pub(crate) fn get_literal(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
6666
assert_eq!(expect_ident(it), expected_name);
6767
assert_eq!(expect_punct(it), ':');
6868
let literal = expect_literal(it);
6969
assert_eq!(expect_punct(it), ',');
7070
literal
7171
}
7272

73-
pub fn get_byte_string(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
73+
pub(crate) fn get_byte_string(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
7474
assert_eq!(expect_ident(it), expected_name);
7575
assert_eq!(expect_punct(it), ':');
7676
let byte_string = expect_byte_string(it);

rust/macros/module.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl ModuleInfo {
303303
}
304304
}
305305

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

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

0 commit comments

Comments
 (0)