Repository: asottile/pyupgrade

Issue:
IndexError: list index out of range
Tested with pyupgrade-1.25.0.

Running the following command on the [more-itertools](https://github.com/erikrose/more-itertools) repository results in the following crash. Only occurs with the `--py3-plus` flag.

```
$ pyupgrade --py3-plus more_itertools/recipes.py
Traceback (most recent call last):
  File "/home/jdufresne/.venv/more-itertools/bin/pyupgrade", line 10, in <module>
    sys.exit(main())
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 2237, in main
    ret |= _fix_file(filename, args)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 2199, in _fix_file
    contents_text = _fix_py3_plus(contents_text)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 1801, in _fix_py3_plus
    visitor.visit(ast_obj)
  File "/usr/lib64/python3.7/ast.py", line 262, in visit
    return visitor(node)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 1536, in generic_visit
    super(FindPy3Plus, self).generic_visit(node)
  File "/usr/lib64/python3.7/ast.py", line 270, in generic_visit
    self.visit(item)
  File "/usr/lib64/python3.7/ast.py", line 262, in visit
    return visitor(node)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 1312, in _visit_sync_func
    self._visit_func(node)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 1307, in _visit_func
    self.generic_visit(node)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 1536, in generic_visit
    super(FindPy3Plus, self).generic_visit(node)
  File "/usr/lib64/python3.7/ast.py", line 270, in generic_visit
    self.visit(item)
  File "/usr/lib64/python3.7/ast.py", line 262, in visit
    return visitor(node)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 1536, in generic_visit
    super(FindPy3Plus, self).generic_visit(node)
  File "/usr/lib64/python3.7/ast.py", line 270, in generic_visit
    self.visit(item)
  File "/usr/lib64/python3.7/ast.py", line 262, in visit
    return visitor(node)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 1353, in visit_Try
    self.generic_visit(node)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 1536, in generic_visit
    super(FindPy3Plus, self).generic_visit(node)
  File "/usr/lib64/python3.7/ast.py", line 270, in generic_visit
    self.visit(item)
  File "/usr/lib64/python3.7/ast.py", line 262, in visit
    return visitor(node)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 1532, in visit_For
    self.generic_visit(node)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 1536, in generic_visit
    super(FindPy3Plus, self).generic_visit(node)
  File "/usr/lib64/python3.7/ast.py", line 270, in generic_visit
    self.visit(item)
  File "/usr/lib64/python3.7/ast.py", line 262, in visit
    return visitor(node)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 1536, in generic_visit
    super(FindPy3Plus, self).generic_visit(node)
  File "/usr/lib64/python3.7/ast.py", line 272, in generic_visit
    self.visit(value)
  File "/usr/lib64/python3.7/ast.py", line 262, in visit
    return visitor(node)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 1536, in generic_visit
    super(FindPy3Plus, self).generic_visit(node)
  File "/usr/lib64/python3.7/ast.py", line 272, in generic_visit
    self.visit(value)
  File "/usr/lib64/python3.7/ast.py", line 262, in visit
    return visitor(node)
  File "/home/jdufresne/.venv/more-itertools/lib64/python3.7/site-packages/pyupgrade.py", line 1393, in visit_Call
    isinstance(node.args[0], ast.Call) and
IndexError: list index out of range
```

Discussion:

Patch:
diff --git a/pyupgrade.py b/pyupgrade.py
index b451f58..161eeb9 100644
--- a/pyupgrade.py
+++ b/pyupgrade.py
@@ -1390,6 +1390,7 @@ class FindPy3Plus(ast.NodeVisitor):
                 isinstance(node.func, ast.Name) and
                 node.func.id == 'next' and
                 not _starargs(node) and
+                len(node.args) == 1 and
                 isinstance(node.args[0], ast.Call) and
                 self._is_six(
                     node.args[0].func,


Test patch:
diff --git a/tests/six_test.py b/tests/six_test.py
index ae53419..3afe83b 100644
--- a/tests/six_test.py
+++ b/tests/six_test.py
@@ -40,6 +40,8 @@ from pyupgrade import _fix_py3_plus
         '(\n'
         '    six\n'
         ').text_type(u)\n',
+        # next is shadowed
+        'next()',
     ),
 )
 def test_fix_six_noop(s):