From 30d4611eacb1c5b27a5e9d2432cb12843054abc9 Mon Sep 17 00:00:00 2001 From: Campbell Barton <ideasman42@gmail.com> Date: Mon, 5 Nov 2018 11:35:56 +1100 Subject: [PATCH] check_style_c: fix error in case statement check --- check_source/check_style_c.py | 19 ++++++++++++++++++- readme.rst | 1 - tests/check_source/check_style_c_test.py | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/check_source/check_style_c.py b/check_source/check_style_c.py index d3d4031..ce6d0e1 100755 --- a/check_source/check_style_c.py +++ b/check_source/check_style_c.py @@ -180,6 +180,19 @@ def tk_advance_line_to_token(index, direction, text, type_): return None +def tk_advance_line_to_token_with_fn(index, direction, text, fn): + """ Advance to a token (on the same line). + """ + assert(isinstance(text, str)) + line = tokens[index].line + index += direction + while tokens[index].line == line: + if (tokens[index].text == text) and fn(tokens[index]): + return index + index += direction + return None + + def tk_advance_flag(index, direction, flag): state = (tokens[index].flag & flag) while ((tokens[index + direction].flag) & flag == state) and index > 0: @@ -693,7 +706,11 @@ def blender_check_kw_switch(fn, index_kw_start, index_kw, index_kw_end): # case ABC : # should be... # case ABC: - i_case = tk_advance_line_to_token(i, 1, ":", Token.Operator) + + # Note, this might be either 'Punctuation' or 'Operator', we need to check both. + i_case = tk_advance_line_to_token_with_fn( + i, 1, ":", + lambda t: t.type in {Token.Punctuation, Token.Operator}) # can be None when the identifier isn't an 'int' if i_case is not None: if tokens[i_case - 1].text.isspace(): diff --git a/readme.rst b/readme.rst index b60c3a8..d667079 100644 --- a/readme.rst +++ b/readme.rst @@ -45,4 +45,3 @@ Utils Programs (scripts) to help with development (currently for converting formats, creating mouse cursor, updating themes). - diff --git a/tests/check_source/check_style_c_test.py b/tests/check_source/check_style_c_test.py index a7573c6..3c2234e 100755 --- a/tests/check_source/check_style_c_test.py +++ b/tests/check_source/check_style_c_test.py @@ -319,6 +319,24 @@ void func(void) \t\tcase 0: \t\t\tcall(); \t\t\tbreak; +\t}""" + FUNC_END + err_found = test_code(code) + self.assertWarning(err_found) + + def test_switch_with_turnary(self): + # -------------------------------------------------------------------- + code = FUNC_BEGIN + """ +\tswitch (value) { +\t\tcase ABC : a = b ? c : d; break; +\t\t\tbreak; +\t}""" + FUNC_END + err_found = test_code(code) + self.assertWarning(err_found, "E132") + + code = FUNC_BEGIN + """ +\tswitch (value) { +\t\tcase ABC: a = b ? c : d; break; +\t\t\tbreak; \t}""" + FUNC_END err_found = test_code(code) self.assertWarning(err_found) -- GitLab