diff mbox series

[ovs-dev] checkpatch: Add check for a whitespace after cast.

Message ID 20201118211858.1362812-1-i.maximets@ovn.org
State New
Headers show
Series [ovs-dev] checkpatch: Add check for a whitespace after cast. | expand

Commit Message

Ilya Maximets Nov. 18, 2020, 9:18 p.m. UTC
Coding style says: "Put a space between the ``()`` used in a cast and
the expression whose type is cast: ``(void *) 0``.".
This style rule is frequently overlooked.  Let's check for it.

Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
---
 tests/checkpatch.at     | 17 +++++++++++++++++
 utilities/checkpatch.py | 13 +++++++++++++
 2 files changed, 30 insertions(+)

Comments

Ian Stokes Nov. 19, 2020, 9:51 a.m. UTC | #1
> Coding style says: "Put a space between the ``()`` used in a cast and
> the expression whose type is cast: ``(void *) 0``.".
> This style rule is frequently overlooked.  Let's check for it.
> 
> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>

Hi Ilya,

Thanks for the patch, it's a nice addition to checkpatch, tested on myside and works as expected, LGTM.

Acked-by: Ian Stokes <ian.stokes@intel.com>

Regards
Ian
diff mbox series

Patch

diff --git a/tests/checkpatch.at b/tests/checkpatch.at
index 6c7394772..a51e46e7a 100755
--- a/tests/checkpatch.at
+++ b/tests/checkpatch.at
@@ -326,3 +326,20 @@  try_checkpatch \
 "
 
 AT_CLEANUP
+
+AT_SETUP([checkpatch - whitespace around cast])
+try_checkpatch \
+   "COMMON_PATCH_HEADER
+    +     (int) a;
+    "
+
+try_checkpatch \
+   "COMMON_PATCH_HEADER
+    +     (int)a;
+    " \
+    "ERROR: Inappropriate spacing around cast
+    #8 FILE: A.c:1:
+         (int)a;
+"
+
+AT_CLEANUP
diff --git a/utilities/checkpatch.py b/utilities/checkpatch.py
index ed231fa6f..681de12ce 100755
--- a/utilities/checkpatch.py
+++ b/utilities/checkpatch.py
@@ -167,6 +167,7 @@  __regex_is_for_if_single_line_bracket = \
 __regex_ends_with_bracket = \
     re.compile(r'[^\s]\) {(\s+/\*[\s\Sa-zA-Z0-9\.,\?\*/+-]*)?$')
 __regex_ptr_declaration_missing_whitespace = re.compile(r'[a-zA-Z0-9]\*[^*]')
+__regex_cast_missing_whitespace = re.compile(r'\)[a-zA-Z0-9]')
 __regex_is_comment_line = re.compile(r'^\s*(/\*|\*\s)')
 __regex_has_comment = re.compile(r'.*(/\*|\*\s)')
 __regex_has_c99_comment = re.compile(r'.*//.*$')
@@ -286,6 +287,12 @@  def pointer_whitespace_check(line):
     return __regex_ptr_declaration_missing_whitespace.search(line) is not None
 
 
+def cast_whitespace_check(line):
+    """Return TRUE if there is no space between the '()' used in a cast and
+       the expression whose type is cast, i.e.: '(void *)foo'"""
+    return __regex_cast_missing_whitespace.search(line) is not None
+
+
 def line_length_check(line):
     """Return TRUE if the line length is too long"""
     if len(line) > 79:
@@ -551,6 +558,12 @@  checks = [
      'print':
      lambda: print_error("Inappropriate spacing in pointer declaration")},
 
+    {'regex': r'(\.c|\.h)(\.in)?$', 'match_name': None,
+     'prereq': lambda x: not is_comment_line(x),
+     'check': lambda x: cast_whitespace_check(x),
+     'print':
+     lambda: print_error("Inappropriate spacing around cast")},
+
     {'regex': r'(\.c|\.h)(\.in)?$', 'match_name': None,
      'prereq': lambda x: not is_comment_line(x),
      'check': lambda x: trailing_operator(x),