diff --git a/check_source/check_style_c.py b/check_source/check_style_c.py index d3d40314f1fd1da92e16a6af86be43b87c23ecaf..ce6d0e1c2627b23907d7514f347e0da5d1a1829a 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 b60c3a8378c235056b476cc48cf692e1f92ac8ce..d667079d33bb84534352eeb537e5e0edfd32bd06 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 a7573c6372bb59ebe37a83d9844bce5c02725db7..3c2234efe31160ce07096795be5e29a218e9be4f 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)