Skip to content

Commit 139d396

Browse files
committed
Documentation: rust: add coding guidelines on lints
In the C side, disabling diagnostics locally, i.e. within the source code, is rare (at least in the kernel). Sometimes warnings are manipulated via the flags at the translation unit level, but that is about it. In Rust, it is easier to change locally the "level" of lints (e.g. allowing them locally). In turn, this means it is easier to globally enable more lints that may trigger a few false positives here and there that need to be allowed locally, but that generally can spot issues or bugs. Thus document this. Reviewed-by: Trevor Gross <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Tested-by: Gary Guo <[email protected]> Reviewed-by: Gary Guo <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 624063b commit 139d396

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Documentation/rust/coding-guidelines.rst

+38
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,41 @@ The equivalent in Rust may look like (ignoring documentation):
227227
That is, the equivalent of ``GPIO_LINE_DIRECTION_IN`` would be referred to as
228228
``gpio::LineDirection::In``. In particular, it should not be named
229229
``gpio::gpio_line_direction::GPIO_LINE_DIRECTION_IN``.
230+
231+
232+
Lints
233+
-----
234+
235+
In Rust, it is possible to ``allow`` particular warnings (diagnostics, lints)
236+
locally, making the compiler ignore instances of a given warning within a given
237+
function, module, block, etc.
238+
239+
It is similar to ``#pragma GCC diagnostic push`` + ``ignored`` + ``pop`` in C
240+
[#]_:
241+
242+
.. code-block:: c
243+
244+
#pragma GCC diagnostic push
245+
#pragma GCC diagnostic ignored "-Wunused-function"
246+
static void f(void) {}
247+
#pragma GCC diagnostic pop
248+
249+
.. [#] In this particular case, the kernel's ``__{always,maybe}_unused``
250+
attributes (C23's ``[[maybe_unused]]``) may be used; however, the example
251+
is meant to reflect the equivalent lint in Rust discussed afterwards.
252+
253+
But way less verbose:
254+
255+
.. code-block:: rust
256+
257+
#[allow(dead_code)]
258+
fn f() {}
259+
260+
By that virtue, it makes it possible to comfortably enable more diagnostics by
261+
default (i.e. outside ``W=`` levels). In particular, those that may have some
262+
false positives but that are otherwise quite useful to keep enabled to catch
263+
potential mistakes.
264+
265+
For more information about diagnostics in Rust, please see:
266+
267+
https://doc.rust-lang.org/stable/reference/attributes/diagnostics.html

0 commit comments

Comments
 (0)