Add 'continue' not properly in loop

This commit is contained in:
Dave Halter
2017-07-19 23:52:41 +02:00
parent 4f7d78716a
commit 4a86571b21
4 changed files with 15 additions and 0 deletions

View File

@@ -85,12 +85,19 @@ class ErrorFinder(Normalizer):
else: else:
self._add_syntax_error('invalid syntax', leaf) self._add_syntax_error('invalid syntax', leaf)
elif leaf.value == 'continue': elif leaf.value == 'continue':
in_loop = False
for block in self._context.blocks: for block in self._context.blocks:
if block.type == 'for_stmt':
in_loop = True
if block.type == 'try_stmt': if block.type == 'try_stmt':
last_block = block.children[-3] last_block = block.children[-3]
if last_block == 'finally' and leaf.start_pos > last_block.start_pos: if last_block == 'finally' and leaf.start_pos > last_block.start_pos:
message = "'continue' not supported inside 'finally' clause" message = "'continue' not supported inside 'finally' clause"
self._add_syntax_error(message, leaf) self._add_syntax_error(message, leaf)
if not in_loop:
message = "'continue' not properly in loop"
self._add_syntax_error(message, leaf)
return ''
def _add_indentation_error(self, message, spacing): def _add_indentation_error(self, message, spacing):
self._add_error(903, "IndentationError: " + message, spacing) self._add_error(903, "IndentationError: " + message, spacing)

View File

@@ -367,6 +367,8 @@ class PEP8Normalizer(ErrorFinder):
self._previous_leaf = leaf self._previous_leaf = leaf
return leaf.value
def _visit_part(self, part, spacing, leaf): def _visit_part(self, part, spacing, leaf):
value = part.value value = part.value
type_ = part.type type_ = part.type

View File

@@ -9,3 +9,8 @@ for x in [1]:
finally: finally:
#: E901 #: E901
continue continue
for x in [1]:
break
continue

View File

@@ -67,6 +67,7 @@ def test_indentation_errors(code, positions):
finally: finally:
continue continue
'''), # 'continue' not supported inside 'finally' clause" '''), # 'continue' not supported inside 'finally' clause"
'continue'
# IndentationError # IndentationError
' foo', ' foo',