# Buggy code
def visit_Call(self, node):  # type: (ast.Call) -> None
    if (
        isinstance(node.func, ast.Name)
        and node.func.id in {"isinstance", "issubclass"}
        and len(node.args) == 2
        and self._is_six(node.args[1], SIX_TYPE_CTX_ATTRS)
    ):
        arg = node.args[1]
        # _is_six() enforces this
        assert isinstance(arg, (ast.Name, ast.Attribute))
        self.six_type_ctx[_ast_to_offset(node.args[1])] = arg
    elif self._is_six(node.func, ("b",)):
        self.six_b.add(_ast_to_offset(node))
    elif self._is_six(node.func, SIX_CALLS) and not _starargs(node):
        self.six_calls[_ast_to_offset(node)] = node
    elif (
        isinstance(node.func, ast.Name)
        and node.func.id == "next"
        and not _starargs(node)
        and isinstance(node.args[0], ast.Call)
        and self._is_six(
            node.args[0].func,
            ("iteritems", "iterkeys", "itervalues"),
        )
        and not _starargs(node.args[0])
    ):
        self.six_iter[_ast_to_offset(node.args[0])] = node.args[0]
    elif (
        isinstance(self._previous_node, ast.Expr)
        and self._is_six(node.func, SIX_RAISES)
        and not _starargs(node)
    ):
        self.six_raises[_ast_to_offset(node)] = node
    elif (
        not self._in_comp
        and self._class_info_stack
        and self._class_info_stack[-1].def_depth == 1
        and isinstance(node.func, ast.Name)
        and node.func.id == "super"
        and len(node.args) == 2
        and isinstance(node.args[0], ast.Name)
        and isinstance(node.args[1], ast.Name)
        and node.args[0].id == self._class_info_stack[-1].name
        and node.args[1].id == self._class_info_stack[-1].first_arg_name
    ):
        self.super_calls[_ast_to_offset(node)] = node
    elif (
        isinstance(node.func, ast.Name)
        and node.func.id == "str"
        and not node.keywords
        and not _starargs(node)
        and (
            len(node.args) == 0
            or (len(node.args) == 1 and isinstance(node.args[0], ast.Str))
        )
    ):
        self.native_literals.add(_ast_to_offset(node))
    elif (
        isinstance(node.func, ast.Attribute)
        and isinstance(node.func.value, ast.Str)
        and node.func.attr == "encode"
        and not _starargs(node)
        and len(node.args) == 1
        and isinstance(node.args[0], ast.Str)
        and _is_codec(node.args[0].s, "utf-8")
    ):
        self.encode_calls[_ast_to_offset(node)] = node
    elif self._is_io_open(node.func):
        self.io_open_calls[_ast_to_offset(node)] = node

    self.generic_visit(node)


# Traceback (IndexError)
$ 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

# Fixed code
def visit_Call(self, node):  # type: (ast.Call) -> None
    if (
        isinstance(node.func, ast.Name)
        and node.func.id in {"isinstance", "issubclass"}
        and len(node.args) == 2
        and self._is_six(node.args[1], SIX_TYPE_CTX_ATTRS)
    ):
        arg = node.args[1]
        # _is_six() enforces this
        assert isinstance(arg, (ast.Name, ast.Attribute))
        self.six_type_ctx[_ast_to_offset(node.args[1])] = arg
    elif self._is_six(node.func, ("b",)):
        self.six_b.add(_ast_to_offset(node))
    elif self._is_six(node.func, SIX_CALLS) and not _starargs(node):
        self.six_calls[_ast_to_offset(node)] = node
    elif (
        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,
            ("iteritems", "iterkeys", "itervalues"),
        )
        and not _starargs(node.args[0])
    ):
        self.six_iter[_ast_to_offset(node.args[0])] = node.args[0]
    elif (
        isinstance(self._previous_node, ast.Expr)
        and self._is_six(node.func, SIX_RAISES)
        and not _starargs(node)
    ):
        self.six_raises[_ast_to_offset(node)] = node
    elif (
        not self._in_comp
        and self._class_info_stack
        and self._class_info_stack[-1].def_depth == 1
        and isinstance(node.func, ast.Name)
        and node.func.id == "super"
        and len(node.args) == 2
        and isinstance(node.args[0], ast.Name)
        and isinstance(node.args[1], ast.Name)
        and node.args[0].id == self._class_info_stack[-1].name
        and node.args[1].id == self._class_info_stack[-1].first_arg_name
    ):
        self.super_calls[_ast_to_offset(node)] = node
    elif (
        isinstance(node.func, ast.Name)
        and node.func.id == "str"
        and not node.keywords
        and not _starargs(node)
        and (
            len(node.args) == 0
            or (len(node.args) == 1 and isinstance(node.args[0], ast.Str))
        )
    ):
        self.native_literals.add(_ast_to_offset(node))
    elif (
        isinstance(node.func, ast.Attribute)
        and isinstance(node.func.value, ast.Str)
        and node.func.attr == "encode"
        and not _starargs(node)
        and len(node.args) == 1
        and isinstance(node.args[0], ast.Str)
        and _is_codec(node.args[0].s, "utf-8")
    ):
        self.encode_calls[_ast_to_offset(node)] = node
    elif self._is_io_open(node.func):
        self.io_open_calls[_ast_to_offset(node)] = node

    self.generic_visit(node)