Repository: asottile/yesqa

Issue:
Can convert limited noqa's to generic noqa's
If you have a line of code that has a flake8 error, but different from the one listed in a noqa comment, the tool will convert `# noqa: X000` to `# noqa:` which then masks the other error.

Example:
```
if x==2:  # noqa: E101
    pass
```
becomes:
```
if x==2:  # noqa: 
    pass
```
which now suppresses E225


Discussion:

Patch:
diff --git a/yesqa.py b/yesqa.py
index 478cbe3..431a8f8 100644
--- a/yesqa.py
+++ b/yesqa.py
@@ -72,18 +72,25 @@ def _rewrite_noqa_comment(
     match = NOQA_RE.search(token.src)
     assert match is not None
 
+    def _remove_noqa() -> None:
+        assert match is not None
+        if match.group() == token.src:
+            _remove_comment(tokens, i)
+        else:
+            src = NOQA_RE.sub('', token.src).strip()
+            if not src.startswith('#'):
+                src = f'# {src}'
+            tokens[i] = token._replace(src=src)
+
     # exclude all lints on the line but no lints
-    if not lints and match.group() == token.src:
-        _remove_comment(tokens, i)
-    elif not lints:
-        src = NOQA_RE.sub('', token.src).strip()
-        if not src.startswith('#'):
-            src = f'# {src}'
-        tokens[i] = token._replace(src=src)
+    if not lints:
+        _remove_noqa()
     elif match.group().lower() != '# noqa':
         codes = set(SEP_RE.split(match.group(1)[1:]))
         expected_codes = codes & lints
-        if expected_codes != codes:
+        if not expected_codes:
+            _remove_noqa()
+        elif expected_codes != codes:
             comment = f'# noqa: {", ".join(sorted(expected_codes))}'
             tokens[i] = token._replace(src=NOQA_RE.sub(comment, token.src))
 


Test patch:
diff --git a/tests/yesqa_test.py b/tests/yesqa_test.py
index 84a604e..6e90ada 100644
--- a/tests/yesqa_test.py
+++ b/tests/yesqa_test.py
@@ -81,6 +81,13 @@ def test_ok(assert_rewrite, src):
             '# a  # noqa: E501\n',
             '# a\n',
         ),
+        pytest.param(
+            'if x==1:  # noqa: F401\n'
+            '    pass\n',
+            'if x==1:\n'
+            '    pass\n',
+            id='wrong noqa',
+        ),
         # file comments
         ('# flake8: noqa\nx = 1\n', 'x = 1\n'),
         ('x = 1  # flake8: noqa\n', 'x = 1\n'),