diff mbox

[ovs-dev,PATCHv2] checkpatch: Enforce bracing around conditionals.

Message ID 20170817212627.3378-1-joe@ovn.org
State Accepted
Headers show

Commit Message

Joe Stringer Aug. 17, 2017, 9:26 p.m. UTC
The coding style states that BSD-style brace placement should be used,
and even single statements should be enclosed. Add checks to checkpatch
for this, particularly for 'else' statements.

Signed-off-by: Joe Stringer <joe@ovn.org>
---
v2: Combine in same check as if_and_for_end_with_bracket_check
---
 utilities/checkpatch.py | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Aaron Conole Aug. 21, 2017, 1:31 p.m. UTC | #1
Joe Stringer <joe@ovn.org> writes:

> The coding style states that BSD-style brace placement should be used,
> and even single statements should be enclosed. Add checks to checkpatch
> for this, particularly for 'else' statements.
>
> Signed-off-by: Joe Stringer <joe@ovn.org>
> ---

Acked-by: Aaron Conole <aconole@redhat.com>


Interestingly - if I do:

  $ find lib/ -name \*.c -exec ./utilities/checkpatch.py -f {} \; | \
         grep bracing | wc -l

before this patch: 92 instances of 'Inappropriate bracing'
after this patch: 102 instances of 'Inappropriate bracing'

-Aaron
Joe Stringer Aug. 21, 2017, 6:44 p.m. UTC | #2
On 21 August 2017 at 06:31, Aaron Conole <aconole@redhat.com> wrote:
> Joe Stringer <joe@ovn.org> writes:
>
>> The coding style states that BSD-style brace placement should be used,
>> and even single statements should be enclosed. Add checks to checkpatch
>> for this, particularly for 'else' statements.
>>
>> Signed-off-by: Joe Stringer <joe@ovn.org>
>> ---
>
> Acked-by: Aaron Conole <aconole@redhat.com>

Thanks, applied to master.

> Interestingly - if I do:
>
>   $ find lib/ -name \*.c -exec ./utilities/checkpatch.py -f {} \; | \
>          grep bracing | wc -l
>
> before this patch: 92 instances of 'Inappropriate bracing'
> after this patch: 102 instances of 'Inappropriate bracing'

Looks like a few may have slipped through the gaps in the pats. I
guess checkpatch.py is new enough that we haven't swept through the
repo and fixed the complaints yet.
diff mbox

Patch

diff --git a/utilities/checkpatch.py b/utilities/checkpatch.py
index 43f10bb3ded3..185ddaf0d5e9 100755
--- a/utilities/checkpatch.py
+++ b/utilities/checkpatch.py
@@ -95,6 +95,8 @@  __regex_ends_with_bracket = \
 __regex_ptr_declaration_missing_whitespace = re.compile(r'[a-zA-Z0-9]\*[^*]')
 __regex_is_comment_line = re.compile(r'^\s*(/\*|\*\s)')
 __regex_trailing_operator = re.compile(r'^[^ ]* [^ ]*[?:]$')
+__regex_conditional_else_bracing = re.compile(r'^\s*else\s*{?$')
+__regex_conditional_else_bracing2 = re.compile(r'^\s*}\selse\s*$')
 
 skip_leading_whitespace_check = False
 skip_trailing_whitespace_check = False
@@ -186,6 +188,10 @@  def if_and_for_end_with_bracket_check(line):
             return True
         if __regex_ends_with_bracket.search(line) is None:
             return False
+    if __regex_conditional_else_bracing.match(line) is not None:
+        return False
+    if __regex_conditional_else_bracing2.match(line) is not None:
+        return False
     return True