# Buggy code
def match(
    self,
    segments,
    match_depth=0,
    parse_depth=0,
    verbosity=0,
    dialect=None,
    match_segment=None,
):
    if self.code_only:
        first_code_idx = None
        # Work through to find the first code segment...
        for idx, seg in enumerate(segments):
            if seg.is_code:
                first_code_idx = idx
                break
        else:
            # We've trying to match on a sequence of segments which contain no code.
            # That means this isn't a match.
            return MatchResult.from_unmatched(segments)

        match = self.target._match(
            segments=segments[first_code_idx:],
            match_depth=match_depth + 1,
            parse_depth=parse_depth,
            verbosity=verbosity,
            dialect=dialect,
            match_segment=match_segment,
        )
        if match:
            # The match will probably have returned a mutated version rather
            # that the raw segment sent for matching. We need to reinsert it
            # back into the sequence in place of the raw one, but we can't
            # just assign at the index because it's a tuple and not a list.
            # to get around that we do this slightly more elaborate construction.

            # NB: This match may be partial or full, either is cool. In the case
            # of a partial match, given that we're only interested in what it STARTS
            # with, then we can still used the unmatched parts on the end.
            # We still need to deal with any non-code segments at the start.
            if self.terminator:
                # We have an optional terminator. We should only match up to when
                # this matches. This should also respect bracket counting.
                match_segments = match.matched_segments
                trailing_segments = match.unmatched_segments

                # Given a set of segments, iterate through looking for
                # a terminator.
                term_match = self.bracket_sensitive_forward_match(
                    segments=trailing_segments,
                    start_bracket=self.start_bracket,
                    end_bracket=self.end_bracket,
                    match_depth=match_depth,
                    parse_depth=parse_depth,
                    verbosity=verbosity,
                    terminator=self.terminator,
                    dialect=dialect,
                    match_segment=match_segment,
                )
                return MatchResult(
                    segments[:first_code_idx]
                    + match_segments
                    + term_match.matched_segments,
                    term_match.unmatched_segments,
                )
            else:
                return MatchResult.from_matched(
                    segments[:first_code_idx]
                    + match.matched_segments
                    + match.unmatched_segments
                )
        else:
            return MatchResult.from_unmatched(segments)
    else:
        raise NotImplementedError(
            "Not expecting to match StartsWith and also not just code!?"
        )


# Traceback (TypeError)
Traceback (most recent call last):
File "/home/user/sql/.venv/bin/sqlfluff", line 11, in <module>
load_entry_point('sqlfluff==0.1.3', 'console_scripts', 'sqlfluff')()
File "/home/user/sql/.venv/lib/python3.7/site-packages/click/core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "/home/user/sql/.venv/lib/python3.7/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/home/user/sql/.venv/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/user/sql/.venv/lib/python3.7/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/user/sql/.venv/lib/python3.7/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/cli/commands.py", line 85, in lint
result = lnt.lint_paths(paths, verbosity=verbose)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/linter.py", line 324, in lint_paths
result.add(self.lint_path(path, verbosity=verbosity, fix=fix))
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/linter.py", line 312, in lint_path
linted_path.add(self.lint_file(f, fname=fname, verbosity=verbosity, fix=fix))
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/linter.py", line 212, in lint_file
parsed, vs, time_dict = self.parse_file(f=f, fname=fname, verbosity=verbosity)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/linter.py", line 193, in parse_file
parsed = fs.parse(recurse=recurse, verbosity=verbosity, dialect=self.dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 228, in parse
verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 394, in expand
res = stmt.parse(recurse=recurse, parse_depth=parse_depth, verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 228, in parse
verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 394, in expand
res = stmt.parse(recurse=recurse, parse_depth=parse_depth, verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 228, in parse
verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 394, in expand
res = stmt.parse(recurse=recurse, parse_depth=parse_depth, verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 228, in parse
verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 394, in expand
res = stmt.parse(recurse=recurse, parse_depth=parse_depth, verbosity=verbosity, dialect=dialect)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 186, in parse
dialect=dialect, match_segment=self.__class__.__name__)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 70, in _match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 273, in match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 70, in _match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 485, in match
match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 70, in _match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 273, in match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 70, in _match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 236, in match
dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 361, in _match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/segments_base.py", line 320, in match
dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 70, in _match
verbosity=verbosity, dialect=dialect, match_segment=match_segment)
File "/home/user/sql/.venv/lib/python3.7/site-packages/sqlfluff/parser/grammar.py", line 586, in match
segment=segments[start_bracket_idx])
TypeError: tuple indices must be integers or slices, not NoneType

# Fixed code
def match(self, segments, parse_context):
    if self.code_only:
        first_code_idx = None
        # Work through to find the first code segment...
        for idx, seg in enumerate(segments):
            if seg.is_code:
                first_code_idx = idx
                break
        else:
            # We've trying to match on a sequence of segments which contain no code.
            # That means this isn't a match.
            return MatchResult.from_unmatched(segments)

        match = self.target._match(
            segments=segments[first_code_idx:],
            parse_context=parse_context.copy(incr="match_depth"),
        )
        if match:
            # The match will probably have returned a mutated version rather
            # that the raw segment sent for matching. We need to reinsert it
            # back into the sequence in place of the raw one, but we can't
            # just assign at the index because it's a tuple and not a list.
            # to get around that we do this slightly more elaborate construction.

            # NB: This match may be partial or full, either is cool. In the case
            # of a partial match, given that we're only interested in what it STARTS
            # with, then we can still used the unmatched parts on the end.
            # We still need to deal with any non-code segments at the start.
            if self.terminator:
                # We have an optional terminator. We should only match up to when
                # this matches. This should also respect bracket counting.
                match_segments = match.matched_segments
                trailing_segments = match.unmatched_segments

                # Given a set of segments, iterate through looking for
                # a terminator.
                res = self._bracket_sensitive_look_ahead_match(
                    segments=trailing_segments,
                    matchers=[self.terminator],
                    parse_context=parse_context,
                )

                # Depending on whether we found a terminator or not we treat
                # the result slightly differently. If no terminator was found,
                # we just use the whole unmatched segment. If we did find one,
                # we match up until (but not including) that terminator.
                term_match = res[1]
                if term_match:
                    m_tail = res[0]
                    u_tail = term_match.all_segments()
                else:
                    m_tail = term_match.unmatched_segments
                    u_tail = ()

                return MatchResult(
                    segments[:first_code_idx] + match_segments + m_tail,
                    u_tail,
                )
            else:
                return MatchResult.from_matched(
                    segments[:first_code_idx] + match.all_segments()
                )
        else:
            return MatchResult.from_unmatched(segments)
    else:
        raise NotImplementedError(
            "Not expecting to match StartsWith and also not just code!?"
        )