# Buggy code
def maybe_autosave(self, index):
    """
    Autosave a file if necessary.

    If the file is newly created (and thus not named by the user), do
    nothing.  If the current contents are the same as the autosave file
    (if it exists) or the original file (if no autosave filee exists),
    then do nothing. If the current contents are the same as the file on
    disc, but the autosave file is different, then remove the autosave
    file. In all other cases, autosave the file.

    Args:
        index (int): index into self.stack.data
    """
    finfo = self.stack.data[index]
    if finfo.newly_created:
        return
    orig_filename = finfo.filename
    orig_hash = self.file_hashes[orig_filename]
    new_hash = self.stack.compute_hash(finfo)
    if orig_filename in self.name_mapping:
        autosave_filename = self.name_mapping[orig_filename]
        autosave_hash = self.file_hashes[autosave_filename]
        if new_hash != autosave_hash:
            if new_hash == orig_hash:
                self.remove_autosave_file(orig_filename)
            else:
                self.autosave(finfo)
    else:
        if new_hash != orig_hash:
            self.autosave(finfo)


# Traceback (KeyError)
File "C:\Users\joven\Anaconda3\lib\site-packages\spyder\plugins\editor\utils\autosave.py", line 116, in do_autosave
stack.autosave.autosave_all()
File "C:\Users\joven\Anaconda3\lib\site-packages\spyder\plugins\editor\utils\autosave.py", line 375, in autosave_all
self.maybe_autosave(index)
File "C:\Users\joven\Anaconda3\lib\site-packages\spyder\plugins\editor\utils\autosave.py", line 335, in maybe_autosave
orig_hash = self.file_hashes[orig_filename]
KeyError: 'C:\\Users\\joven\\.spyder-py3\\autosave\\optpath.py'

# Fixed code
def maybe_autosave(self, index):
    """
    Autosave a file if necessary.

    If the file is newly created (and thus not named by the user), do
    nothing.  If the current contents are the same as the autosave file
    (if it exists) or the original file (if no autosave filee exists),
    then do nothing. If the current contents are the same as the file on
    disc, but the autosave file is different, then remove the autosave
    file. In all other cases, autosave the file.

    Args:
        index (int): index into self.stack.data
    """
    finfo = self.stack.data[index]
    if finfo.newly_created:
        return
    orig_filename = finfo.filename
    try:
        orig_hash = self.file_hashes[orig_filename]
    except KeyError:
        # This should not happen, but it does: spyder-ide/spyder#11468
        # In this case, use an impossible value for the hash, so that
        # contents of buffer are considered different from contents of
        # original file.
        logger.error("KeyError when retrieving hash of %s", orig_filename)
        orig_hash = None
    new_hash = self.stack.compute_hash(finfo)
    if orig_filename in self.name_mapping:
        autosave_filename = self.name_mapping[orig_filename]
        autosave_hash = self.file_hashes[autosave_filename]
        if new_hash != autosave_hash:
            if new_hash == orig_hash:
                self.remove_autosave_file(orig_filename)
            else:
                self.autosave(finfo)
    else:
        if new_hash != orig_hash:
            self.autosave(finfo)