# Buggy code
def apply(self):
    for node in self.document.footnotes:
        if node["names"][0] not in self.document.footnote_refs:
            logger.warning(
                "Footnote [%s] is not referenced.",
                node["names"][0],
                type="ref",
                subtype="footnote",
                location=node,
            )

    for node in self.document.autofootnotes:
        if not any(
            ref["auto"] == node["auto"] for ref in self.document.autofootnote_refs
        ):
            logger.warning(
                "Footnote [#] is not referenced.",
                type="ref",
                subtype="footnote",
                location=node,
            )


# Traceback (IndexError)
# Sphinx version: 1.6.2
# Python version: 3.6.1 (CPython)
# Docutils version: 0.13.1 release
# Jinja2 version: 2.9.6
# Last messages:
#   building [html]: targets for 2 source files that are out of date
#
#   updating environment:
#
#   2 added, 0 changed, 0 removed
#
#   reading sources... [ 50%] index
#
#   reading sources... [100%] my_module
#
# Loaded extensions:
#   alabaster (0.7.10) from /home/accounts/this_user/local/miniconda2/envs/sphinx_test/lib/python3.6/site-packages/alabaster/__init__.py
#   sphinx.ext.autodoc (1.6.2) from /home/accounts/this_user/local/miniconda2/envs/sphinx_test/lib/python3.6/site-packages/Sphinx-1.6.2-py3.6.egg/sphinx/ext/autodoc.py
Traceback (most recent call last):
File "/home/accounts/this_user/local/miniconda2/envs/sphinx_test/lib/python3.6/site-packages/Sphinx-1.6.2-py3.6.egg/sphinx/cmdline.py", line 306, in main
app.build(opts.force_all, filenames)
File "/home/accounts/this_user/local/miniconda2/envs/sphinx_test/lib/python3.6/site-packages/Sphinx-1.6.2-py3.6.egg/sphinx/application.py", line 339, in build
self.builder.build_update()
File "/home/accounts/this_user/local/miniconda2/envs/sphinx_test/lib/python3.6/site-packages/Sphinx-1.6.2-py3.6.egg/sphinx/builders/__init__.py", line 328, in build_update
'out of date' % len(to_build))
File "/home/accounts/this_user/local/miniconda2/envs/sphinx_test/lib/python3.6/site-packages/Sphinx-1.6.2-py3.6.egg/sphinx/builders/__init__.py", line 341, in build
updated_docnames = set(self.env.update(self.config, self.srcdir, self.doctreedir))
File "/home/accounts/this_user/local/miniconda2/envs/sphinx_test/lib/python3.6/site-packages/Sphinx-1.6.2-py3.6.egg/sphinx/environment/__init__.py", line 584, in update
self._read_serial(docnames, self.app)
File "/home/accounts/this_user/local/miniconda2/envs/sphinx_test/lib/python3.6/site-packages/Sphinx-1.6.2-py3.6.egg/sphinx/environment/__init__.py", line 603, in _read_serial
self.read_doc(docname, app)
File "/home/accounts/this_user/local/miniconda2/envs/sphinx_test/lib/python3.6/site-packages/Sphinx-1.6.2-py3.6.egg/sphinx/environment/__init__.py", line 724, in read_doc
pub.publish()
File "/home/accounts/this_user/local/miniconda2/envs/sphinx_test/lib/python3.6/site-packages/docutils/core.py", line 218, in publish
self.apply_transforms()
File "/home/accounts/this_user/local/miniconda2/envs/sphinx_test/lib/python3.6/site-packages/docutils/core.py", line 199, in apply_transforms
self.document.transformer.apply_transforms()
File "/home/accounts/this_user/local/miniconda2/envs/sphinx_test/lib/python3.6/site-packages/docutils/transforms/__init__.py", line 171, in apply_transforms
transform.apply(**kwargs)
File "/home/accounts/this_user/local/miniconda2/envs/sphinx_test/lib/python3.6/site-packages/Sphinx-1.6.2-py3.6.egg/sphinx/transforms/__init__.py", line 292, in apply
if node['names'][0] not in self.document.footnote_refs:
IndexError: list index out of range

# Fixed code
def apply(self):
    for node in self.document.footnotes:
        if node["names"] == []:
            # footnote having duplicated number.  It is already warned at parser.
            pass
        elif node["names"][0] not in self.document.footnote_refs:
            logger.warning(
                "Footnote [%s] is not referenced.",
                node["names"][0],
                type="ref",
                subtype="footnote",
                location=node,
            )

    for node in self.document.autofootnotes:
        if not any(
            ref["auto"] == node["auto"] for ref in self.document.autofootnote_refs
        ):
            logger.warning(
                "Footnote [#] is not referenced.",
                type="ref",
                subtype="footnote",
                location=node,
            )