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

Joe Stringer joe at ovn.org
Wed Aug 16 23:01:26 UTC 2017


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.

Signed-off-by: Joe Stringer <joe at ovn.org>
---
 utilities/checkpatch.py | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/utilities/checkpatch.py b/utilities/checkpatch.py
index 43f10bb3ded3..c8381c98403b 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
@@ -212,6 +214,12 @@ def trailing_operator(line):
     return __regex_trailing_operator.match(line) is not None
 
 
+def if_else_bracing_check(line):
+    if __regex_conditional_else_bracing.match(line) is not None:
+        return True
+    return __regex_conditional_else_bracing2.match(line) is not None
+
+
 checks = [
     {'regex': None,
      'match_name':
@@ -250,6 +258,11 @@ checks = [
      'check': lambda x: trailing_operator(x),
      'print':
      lambda: print_error("Line has '?' or ':' operator at end of line")},
+
+    {'regex': '(.c|.h)(.in)?$', 'match_name': None,
+     'prereq': lambda x: not is_comment_line(x),
+     'check': lambda x: if_else_bracing_check(x),
+     'print': lambda: print_error("Improper bracing in conditional statement")}
 ]
 
 
-- 
2.14.1



More information about the dev mailing list