Skip to content

Commit

Permalink
Avoid removing comment hash for noqa's with trailing content
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 17, 2023
1 parent 3ce7b04 commit cad380e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
2 changes: 2 additions & 0 deletions crates/ruff/resources/test/fixtures/ruff/RUF100_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# noqa # comment
print() # noqa
print() # noqa # comment
print() # noqa comment
print(a) # noqa
print(a) # noqa # comment
print(a) # noqa comment

# noqa: E501, F821
# noqa: E501, F821 # comment
Expand Down
18 changes: 16 additions & 2 deletions crates/ruff/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,16 @@ pub fn check_noqa(
Location::new(row + 1, start - leading_spaces),
Location::new(row + 1, end + trailing_spaces),
));
} else {
} else if lines[row].chars().nth(end + 1).map_or(false, |c| c == '#') {
diagnostic.amend(Fix::deletion(
Location::new(row + 1, start),
Location::new(row + 1, end + trailing_spaces),
));
} else {
diagnostic.amend(Fix::deletion(
Location::new(row + 1, start + 1),
Location::new(row + 1, end),
));
}
}
diagnostics.push(diagnostic);
Expand Down Expand Up @@ -242,11 +247,20 @@ pub fn check_noqa(
Location::new(row + 1, start - leading_spaces),
Location::new(row + 1, end + trailing_spaces),
));
} else {
} else if lines[row]
.chars()
.nth(end + 1)
.map_or(false, |c| c == '#')
{
diagnostic.amend(Fix::deletion(
Location::new(row + 1, start),
Location::new(row + 1, end + trailing_spaces),
));
} else {
diagnostic.amend(Fix::deletion(
Location::new(row + 1, start + 1),
Location::new(row + 1, end),
));
}
} else {
diagnostic.amend(Fix::replacement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,44 @@ expression: diagnostics
row: 4
column: 16
parent: ~
- kind:
name: UnusedNOQA
body: "Unused blanket `noqa` directive"
suggestion: "Remove unused `noqa` directive"
fixable: true
location:
row: 5
column: 9
end_location:
row: 5
column: 15
fix:
content: ""
location:
row: 5
column: 10
end_location:
row: 5
column: 15
parent: ~
- kind:
name: UnusedNOQA
body: "Unused `noqa` directive (unused: `E501`, `F821`)"
suggestion: "Remove unused `noqa` directive"
fixable: true
location:
row: 8
row: 10
column: 0
end_location:
row: 8
row: 10
column: 18
fix:
content: ""
location:
row: 8
row: 10
column: 0
end_location:
row: 9
row: 11
column: 0
parent: ~
- kind:
Expand All @@ -108,18 +128,18 @@ expression: diagnostics
suggestion: "Remove unused `noqa` directive"
fixable: true
location:
row: 9
row: 11
column: 0
end_location:
row: 9
row: 11
column: 18
fix:
content: ""
location:
row: 9
row: 11
column: 0
end_location:
row: 9
row: 11
column: 19
parent: ~
- kind:
Expand All @@ -128,18 +148,18 @@ expression: diagnostics
suggestion: "Remove unused `noqa` directive"
fixable: true
location:
row: 10
row: 12
column: 9
end_location:
row: 10
row: 12
column: 27
fix:
content: ""
location:
row: 10
row: 12
column: 7
end_location:
row: 10
row: 12
column: 27
parent: ~
- kind:
Expand All @@ -148,18 +168,18 @@ expression: diagnostics
suggestion: "Remove unused `noqa` directive"
fixable: true
location:
row: 11
row: 13
column: 9
end_location:
row: 11
row: 13
column: 27
fix:
content: ""
location:
row: 11
row: 13
column: 9
end_location:
row: 11
row: 13
column: 28
parent: ~
- kind:
Expand All @@ -168,18 +188,18 @@ expression: diagnostics
suggestion: "Remove unused `noqa` directive"
fixable: true
location:
row: 12
row: 14
column: 10
end_location:
row: 12
row: 14
column: 28
fix:
content: "# noqa: F821"
location:
row: 12
row: 14
column: 10
end_location:
row: 12
row: 14
column: 28
parent: ~
- kind:
Expand All @@ -188,18 +208,18 @@ expression: diagnostics
suggestion: "Remove unused `noqa` directive"
fixable: true
location:
row: 13
row: 15
column: 10
end_location:
row: 13
row: 15
column: 28
fix:
content: "# noqa: F821"
location:
row: 13
row: 15
column: 10
end_location:
row: 13
row: 15
column: 28
parent: ~

0 comments on commit cad380e

Please sign in to comment.