Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match rule prefixes from external codes setting in unused-noqa #8177

Merged
merged 2 commits into from Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF100_0.py
Expand Up @@ -21,6 +21,9 @@ def f() -> None:
# Invalid (but external)
d = 1 # noqa: F841, V101

# Invalid (but external)
d = 1 # noqa: V500

# fmt: off
# Invalid - no space before #
d = 1# noqa: E501
Expand Down
6 changes: 6 additions & 0 deletions crates/ruff_linter/src/checkers/noqa.rs
Expand Up @@ -128,7 +128,13 @@ pub(crate) fn check_noqa(
}

if line.matches.iter().any(|match_| *match_ == code)
// Perform a fast-check for exact matches first
|| settings.external.contains(code)
// then check for matching prefixes
|| settings
.external
.iter()
.any(|external| code.starts_with(external))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since most codes won't match an external, I bet it's better to just do this iteration every time and skip the direct check, but obviously can't say for sure.

It's probably even better to just use a vector instead of a hash set, since we're doing this iteration every time, and the set of externals is gonna tend to be quite small.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Defer to you as to whether you want to change.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okee

{
valid_codes.push(code);
} else {
Expand Down
19 changes: 19 additions & 0 deletions crates/ruff_linter/src/rules/ruff/mod.rs
Expand Up @@ -120,6 +120,25 @@ mod tests {
Ok(())
}

#[test]
fn ruf100_0_prefix() -> Result<()> {
let diagnostics = test_path(
Path::new("ruff/RUF100_0.py"),
&settings::LinterSettings {
external: FxHashSet::from_iter(vec!["V".to_string()]),
..settings::LinterSettings::for_rules(vec![
Rule::UnusedNOQA,
Rule::LineTooLong,
Rule::UnusedImport,
Rule::UnusedVariable,
Rule::TabIndentation,
])
},
)?;
assert_messages!(diagnostics);
Ok(())
}

#[test]
fn ruf100_1() -> Result<()> {
let diagnostics = test_path(
Expand Down
Expand Up @@ -86,7 +86,7 @@ RUF100_0.py:22:12: RUF100 [*] Unused `noqa` directive (unused: `F841`)
22 | d = 1 # noqa: F841, V101
| ^^^^^^^^^^^^^^^^^^ RUF100
23 |
24 | # fmt: off
24 | # Invalid (but external)
|
= help: Remove unused `noqa` directive

Expand All @@ -97,171 +97,191 @@ RUF100_0.py:22:12: RUF100 [*] Unused `noqa` directive (unused: `F841`)
22 |- d = 1 # noqa: F841, V101
22 |+ d = 1 # noqa: V101
23 23 |
24 24 | # fmt: off
25 25 | # Invalid - no space before #
24 24 | # Invalid (but external)
25 25 | d = 1 # noqa: V500

RUF100_0.py:26:10: RUF100 [*] Unused `noqa` directive (unused: `E501`)
RUF100_0.py:25:12: RUF100 [*] Unused `noqa` directive (unknown: `V500`)
|
24 | # fmt: off
25 | # Invalid - no space before #
26 | d = 1# noqa: E501
| ^^^^^^^^^^^^ RUF100
27 |
28 | # Invalid - many spaces before #
24 | # Invalid (but external)
25 | d = 1 # noqa: V500
| ^^^^^^^^^^^^ RUF100
26 |
27 | # fmt: off
|
= help: Remove unused `noqa` directive

ℹ Fix
22 22 | d = 1 # noqa: F841, V101
23 23 |
24 24 | # fmt: off
25 25 | # Invalid - no space before #
26 |- d = 1# noqa: E501
26 |+ d = 1
27 27 |
28 28 | # Invalid - many spaces before #
29 29 | d = 1 # noqa: E501
24 24 | # Invalid (but external)
25 |- d = 1 # noqa: V500
25 |+ d = 1
26 26 |
27 27 | # fmt: off
28 28 | # Invalid - no space before #

RUF100_0.py:29:10: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
27 | # fmt: off
28 | # Invalid - no space before #
29 | d = 1# noqa: E501
| ^^^^^^^^^^^^ RUF100
30 |
31 | # Invalid - many spaces before #
|
= help: Remove unused `noqa` directive

ℹ Fix
26 26 |
27 27 | # fmt: off
28 28 | # Invalid - no space before #
29 |- d = 1# noqa: E501
29 |+ d = 1
30 30 |
31 31 | # Invalid - many spaces before #
32 32 | d = 1 # noqa: E501

RUF100_0.py:29:5: F841 [*] Local variable `d` is assigned to but never used
RUF100_0.py:32:5: F841 [*] Local variable `d` is assigned to but never used
|
28 | # Invalid - many spaces before #
29 | d = 1 # noqa: E501
31 | # Invalid - many spaces before #
32 | d = 1 # noqa: E501
| ^ F841
30 | # fmt: on
33 | # fmt: on
|
= help: Remove assignment to unused variable `d`

ℹ Suggested fix
26 26 | d = 1# noqa: E501
27 27 |
28 28 | # Invalid - many spaces before #
29 |- d = 1 # noqa: E501
30 29 | # fmt: on
31 30 |
32 31 |
29 29 | d = 1# noqa: E501
30 30 |
31 31 | # Invalid - many spaces before #
32 |- d = 1 # noqa: E501
33 32 | # fmt: on
34 33 |
35 34 |

RUF100_0.py:29:33: RUF100 [*] Unused `noqa` directive (unused: `E501`)
RUF100_0.py:32:33: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
28 | # Invalid - many spaces before #
29 | d = 1 # noqa: E501
31 | # Invalid - many spaces before #
32 | d = 1 # noqa: E501
| ^^^^^^^^^^^^ RUF100
30 | # fmt: on
33 | # fmt: on
|
= help: Remove unused `noqa` directive

ℹ Fix
26 26 | d = 1# noqa: E501
27 27 |
28 28 | # Invalid - many spaces before #
29 |- d = 1 # noqa: E501
29 |+ d = 1
30 30 | # fmt: on
31 31 |
32 32 |
29 29 | d = 1# noqa: E501
30 30 |
31 31 | # Invalid - many spaces before #
32 |- d = 1 # noqa: E501
32 |+ d = 1
33 33 | # fmt: on
34 34 |
35 35 |

RUF100_0.py:55:6: RUF100 [*] Unused `noqa` directive (unused: `F841`)
RUF100_0.py:58:6: RUF100 [*] Unused `noqa` directive (unused: `F841`)
|
54 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
55 | """ # noqa: E501, F841
57 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
58 | """ # noqa: E501, F841
| ^^^^^^^^^^^^^^^^^^ RUF100
56 |
57 | # Invalid
59 |
60 | # Invalid
|
= help: Remove unused `noqa` directive

ℹ Fix
52 52 | https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
53 53 |
54 54 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
55 |-""" # noqa: E501, F841
55 |+""" # noqa: E501
55 55 | https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
56 56 |
57 57 | # Invalid
58 58 | _ = """Lorem ipsum dolor sit amet.
57 57 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
58 |-""" # noqa: E501, F841
58 |+""" # noqa: E501
59 59 |
60 60 | # Invalid
61 61 | _ = """Lorem ipsum dolor sit amet.

RUF100_0.py:63:6: RUF100 [*] Unused `noqa` directive (unused: `E501`)
RUF100_0.py:66:6: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
62 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
63 | """ # noqa: E501
65 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
66 | """ # noqa: E501
| ^^^^^^^^^^^^ RUF100
64 |
65 | # Invalid
67 |
68 | # Invalid
|
= help: Remove unused `noqa` directive

ℹ Fix
60 60 | https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
61 61 |
62 62 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
63 |-""" # noqa: E501
63 |+"""
63 63 | https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
64 64 |
65 65 | # Invalid
66 66 | _ = """Lorem ipsum dolor sit amet.
65 65 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
66 |-""" # noqa: E501
66 |+"""
67 67 |
68 68 | # Invalid
69 69 | _ = """Lorem ipsum dolor sit amet.

RUF100_0.py:71:6: RUF100 [*] Unused blanket `noqa` directive
RUF100_0.py:74:6: RUF100 [*] Unused blanket `noqa` directive
|
70 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
71 | """ # noqa
73 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
74 | """ # noqa
| ^^^^^^ RUF100
72 |
73 | # Valid
75 |
76 | # Valid
|
= help: Remove unused `noqa` directive

ℹ Fix
68 68 | https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
69 69 |
70 70 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
71 |-""" # noqa
71 |+"""
71 71 | https://github.com/PyCQA/pycodestyle/pull/258/files#diff-841c622497a8033d10152bfdfb15b20b92437ecdea21a260944ea86b77b51533
72 72 |
73 73 | # Valid
74 74 | # this is a veryyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy long comment # noqa: E501
73 73 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
74 |-""" # noqa
74 |+"""
75 75 |
76 76 | # Valid
77 77 | # this is a veryyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy long comment # noqa: E501

RUF100_0.py:85:8: F401 [*] `shelve` imported but unused
RUF100_0.py:88:8: F401 [*] `shelve` imported but unused
|
83 | import collections # noqa
84 | import os # noqa: F401, RUF100
85 | import shelve # noqa: RUF100
86 | import collections # noqa
87 | import os # noqa: F401, RUF100
88 | import shelve # noqa: RUF100
| ^^^^^^ F401
86 | import sys # noqa: F401, RUF100
89 | import sys # noqa: F401, RUF100
|
= help: Remove unused import: `shelve`

ℹ Fix
82 82 |
83 83 | import collections # noqa
84 84 | import os # noqa: F401, RUF100
85 |-import shelve # noqa: RUF100
86 85 | import sys # noqa: F401, RUF100
87 86 |
88 87 | print(sys.path)
85 85 |
86 86 | import collections # noqa
87 87 | import os # noqa: F401, RUF100
88 |-import shelve # noqa: RUF100
89 88 | import sys # noqa: F401, RUF100
90 89 |
91 90 | print(sys.path)

RUF100_0.py:90:89: E501 Line too long (89 > 88)
RUF100_0.py:93:89: E501 Line too long (89 > 88)
|
88 | print(sys.path)
89 |
90 | "shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
91 | print(sys.path)
92 |
93 | "shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
| ^ E501
|

RUF100_0.py:90:92: RUF100 [*] Unused `noqa` directive (unused: `F401`)
RUF100_0.py:93:92: RUF100 [*] Unused `noqa` directive (unused: `F401`)
|
88 | print(sys.path)
89 |
90 | "shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
91 | print(sys.path)
92 |
93 | "shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
| ^^^^^^^^^^^^ RUF100
|
= help: Remove unused `noqa` directive

ℹ Fix
87 87 |
88 88 | print(sys.path)
89 89 |
90 |-"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
90 |+"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]"
91 91 |
90 90 |
91 91 | print(sys.path)
92 92 |
93 93 | def f():
93 |-"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
93 |+"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]"
94 94 |
95 95 |
96 96 | def f():