History

Merge tag 'rust-rustfmt' of git://git.kernel.org/pub/scm/linux/kernel/git/ojeda/linux

Pull rustfmt fixes from Miguel Ojeda: "Rust 'rustfmt' cleanup 'rustfmt', by default, formats imports in a way that is prone to conflicts while merging and rebasing, since in some cases it condenses several items into the same line. Document in our guidelines that we will handle this for the moment with the trailing empty comment workaround and make the tree 'rustfmt'-clean again" * tag 'rust-rustfmt' of git://git.kernel.org/pub/scm/linux/kernel/git/ojeda/linux: rust: bitmap: fix formatting rust: cpufreq: fix formatting rust: alloc: employ a trailing comment to keep vertical layout docs: rust: add section on imports formatting

Linus Torvalds committed 1 year ago

Commit 1c64efc2 parents 648937f 1f1d3e14 files · +83 −5Browse files

Unified · Split

4 files
  1. Documentation/rust/coding-guidelines.rst +75 −0
  2. rust/kernel/alloc/kvec.rs +1 −1
  3. rust/kernel/bitmap.rs +6 −2
  4. rust/kernel/cpufreq.rs +1 −2
Documentation/rust/coding-guidelines.rst+75−0
@@ -38,6 +38,81 @@ Like ``clang-format`` for the rest of the kernel, ``rustfmt`` works on · 37 unmodified lines
38individual files, and does not require a kernel configuration. Sometimes it may38individual files, and does not require a kernel configuration. Sometimes it may
39even work with broken code.39even work with broken code.
40 40
41Imports
42~~~~~~~
43
44``rustfmt``, by default, formats imports in a way that is prone to conflicts
45while merging and rebasing, since in some cases it condenses several items into
46the same line. For instance:
47
48.. code-block:: rust
49
50 // Do not use this style.
51 use crate::{
52 example1,
53 example2::{example3, example4, example5},
54 example6, example7,
55 example8::example9,
56 };
57
58Instead, the kernel uses a vertical layout that looks like this:
59
60.. code-block:: rust
61
62 use crate::{
63 example1,
64 example2::{
65 example3,
66 example4,
67 example5, //
68 },
69 example6,
70 example7,
71 example8::example9, //
72 };
73
74That is, each item goes into its own line, and braces are used as soon as there
75is more than one item in a list.
76
77The trailing empty comment allows to preserve this formatting. Not only that,
78``rustfmt`` will actually reformat imports vertically when the empty comment is
79added. That is, it is possible to easily reformat the original example into the
80expected style by running ``rustfmt`` on an input like:
81
82.. code-block:: rust
83
84 // Do not use this style.
85 use crate::{
86 example1,
87 example2::{example3, example4, example5, //
88 },
89 example6, example7,
90 example8::example9, //
91 };
92
93The trailing empty comment works for nested imports, as shown above, as well as
94for single item imports -- this can be useful to minimize diffs within patch
95series:
96
97.. code-block:: rust
98
99 use crate::{
100 example1, //
101 };
102
103The trailing empty comment works in any of the lines within the braces, but it
104is preferred to keep it in the last item, since it is reminiscent of the
105trailing comma in other formatters. Sometimes it may be simpler to avoid moving
106the comment several times within a patch series due to changes in the list.
107
108There may be cases where exceptions may need to be made, i.e. none of this is
109a hard rule. There is also code that is not migrated to this style yet, but
110please do not introduce code in other styles.
111
112Eventually, the goal is to get ``rustfmt`` to support this formatting style (or
113a similar one) automatically in a stable release without requiring the trailing
114empty comment. Thus, at some point, the goal is to remove those comments.
115
41 116
42Comments117Comments
43--------118--------
rust/kernel/alloc/kvec.rs+1−1
@@ -9,7 +9,7 @@ use super::{ · 8 unmodified lines
9};9};
10use crate::{10use crate::{
11 fmt,11 fmt,
12 page::AsPageIter,12 page::AsPageIter, //
13};13};
14use core::{14use core::{
15 borrow::{Borrow, BorrowMut},15 borrow::{Borrow, BorrowMut},
rust/kernel/bitmap.rs+6−2
@@ -167,7 +167,9 @@ impl core::ops::Deref for BitmapVec { · 166 unmodified lines
167 let ptr = if self.nbits <= BITS_PER_LONG {167 let ptr = if self.nbits <= BITS_PER_LONG {
168 // SAFETY: Bitmap is represented inline.168 // SAFETY: Bitmap is represented inline.
169 #[allow(unused_unsafe, reason = "Safe since Rust 1.92.0")]169 #[allow(unused_unsafe, reason = "Safe since Rust 1.92.0")]
170 unsafe { core::ptr::addr_of!(self.repr.bitmap) }170 unsafe {
171 core::ptr::addr_of!(self.repr.bitmap)
172 }
171 } else {173 } else {
172 // SAFETY: Bitmap is represented as array of `unsigned long`.174 // SAFETY: Bitmap is represented as array of `unsigned long`.
173 unsafe { self.repr.ptr.as_ptr() }175 unsafe { self.repr.ptr.as_ptr() }
@@ -184,7 +186,9 @@ impl core::ops::DerefMut for BitmapVec { · 10 unmodified lines
184 let ptr = if self.nbits <= BITS_PER_LONG {186 let ptr = if self.nbits <= BITS_PER_LONG {
185 // SAFETY: Bitmap is represented inline.187 // SAFETY: Bitmap is represented inline.
186 #[allow(unused_unsafe, reason = "Safe since Rust 1.92.0")]188 #[allow(unused_unsafe, reason = "Safe since Rust 1.92.0")]
187 unsafe { core::ptr::addr_of_mut!(self.repr.bitmap) }189 unsafe {
190 core::ptr::addr_of_mut!(self.repr.bitmap)
191 }
188 } else {192 } else {
189 // SAFETY: Bitmap is represented as array of `unsigned long`.193 // SAFETY: Bitmap is represented as array of `unsigned long`.
190 unsafe { self.repr.ptr.as_ptr() }194 unsafe { self.repr.ptr.as_ptr() }
rust/kernel/cpufreq.rs+1−2
@@ -38,8 +38,7 @@ use macros::vtable; · 37 unmodified lines
38const CPUFREQ_NAME_LEN: usize = bindings::CPUFREQ_NAME_LEN as usize;38const CPUFREQ_NAME_LEN: usize = bindings::CPUFREQ_NAME_LEN as usize;
39 39
40/// Default transition latency value in nanoseconds.40/// Default transition latency value in nanoseconds.
41pub const DEFAULT_TRANSITION_LATENCY_NS: u32 =41pub const DEFAULT_TRANSITION_LATENCY_NS: u32 = bindings::CPUFREQ_DEFAULT_TRANSITION_LATENCY_NS;
42 bindings::CPUFREQ_DEFAULT_TRANSITION_LATENCY_NS;
43 42
44/// CPU frequency driver flags.43/// CPU frequency driver flags.
45pub mod flags {44pub mod flags {