Repository: sqlfluff/sqlfluff

Issue:
[L001] Applying rule L001 threw an Exception: tuple index out of range
Testing SQLFluff again after https://github.com/sqlfluff/sqlfluff/pull/1364.

## Expected Behaviour

Not crash, and maybe delete the space character on the first line.

## Observed Behaviour

Prints the following error message:
```
==== finding fixable violations ====
CRITICAL   [L001] Applying rule L001 threw an Exception: tuple index out of range 
Traceback (most recent call last):
  File "/home/vladimir/work/extern/sqlfluff/venv/lib/python3.9/site-packages/sqlfluff/core/rules/base.py", line 301, in crawl
    res = self._eval(
  File "/home/vladimir/work/extern/sqlfluff/venv/lib/python3.9/site-packages/sqlfluff/rules/L001.py", line 44, in _eval
    while raw_stack[idx].is_type("whitespace"):
IndexError: tuple index out of range
==== no fixable linting violations found ====
  [1 unfixable linting violations found]
All Finished 📜 🎉!
```

sqlfluff parse output:
```
$ sqlfluff parse test.sql
[L:  1, P:  1]      |file:
[L:  1, P:  1]      |    whitespace:                                               ' '
[L:  1, P:  2]      |    newline:                                                  '\n'
[L:  2, P:  1]      |    statement:
[L:  2, P:  1]      |        select_statement:
[L:  2, P:  1]      |            select_clause:
[L:  2, P:  1]      |                keyword:                                      'SELECT'
[L:  2, P:  7]      |                [META] indent:
[L:  2, P:  7]      |                whitespace:                                   ' '
[L:  2, P:  8]      |                select_clause_element:
[L:  2, P:  8]      |                    literal:                                  '1'
[L:  2, P:  9]      |            [META] dedent:
[L:  2, P:  9]      |    newline:                                                  '\n'
```

## Steps to Reproduce

```console
$ printf ' \nSELECT 1\n' > test.sql
$ sqlfluff fix test.sql
```

## Dialect

None

## Version

SQLFluff 17e15d1dc09442596c83c7f56826f24659c877a0, Python 3.9.6

## Configuration

None

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/sqlfluff/sqlfluff/pull/1372?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff) Report
> Merging [#1372](https://codecov.io/gh/sqlfluff/sqlfluff/pull/1372?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff) (b789929) into [main](https://codecov.io/gh/sqlfluff/sqlfluff/commit/17e15d1dc09442596c83c7f56826f24659c877a0?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff) (17e15d1) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/sqlfluff/sqlfluff/pull/1372/graphs/tree.svg?width=650&height=150&src=pr&token=maM8kLgCmn&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff)](https://codecov.io/gh/sqlfluff/sqlfluff/pull/1372?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff)

```diff
@@            Coverage Diff            @@
##              main     #1372   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          125       125           
  Lines         8398      8398           
=========================================
  Hits          8398      8398           
```

| Flag | Coverage Δ | |
|---|---|---|
| dbt018-py38 | `62.20% <100.00%> (ø)` | |
| dbt019-py38 | `62.20% <100.00%> (ø)` | |
| dbt020-py38 | `62.18% <100.00%> (ø)` | |
| py36 | `98.90% <100.00%> (ø)` | |
| py37 | `98.94% <100.00%> (ø)` | |
| py38 | `98.95% <100.00%> (ø)` | |
| py39 | `98.95% <100.00%> (ø)` | |
| win-py37 | `?` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/sqlfluff/sqlfluff/pull/1372?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff) | Coverage Δ | |
|---|---|---|
| [src/sqlfluff/rules/L001.py](https://codecov.io/gh/sqlfluff/sqlfluff/pull/1372/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff#diff-c3JjL3NxbGZsdWZmL3J1bGVzL0wwMDEucHk=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/sqlfluff/sqlfluff/pull/1372?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/sqlfluff/sqlfluff/pull/1372?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff). Last update [17e15d1...b789929](https://codecov.io/gh/sqlfluff/sqlfluff/pull/1372?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sqlfluff).


Patch:
diff --git a/src/sqlfluff/rules/L001.py b/src/sqlfluff/rules/L001.py
index 088a67cf2..020294175 100644
--- a/src/sqlfluff/rules/L001.py
+++ b/src/sqlfluff/rules/L001.py
@@ -41,7 +41,7 @@ class Rule_L001(BaseRule):
             # If we find a newline, which is preceded by whitespace, then bad
             deletions = []
             idx = -1
-            while raw_stack[idx].is_type("whitespace"):
+            while abs(idx) <= len(raw_stack) and raw_stack[idx].is_type("whitespace"):
                 deletions.append(raw_stack[idx])
                 idx -= 1
             return LintResult(


Test patch:
diff --git a/test/fixtures/rules/std_rule_cases/L001.yml b/test/fixtures/rules/std_rule_cases/L001.yml
index 4b644f1c1..4a7959b2f 100644
--- a/test/fixtures/rules/std_rule_cases/L001.yml
+++ b/test/fixtures/rules/std_rule_cases/L001.yml
@@ -3,3 +3,8 @@ rule: L001
 test_fail_trailing_whitespace:
   fail_str: "SELECT 1     \n"
   fix_str: "SELECT 1\n"
+
+
+test_fail_trailing_whitespace_on_initial_blank_line:
+  fail_str: " \nSELECT 1     \n"
+  fix_str: "\nSELECT 1\n"