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

Ignore end-of-line file exemption comments #6160

Merged
merged 2 commits into from
Jul 29, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/ruff/resources/test/fixtures/ruff/ruff_noqa_invalid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os # ruff: noqa: F401


def f():
x = 1
43 changes: 28 additions & 15 deletions crates/ruff/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use ruff_python_ast::Ranged;
use ruff_text_size::{TextLen, TextRange, TextSize};

use ruff_diagnostics::Diagnostic;
use ruff_python_trivia::indentation_at_offset;
use ruff_source_file::{LineEnding, Locator};

use crate::codes::NoqaCode;
Expand Down Expand Up @@ -248,22 +249,34 @@ impl FileExemption {
let path_display = relativize_path(path);
warn!("Invalid `# ruff: noqa` directive at {path_display}:{line}: {err}");
}
Ok(Some(ParsedFileExemption::All)) => {
return Some(Self::All);
}
Ok(Some(ParsedFileExemption::Codes(codes))) => {
exempt_codes.extend(codes.into_iter().filter_map(|code| {
if let Ok(rule) = Rule::from_code(get_redirect_target(code).unwrap_or(code))
{
Some(rule.noqa_code())
} else {
#[allow(deprecated)]
let line = locator.compute_line_index(range.start());
let path_display = relativize_path(path);
warn!("Invalid code provided to `# ruff: noqa` at {path_display}:{line}: {code}");
None
Ok(Some(exemption)) => {
if indentation_at_offset(range.start(), locator).is_none() {
#[allow(deprecated)]
let line = locator.compute_line_index(range.start());
let path_display = relativize_path(path);
warn!("Unexpected `# ruff: noqa` directive at {path_display}:{line}. File-level suppression comments must appear on their own line.");
continue;
}

match exemption {
ParsedFileExemption::All => {
return Some(Self::All);
}
}));
ParsedFileExemption::Codes(codes) => {
exempt_codes.extend(codes.into_iter().filter_map(|code| {
if let Ok(rule) = Rule::from_code(get_redirect_target(code).unwrap_or(code))
{
Some(rule.noqa_code())
} else {
#[allow(deprecated)]
let line = locator.compute_line_index(range.start());
let path_display = relativize_path(path);
warn!("Invalid rule code provided to `# ruff: noqa` at {path_display}:{line}: {code}");
None
}
}));
}
}
}
Ok(None) => {}
}
Expand Down
18 changes: 14 additions & 4 deletions crates/ruff/src/rules/ruff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,29 @@ mod tests {
}

#[test]
fn ruff_noqa() -> Result<()> {
fn ruff_noqa_all() -> Result<()> {
let diagnostics = test_path(
Path::new("ruff/ruff_noqa.py"),
Path::new("ruff/ruff_noqa_all.py"),
&settings::Settings::for_rules(vec![Rule::UnusedImport, Rule::UnusedVariable]),
)?;
assert_messages!(diagnostics);
Ok(())
}

#[test]
fn ruff_targeted_noqa() -> Result<()> {
fn ruff_noqa_codes() -> Result<()> {
let diagnostics = test_path(
Path::new("ruff/ruff_targeted_noqa.py"),
Path::new("ruff/ruff_noqa_codes.py"),
&settings::Settings::for_rules(vec![Rule::UnusedImport, Rule::UnusedVariable]),
)?;
assert_messages!(diagnostics);
Ok(())
}

#[test]
fn ruff_noqa_invalid() -> Result<()> {
let diagnostics = test_path(
Path::new("ruff/ruff_noqa_invalid.py"),
&settings::Settings::for_rules(vec![Rule::UnusedImport, Rule::UnusedVariable]),
)?;
assert_messages!(diagnostics);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff/src/rules/ruff/mod.rs
---
ruff_targeted_noqa.py:8:5: F841 [*] Local variable `x` is assigned to but never used
ruff_noqa_codes.py:8:5: F841 [*] Local variable `x` is assigned to but never used
|
7 | def f():
8 | x = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: crates/ruff/src/rules/ruff/mod.rs
---
ruff_noqa_invalid.py:1:8: F401 [*] `os` imported but unused
|
1 | import os # ruff: noqa: F401
| ^^ F401
|
= help: Remove unused import: `os`

ℹ Fix
1 |-import os # ruff: noqa: F401
2 1 |
3 2 |
4 3 | def f():

ruff_noqa_invalid.py:5:5: F841 [*] Local variable `x` is assigned to but never used
|
4 | def f():
5 | x = 1
| ^ F841
|
= help: Remove assignment to unused variable `x`

ℹ Suggested fix
2 2 |
3 3 |
4 4 | def f():
5 |- x = 1
5 |+ pass


2 changes: 1 addition & 1 deletion crates/ruff_python_ast/src/whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn indentation<'a, T>(locator: &'a Locator, located: &T) -> Option<&'a str>
where
T: Ranged,
{
indentation_at_offset(locator, located.start())
indentation_at_offset(located.start(), locator)
}

/// Return the end offset at which the empty lines following a statement.
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ fn handle_own_line_comment_between_branches<'a>(

// It depends on the indentation level of the comment if it is a leading comment for the
// following branch or if it a trailing comment of the previous body's last statement.
let comment_indentation = indentation_at_offset(locator, comment.slice().range().start())
let comment_indentation = indentation_at_offset(comment.slice().range().start(), locator)
.unwrap_or_default()
.len();

Expand Down Expand Up @@ -402,7 +402,7 @@ fn handle_match_comment<'a>(

let next_case = match_stmt.cases.get(current_case_index + 1);

let comment_indentation = indentation_at_offset(locator, comment.slice().range().start())
let comment_indentation = indentation_at_offset(comment.slice().range().start(), locator)
.unwrap_or_default()
.len();
let match_case_indentation = indentation(locator, match_case).unwrap().len();
Expand Down Expand Up @@ -480,7 +480,7 @@ fn handle_own_line_comment_after_branch<'a>(

// We only care about the length because indentations with mixed spaces and tabs are only valid if
// the indent-level doesn't depend on the tab width (the indent level must be the same if the tab width is 1 or 8).
let comment_indentation = indentation_at_offset(locator, comment.slice().range().start())
let comment_indentation = indentation_at_offset(comment.slice().range().start(), locator)
.unwrap_or_default()
.len();

Expand All @@ -493,7 +493,7 @@ fn handle_own_line_comment_after_branch<'a>(
// # Trailing if comment
// ```
// Here we keep the comment a trailing comment of the `if`
let preceding_indentation = indentation_at_offset(locator, preceding_node.start())
let preceding_indentation = indentation_at_offset(preceding_node.start(), locator)
.unwrap_or_default()
.len();
if comment_indentation == preceding_indentation {
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_trivia/src/whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ruff_source_file::Locator;
use ruff_text_size::{TextRange, TextSize};

/// Extract the leading indentation from a line.
pub fn indentation_at_offset<'a>(locator: &'a Locator, offset: TextSize) -> Option<&'a str> {
pub fn indentation_at_offset<'a>(offset: TextSize, locator: &'a Locator) -> Option<&'a str> {
let line_start = locator.line_start(offset);
let indentation = &locator.contents()[TextRange::new(line_start, offset)];

Expand Down
Loading