Skip to content
Snippets Groups Projects
Commit 30d4611e authored by Campbell Barton's avatar Campbell Barton
Browse files

check_style_c: fix error in case statement check

parent 19223f7a
No related branches found
No related tags found
No related merge requests found
...@@ -180,6 +180,19 @@ def tk_advance_line_to_token(index, direction, text, type_): ...@@ -180,6 +180,19 @@ def tk_advance_line_to_token(index, direction, text, type_):
return None 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): def tk_advance_flag(index, direction, flag):
state = (tokens[index].flag & flag) state = (tokens[index].flag & flag)
while ((tokens[index + direction].flag) & flag == state) and index > 0: 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): ...@@ -693,7 +706,11 @@ def blender_check_kw_switch(fn, index_kw_start, index_kw, index_kw_end):
# case ABC : # case ABC :
# should be... # should be...
# case ABC: # 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' # can be None when the identifier isn't an 'int'
if i_case is not None: if i_case is not None:
if tokens[i_case - 1].text.isspace(): if tokens[i_case - 1].text.isspace():
......
...@@ -45,4 +45,3 @@ Utils ...@@ -45,4 +45,3 @@ Utils
Programs (scripts) to help with development Programs (scripts) to help with development
(currently for converting formats, creating mouse cursor, updating themes). (currently for converting formats, creating mouse cursor, updating themes).
...@@ -319,6 +319,24 @@ void func(void) ...@@ -319,6 +319,24 @@ void func(void)
\t\tcase 0: \t\tcase 0:
\t\t\tcall(); \t\t\tcall();
\t\t\tbreak; \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 \t}""" + FUNC_END
err_found = test_code(code) err_found = test_code(code)
self.assertWarning(err_found) self.assertWarning(err_found)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment