Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF102.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
import json # noqa: E402, INVALID400, V100
# Test for rule redirects
import pandas as pd # noqa: TCH002
# Invalid code with trailing reason (single comment)
import pathlib # noqa: INVALID123 some reason
6 changes: 4 additions & 2 deletions crates/ruff_linter/src/rules/ruff/rules/invalid_rule_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};

use crate::Locator;
use crate::checkers::ast::LintContext;
use crate::fix::edits::delete_comment;
use crate::noqa::{Code, Directive};
use crate::noqa::{Codes, NoqaDirectives};
use crate::registry::Rule;
Expand Down Expand Up @@ -99,7 +100,7 @@ pub(crate) fn invalid_noqa_code(
.partition(|&code| code_is_valid(code.as_str(), external));

if valid_codes.is_empty() {
all_codes_invalid_diagnostic(directive, invalid_codes, context);
all_codes_invalid_diagnostic(directive, invalid_codes, locator, context);
} else {
for invalid_code in invalid_codes {
some_codes_are_invalid_diagnostic(directive, invalid_code, locator, context);
Expand All @@ -116,6 +117,7 @@ pub(crate) fn code_is_valid(code: &str, external: &[String]) -> bool {
fn all_codes_invalid_diagnostic(
directive: &Codes<'_>,
invalid_codes: Vec<&Code<'_>>,
locator: &Locator,
context: &LintContext,
) {
context
Expand All @@ -131,7 +133,7 @@ fn all_codes_invalid_diagnostic(
},
directive.range(),
)
.set_fix(Fix::safe_edit(Edit::range_deletion(directive.range())));
.set_fix(Fix::safe_edit(delete_comment(directive.range(), locator)));
}

fn some_codes_are_invalid_diagnostic(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID123
help: Remove the `# noqa` comment
1 | # Invalid code
- import os # noqa: INVALID123
2 + import os
2 + import os
3 | # External code
4 | import re # noqa: V123
5 | # Valid noqa
Expand All @@ -33,7 +33,7 @@ help: Remove the `# noqa` comment
2 | import os # noqa: INVALID123
3 | # External code
- import re # noqa: V123
4 + import re
4 + import re
5 | # Valid noqa
6 | import sys # noqa: E402
7 | from functools import cache # Preceeding comment # noqa: F401, INVALID456
Expand Down Expand Up @@ -73,7 +73,7 @@ help: Remove the `# noqa` comment
6 | import sys # noqa: E402
7 | from functools import cache # Preceeding comment # noqa: F401, INVALID456
- from itertools import product # Preceeding comment # noqa: INVALID789
8 + from itertools import product # Preceeding comment
8 + from itertools import product # Preceeding comment
9 | # Succeeding comment
10 | import math # noqa: INVALID000 # Succeeding comment
11 | # Mixed valid and invalid
Expand All @@ -93,7 +93,7 @@ help: Remove the `# noqa` comment
8 | from itertools import product # Preceeding comment # noqa: INVALID789
9 | # Succeeding comment
- import math # noqa: INVALID000 # Succeeding comment
10 + import math # Succeeding comment
10 + import math # Succeeding comment
11 | # Mixed valid and invalid
12 | from typing import List # noqa: F401, INVALID123
13 | # Test for multiple invalid
Expand Down Expand Up @@ -196,6 +196,7 @@ help: Remove the rule code `INVALID400`
18 + import json # noqa: E402, V100
19 | # Test for rule redirects
20 | import pandas as pd # noqa: TCH002
21 | # Invalid code with trailing reason (single comment)

RUF102 [*] Invalid rule code in `# noqa`: V100
--> RUF102.py:18:40
Expand All @@ -215,3 +216,19 @@ help: Remove the rule code `V100`
18 + import json # noqa: E402, INVALID400
19 | # Test for rule redirects
20 | import pandas as pd # noqa: TCH002
21 | # Invalid code with trailing reason (single comment)

RUF102 [*] Invalid rule code in `# noqa`: INVALID123
--> RUF102.py:22:17
|
20 | import pandas as pd # noqa: TCH002
21 | # Invalid code with trailing reason (single comment)
22 | import pathlib # noqa: INVALID123 some reason
| ^^^^^^^^^^^^^^^^^^
|
help: Remove the `# noqa` comment
19 | # Test for rule redirects
20 | import pandas as pd # noqa: TCH002
21 | # Invalid code with trailing reason (single comment)
- import pathlib # noqa: INVALID123 some reason
22 + import pathlib
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID123
help: Remove the `# noqa` comment
1 | # Invalid code
- import os # noqa: INVALID123
2 + import os
2 + import os
3 | # External code
4 | import re # noqa: V123
5 | # Valid noqa
Expand Down Expand Up @@ -53,7 +53,7 @@ help: Remove the `# noqa` comment
6 | import sys # noqa: E402
7 | from functools import cache # Preceeding comment # noqa: F401, INVALID456
- from itertools import product # Preceeding comment # noqa: INVALID789
8 + from itertools import product # Preceeding comment
8 + from itertools import product # Preceeding comment
9 | # Succeeding comment
10 | import math # noqa: INVALID000 # Succeeding comment
11 | # Mixed valid and invalid
Expand All @@ -73,7 +73,7 @@ help: Remove the `# noqa` comment
8 | from itertools import product # Preceeding comment # noqa: INVALID789
9 | # Succeeding comment
- import math # noqa: INVALID000 # Succeeding comment
10 + import math # Succeeding comment
10 + import math # Succeeding comment
11 | # Mixed valid and invalid
12 | from typing import List # noqa: F401, INVALID123
13 | # Test for multiple invalid
Expand Down Expand Up @@ -176,3 +176,19 @@ help: Remove the rule code `INVALID400`
18 + import json # noqa: E402, V100
19 | # Test for rule redirects
20 | import pandas as pd # noqa: TCH002
21 | # Invalid code with trailing reason (single comment)

RUF102 [*] Invalid rule code in `# noqa`: INVALID123
--> RUF102.py:22:17
|
20 | import pandas as pd # noqa: TCH002
21 | # Invalid code with trailing reason (single comment)
22 | import pathlib # noqa: INVALID123 some reason
| ^^^^^^^^^^^^^^^^^^
|
help: Remove the `# noqa` comment
19 | # Test for rule redirects
20 | import pandas as pd # noqa: TCH002
21 | # Invalid code with trailing reason (single comment)
- import pathlib # noqa: INVALID123 some reason
22 + import pathlib