# Buggy code
def fix(self, parsed: BaseSegment, config: Optional[FluffConfig] = None):
    """Fix a parsed file object."""
    # Set up our config
    config = config or self.config
    # If we're in fix mode, then we need to progressively call and reconstruct
    working = parsed
    # Keep a set of previous versions to catch infinite loops.
    previous_versions = {working.raw}
    # A placeholder for the fixes we had on the previous loop
    last_fixes = None
    # How many loops have we had
    fix_loop_idx = 0
    # How many loops are we allowed
    loop_limit = config.get("runaway_limit")
    # Keep track of the errors from round 1
    linting_errors = []
    initial_linting_errors = []
    # Enter into the main fix loop. Some fixes may introduce other
    # problems and so we loop around this until we reach stability
    # or we reach the limit.
    while fix_loop_idx < loop_limit:
        fix_loop_idx += 1
        changed = False
        # Iterate through each rule.
        for crawler in self.get_ruleset(config=config):
            # fixes should be a dict {} with keys edit, delete, create
            # delete is just a list of segments to delete
            # edit and create are list of tuples. The first element is the
            # "anchor", the segment to look for either to edit or to insert BEFORE.
            # The second is the element to insert or create.
            lerrs, _, fixes, _ = crawler.crawl(
                working, dialect=config.get("dialect_obj"), fix=True
            )
            linting_errors += lerrs
            # Are there fixes to apply?
            if fixes:
                linter_logger.info("Applying Fixes: %s", fixes)
                # Do some sanity checks on the fixes before applying.
                if last_fixes and fixes == last_fixes:
                    linter_logger.warning(
                        "One fix for %s not applied, it would re-cause the same error.",
                        crawler.code,
                    )
                else:
                    last_fixes = fixes
                    # Actually apply fixes.
                    new_working, _ = working.apply_fixes(fixes)
                    # Check for infinite loops
                    if new_working.raw not in previous_versions:
                        # We've not seen this version of the file so far. Continue.
                        working = new_working
                        previous_versions.add(working.raw)
                        changed = True
                        continue
                    # Applying these fixes took us back to a state which we've
                    # seen before. Abort.
                    linter_logger.warning(
                        "One fix for %s not applied, it would re-cause the same error.",
                        crawler.code,
                    )
        # Keep track of initial errors for reporting.
        if fix_loop_idx == 1:
            initial_linting_errors = linting_errors.copy()
        # We did not change the file. Either the file is clean (no fixes), or
        # any fixes which are present will take us back to a previous state.
        if not changed:
            linter_logger.info(
                "Fix loop complete. Stability achieved after %s/%s loops.",
                fix_loop_idx,
                loop_limit,
            )
            break
    else:
        linter_logger.warning(
            "Loop limit on fixes reached [%s]. Some fixes may be overdone.",
            loop_limit,
        )
    return working, initial_linting_errors


# Traceback (IndexError)
Traceback (most recent call last):
File "/Users/niallwoodward/dev/pull_requests/dbt_sqlfluff_venv/bin/sqlfluff", line 11, in <module>
load_entry_point('sqlfluff', 'console_scripts', 'sqlfluff')()
File "/Users/niallwoodward/dev/pull_requests/dbt_sqlfluff_venv/lib/python3.6/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/Users/niallwoodward/dev/pull_requests/dbt_sqlfluff_venv/lib/python3.6/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/Users/niallwoodward/dev/pull_requests/dbt_sqlfluff_venv/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/niallwoodward/dev/pull_requests/dbt_sqlfluff_venv/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/niallwoodward/dev/pull_requests/dbt_sqlfluff_venv/lib/python3.6/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/cli/commands.py", line 323, in lint
ignore_files=not disregard_sqlfluffignores,
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/linter.py", line 1161, in lint_paths
ignore_files=ignore_files,
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/linter.py", line 1139, in lint_path
target_file.read(), fname=fname, fix=fix, config=config
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/linter.py", line 939, in lint_string
parsed = self.parse_string(in_str=in_str, fname=fname, config=config)
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/linter.py", line 723, in parse_string
tokens, lex_vs = lexer.lex(templated_file)
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/parser/lexer.py", line 343, in lex
return self.enrich_segments(segment_buff, raw), violations
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/parser/lexer.py", line 373, in enrich_segments
templated_slice
File "/Users/niallwoodward/dev/pull_requests/sqlfluff/src/sqlfluff/core/templaters/base.py", line 283, in templated_slice_to_source_slice
elif start_slices[0][0] == "literal":
IndexError: list index out of range

# Fixed code
def fix(
    self, tree: BaseSegment, config: Optional[FluffConfig] = None
) -> Tuple[BaseSegment, List[SQLLintError]]:
    """Return the fixed tree and violations from lintfix when we're fixing."""
    fixed_tree, violations = self.lint_fix(tree, config, fix=True)
    return fixed_tree, violations