Skip to content

Commit 1c64efc

Browse files
committed
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
2 parents 648937f + 1f1d3e1 commit 1c64efc

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

Documentation/rust/coding-guidelines.rst

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,81 @@ Like ``clang-format`` for the rest of the kernel, ``rustfmt`` works on
3838
individual files, and does not require a kernel configuration. Sometimes it may
3939
even work with broken code.
4040

41+
Imports
42+
~~~~~~~
43+
44+
``rustfmt``, by default, formats imports in a way that is prone to conflicts
45+
while merging and rebasing, since in some cases it condenses several items into
46+
the 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+
58+
Instead, 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+
74+
That is, each item goes into its own line, and braces are used as soon as there
75+
is more than one item in a list.
76+
77+
The trailing empty comment allows to preserve this formatting. Not only that,
78+
``rustfmt`` will actually reformat imports vertically when the empty comment is
79+
added. That is, it is possible to easily reformat the original example into the
80+
expected 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+
93+
The trailing empty comment works for nested imports, as shown above, as well as
94+
for single item imports -- this can be useful to minimize diffs within patch
95+
series:
96+
97+
.. code-block:: rust
98+
99+
use crate::{
100+
example1, //
101+
};
102+
103+
The trailing empty comment works in any of the lines within the braces, but it
104+
is preferred to keep it in the last item, since it is reminiscent of the
105+
trailing comma in other formatters. Sometimes it may be simpler to avoid moving
106+
the comment several times within a patch series due to changes in the list.
107+
108+
There may be cases where exceptions may need to be made, i.e. none of this is
109+
a hard rule. There is also code that is not migrated to this style yet, but
110+
please do not introduce code in other styles.
111+
112+
Eventually, the goal is to get ``rustfmt`` to support this formatting style (or
113+
a similar one) automatically in a stable release without requiring the trailing
114+
empty comment. Thus, at some point, the goal is to remove those comments.
115+
41116

42117
Comments
43118
--------

rust/kernel/alloc/kvec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::{
99
};
1010
use crate::{
1111
fmt,
12-
page::AsPageIter,
12+
page::AsPageIter, //
1313
};
1414
use core::{
1515
borrow::{Borrow, BorrowMut},

rust/kernel/bitmap.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ impl core::ops::Deref for BitmapVec {
167167
let ptr = if self.nbits <= BITS_PER_LONG {
168168
// SAFETY: Bitmap is represented inline.
169169
#[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+
}
171173
} else {
172174
// SAFETY: Bitmap is represented as array of `unsigned long`.
173175
unsafe { self.repr.ptr.as_ptr() }
@@ -184,7 +186,9 @@ impl core::ops::DerefMut for BitmapVec {
184186
let ptr = if self.nbits <= BITS_PER_LONG {
185187
// SAFETY: Bitmap is represented inline.
186188
#[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+
}
188192
} else {
189193
// SAFETY: Bitmap is represented as array of `unsigned long`.
190194
unsafe { self.repr.ptr.as_ptr() }

rust/kernel/cpufreq.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ use macros::vtable;
3838
const CPUFREQ_NAME_LEN: usize = bindings::CPUFREQ_NAME_LEN as usize;
3939

4040
/// Default transition latency value in nanoseconds.
41-
pub const DEFAULT_TRANSITION_LATENCY_NS: u32 =
42-
bindings::CPUFREQ_DEFAULT_TRANSITION_LATENCY_NS;
41+
pub const DEFAULT_TRANSITION_LATENCY_NS: u32 = bindings::CPUFREQ_DEFAULT_TRANSITION_LATENCY_NS;
4342

4443
/// CPU frequency driver flags.
4544
pub mod flags {

0 commit comments

Comments
 (0)