Repository: sqlfluff/sqlfluff

Issue:
Silence warnings / safety for when fixing from stdin
Another classic stdin fix issue from me! Fixing sql from stdin should probably have these rules:

1. _Only_ the fixed SQL is printed to stdout.
2. Safety not needed because files are not mutated in place

However, on current master:

```
➜ echo 'select 1 from t' | sqlfluff fix - 
The fix option is only available in combination with --rules. This is for your own safety! To disable this safety feature use --no-safety or --s.

➜ echo 'select 1 from t' | sqlfluff fix - --no-safety
NO SAFETY: Attempting fixes for all enabled rules.
select 1 from t
```
This would enable CLI users to assume that if there is stdout, it is fixed sql and not fixed sql mixed with other garbage. In this case we need to skip the safety checks if stdin, and silence the warning.

With some 👍 s, I'll take this one. Expect a PR from me either way 😉 

Discussion:

Patch:
diff --git a/src/sqlfluff/cli/commands.py b/src/sqlfluff/cli/commands.py
index b2497e7d1..055fc874d 100644
--- a/src/sqlfluff/cli/commands.py
+++ b/src/sqlfluff/cli/commands.py
@@ -385,8 +385,12 @@ def fix(
     character to indicate reading from *stdin* or a dot/blank ('.'/' ') which will
     be interpreted like passing the current working directory as a path argument.
     """
+    # some quick checks
+    fixing_stdin = ("-",) == paths
+    no_safety = no_safety or fixing_stdin  # saftey not needed if fixing stdin
+
     c = get_config(**kwargs)
-    lnt, formatter = get_linter_and_formatter(c, silent=("-",) == paths)
+    lnt, formatter = get_linter_and_formatter(c, silent=fixing_stdin)
     verbose = c.get("verbose")
 
     bencher = BenchIt()
@@ -398,9 +402,11 @@ def fix(
 
     # Check that if fix is specified, that we have picked only a subset of rules
     if no_safety:
-        click.echo(
-            colorize("NO SAFETY", "red") + ": Attempting fixes for all enabled rules."
-        )
+        if not fixing_stdin:
+            click.echo(
+                colorize("NO SAFETY", "red")
+                + ": Attempting fixes for all enabled rules."
+            )
     elif lnt.config.get("rule_whitelist") is None:
         click.echo(
             (
@@ -412,7 +418,7 @@ def fix(
         sys.exit(1)
 
     # handle stdin case. should output formatted sql to stdout and nothing else.
-    if ("-",) == paths:
+    if fixing_stdin:
         stdin = sys.stdin.read()
         # TODO: Remove verbose
         result = lnt.lint_string_wrapped(stdin, fname="stdin", fix=True)


Test patch:
diff --git a/test/cli/commands_test.py b/test/cli/commands_test.py
index 23b64d4f4..6a5f55dd2 100644
--- a/test/cli/commands_test.py
+++ b/test/cli/commands_test.py
@@ -333,6 +333,19 @@ def test__cli__command_fix_stdin(stdin, rules, stdout):
     assert result.output == stdout
 
 
+def test__cli__command_fix_stdin_safety():
+    """Check edge cases regarding safety when fixing stdin."""
+    perfect_sql = "select col from table"
+
+    # just prints the very same thing
+    result = invoke_assert_code(args=[fix, ("-",)], cli_input=perfect_sql)
+    assert result.output.strip() == perfect_sql
+
+    # still no warning if no safety specified unnecessarily
+    result = invoke_assert_code(args=[fix, ("-", "--no-safety")], cli_input=perfect_sql)
+    assert result.output.strip() == perfect_sql
+
+
 @pytest.mark.parametrize(
     "rule,fname,prompt,exit_code",
     [