From 250a0a5ba9d2cfbdb7c77b421ff1e03d630bea46 Mon Sep 17 00:00:00 2001 From: Utkarsh Verma Date: Sat, 25 Sep 2021 22:08:52 +0530 Subject: [PATCH] docs: checkpatch: add multiline, do/while, and multiple-assignment messages Added and documented 3 new message types: - MULTILINE_DEREFERENCE - SINGLE_STATEMENT_DO_WHILE_MACRO - MULTIPLE_ASSIGNMENTS Signed-off-by: Utkarsh Verma Link: https://lore.kernel.org/r/20210925163852.12096-1-utkarshverma294@gmail.com Signed-off-by: Jonathan Corbet --- Documentation/dev-tools/checkpatch.rst | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Documentation/dev-tools/checkpatch.rst b/Documentation/dev-tools/checkpatch.rst index 4f89c36..5cbc846 100644 --- a/Documentation/dev-tools/checkpatch.rst +++ b/Documentation/dev-tools/checkpatch.rst @@ -716,6 +716,33 @@ Indentation and Line Breaks See: https://lore.kernel.org/lkml/20120203052727.GA15035@leaf/ + **MULTILINE_DEREFERENCE** + A single dereferencing identifier spanned on multiple lines like:: + + struct_identifier->member[index]. + member = ; + + is generally hard to follow. It can easily lead to typos and so makes + the code vulnerable to bugs. + + If fixing the multiple line dereferencing leads to an 80 column + violation, then either rewrite the code in a more simple way or if the + starting part of the dereferencing identifier is the same and used at + multiple places then store it in a temporary variable, and use that + temporary variable only at all the places. For example, if there are + two dereferencing identifiers:: + + member1->member2->member3.foo1; + member1->member2->member3.foo2; + + then store the member1->member2->member3 part in a temporary variable. + It not only helps to avoid the 80 column violation but also reduces + the program size by removing the unnecessary dereferences. + + But if none of the above methods work then ignore the 80 column + violation because it is much easier to read a dereferencing identifier + on a single line. + **TRAILING_STATEMENTS** Trailing statements (for example after any conditional) should be on the next line. @@ -872,6 +899,17 @@ Macros, Attributes and Symbols See: https://lore.kernel.org/lkml/1399671106.2912.21.camel@joe-AO725/ + **SINGLE_STATEMENT_DO_WHILE_MACRO** + For the multi-statement macros, it is necessary to use the do-while + loop to avoid unpredictable code paths. The do-while loop helps to + group the multiple statements into a single one so that a + function-like macro can be used as a function only. + + But for the single statement macros, it is unnecessary to use the + do-while loop. Although the code is syntactically correct but using + the do-while loop is redundant. So remove the do-while loop for single + statement macros. + **WEAK_DECLARATION** Using weak declarations like __attribute__((weak)) or __weak can have unintended link defects. Avoid using them. @@ -947,6 +985,11 @@ Functions and Variables Your compiler (or rather your loader) automatically does it for you. + **MULTIPLE_ASSIGNMENTS** + Multiple assignments on a single line makes the code unnecessarily + complicated. So on a single line assign value to a single variable + only, this makes the code more readable and helps avoid typos. + **RETURN_PARENTHESES** return is not a function and as such doesn't need parentheses:: -- 2.7.4