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

Handle multi-byte lines in RUF100 #2392

Merged
merged 1 commit into from
Jan 31, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions resources/test/fixtures/ruff/RUF100_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ def f() -> None:
import sys # noqa: F401, RUF100

print(sys.path)

"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
10 changes: 8 additions & 2 deletions src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ pub fn check_noqa(
if enforce_noqa {
for (row, (directive, matches)) in noqa_directives {
match directive {
Directive::All(spaces, start, end) => {
Directive::All(spaces, start_byte, end_byte) => {
if matches.is_empty() {
let start = lines[row][..start_byte].chars().count();
let end = start + lines[row][start_byte..end_byte].chars().count();

let mut diagnostic = Diagnostic::new(
violations::UnusedNOQA { codes: None },
Range::new(Location::new(row + 1, start), Location::new(row + 1, end)),
Expand All @@ -117,7 +120,7 @@ pub fn check_noqa(
diagnostics.push(diagnostic);
}
}
Directive::Codes(spaces, start, end, codes) => {
Directive::Codes(spaces, start_byte, end_byte, codes) => {
let mut disabled_codes = vec![];
let mut unknown_codes = vec![];
let mut unmatched_codes = vec![];
Expand Down Expand Up @@ -153,6 +156,9 @@ pub fn check_noqa(
&& unknown_codes.is_empty()
&& unmatched_codes.is_empty())
{
let start = lines[row][..start_byte].chars().count();
let end = start + lines[row][start_byte..end_byte].chars().count();

let mut diagnostic = Diagnostic::new(
violations::UnusedNOQA {
codes: Some(UnusedCodes {
Expand Down
6 changes: 3 additions & 3 deletions src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ fn add_noqa_inner(
output.push_str(line_ending);
count += 1;
}
Directive::Codes(_, start, _, existing) => {
Directive::Codes(_, start_byte, _, existing) => {
// Reconstruct the line based on the preserved rule codes.
// This enables us to tally the number of edits.
let mut formatted = String::new();
let mut formatted = String::with_capacity(line.len());

// Add existing content.
formatted.push_str(line[..start].trim_end());
formatted.push_str(line[..start_byte].trim_end());

// Add `noqa` directive.
formatted.push_str(" # noqa: ");
Expand Down
35 changes: 35 additions & 0 deletions src/rules/ruff/snapshots/ruff__rules__ruff__tests__ruf100_0.snap
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,39 @@ expression: diagnostics
row: 86
column: 0
parent: ~
- kind:
LineTooLong:
- 103
- 88
location:
row: 90
column: 88
end_location:
row: 90
column: 103
fix: ~
parent: ~
- kind:
UnusedNOQA:
codes:
unknown: []
disabled: []
unmatched:
- F401
location:
row: 90
column: 91
end_location:
row: 90
column: 103
fix:
content:
- ""
location:
row: 90
column: 89
end_location:
row: 90
column: 103
parent: ~