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
51 changes: 51 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/suppressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,57 @@ def f():
print("hello")


def f():
# Should only cover the first statement, leaving a single diagnostic for bar
# ruff: ignore[F841]
foo = 0
bar = 0


def f():
# Should only cover the first statement, leaving a single diagnostic for bar
foo = 0 # ruff: ignore[F841]
bar = 0


def f():
# Should only cover the multiline statement, leaving a single diagnostic for bar
foo = """
value
""" # ruff: ignore[F841]
bar = 0


# ruff: ignore[ARG001] should cover the entire def
def f(
foo,
bar,
):
print("hello")


def f(
# ruff: ignore[ARG001] should only cover the first argument
foo,
bar,
):
print("hello")


def f(
foo, # ruff: ignore[ARG001] should only cover the first argument
bar,
):
print("hello")


def f(
foo,
bar,
): # ruff: ignore[ARG001] should cover nothing and be marked as unused
pass


# Ensure LAST suppression in file is reported.
# https://github.com/astral-sh/ruff/issues/23235
# ruff:disable[F401]
Expand Down
12 changes: 8 additions & 4 deletions crates/ruff_linter/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ pub fn add_noqa_to_path(
);

// Parse range suppression comments
let suppressions = Suppressions::from_tokens(locator.contents(), parsed.tokens(), &indexer);
let suppressions =
Suppressions::from_tokens(locator.contents(), parsed.tokens(), &indexer, settings);

// Generate diagnostics, ignoring any existing `noqa` directives.
let diagnostics = check_path(
Expand Down Expand Up @@ -479,7 +480,8 @@ pub fn lint_only(
);

// Parse range suppression comments
let suppressions = Suppressions::from_tokens(locator.contents(), parsed.tokens(), &indexer);
let suppressions =
Suppressions::from_tokens(locator.contents(), parsed.tokens(), &indexer, settings);

// Generate diagnostics.
let diagnostics = check_path(
Expand Down Expand Up @@ -596,7 +598,8 @@ pub fn lint_fix<'a>(
);

// Parse range suppression comments
let suppressions = Suppressions::from_tokens(locator.contents(), parsed.tokens(), &indexer);
let suppressions =
Suppressions::from_tokens(locator.contents(), parsed.tokens(), &indexer, settings);

// Generate diagnostics.
let diagnostics = check_path(
Expand Down Expand Up @@ -978,7 +981,8 @@ mod tests {
&locator,
&indexer,
);
let suppressions = Suppressions::from_tokens(locator.contents(), parsed.tokens(), &indexer);
let suppressions =
Suppressions::from_tokens(locator.contents(), parsed.tokens(), &indexer, settings);
let mut diagnostics = check_path(
path,
None,
Expand Down
5 changes: 5 additions & 0 deletions crates/ruff_linter/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,8 @@ pub(crate) const fn is_trailing_pragma_in_line_length_enabled(preview: PreviewMo
pub(crate) const fn is_collapsible_if_fix_safe_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
}

// https://github.com/astral-sh/ruff/pull/23404
pub(crate) const fn is_ruff_ignore_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
}
3 changes: 2 additions & 1 deletion crates/ruff_linter/src/rules/pyflakes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,8 @@ mod tests {
&locator,
&indexer,
);
let suppressions = Suppressions::from_tokens(locator.contents(), parsed.tokens(), &indexer);
let suppressions =
Suppressions::from_tokens(locator.contents(), parsed.tokens(), &indexer, &settings);
let mut messages = check_path(
Path::new("<filename>"),
None,
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff_linter/src/rules/ruff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,13 +489,15 @@ mod tests {
Path::new("ruff/suppressions.py"),
&settings::LinterSettings::for_rules(vec![
Rule::UnusedVariable,
Rule::UnusedFunctionArgument,
Rule::AmbiguousVariableName,
Rule::UnusedNOQA,
Rule::InvalidRuleCode,
Rule::InvalidSuppressionComment,
Rule::UnmatchedSuppressionComment,
])
.with_external_rules(&["TK421"]),
.with_external_rules(&["TK421"])
.with_preview_mode(),
)?;
assert_diagnostics!(diagnostics);
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,21 +406,116 @@ help: Remove suppression comment
112 |
note: This is an unsafe fix and may change runtime behavior

F841 [*] Local variable `bar` is assigned to but never used
--> suppressions.py:118:5
|
116 | # ruff: ignore[F841]
117 | foo = 0
118 | bar = 0
| ^^^
|
help: Remove assignment to unused variable `bar`
115 | # Should only cover the first statement, leaving a single diagnostic for bar
116 | # ruff: ignore[F841]
117 | foo = 0
- bar = 0
118 |
119 |
120 | def f():
note: This is an unsafe fix and may change runtime behavior

F841 [*] Local variable `bar` is assigned to but never used
--> suppressions.py:124:5
|
122 | # Should only cover the first statement, leaving a single diagnostic for bar
123 | foo = 0 # ruff: ignore[F841]
124 | bar = 0
| ^^^
|
help: Remove assignment to unused variable `bar`
121 | def f():
122 | # Should only cover the first statement, leaving a single diagnostic for bar
123 | foo = 0 # ruff: ignore[F841]
- bar = 0
124 |
125 |
126 | def f():
note: This is an unsafe fix and may change runtime behavior

F841 [*] Local variable `bar` is assigned to but never used
--> suppressions.py:132:5
|
130 | value
131 | """ # ruff: ignore[F841]
132 | bar = 0
| ^^^
|
help: Remove assignment to unused variable `bar`
129 | foo = """
130 | value
131 | """ # ruff: ignore[F841]
- bar = 0
132 |
133 |
134 | # ruff: ignore[ARG001] should cover the entire def
note: This is an unsafe fix and may change runtime behavior

ARG001 Unused function argument: `bar`
--> suppressions.py:146:5
|
144 | # ruff: ignore[ARG001] should only cover the first argument
145 | foo,
146 | bar,
| ^^^
147 | ):
148 | print("hello")
|

ARG001 Unused function argument: `bar`
--> suppressions.py:153:5
|
151 | def f(
152 | foo, # ruff: ignore[ARG001] should only cover the first argument
153 | bar,
| ^^^
154 | ):
155 | print("hello")
|

RUF100 [*] Unused suppression (unused: `ARG001`)
--> suppressions.py:161:5
|
159 | foo,
160 | bar,
161 | ): # ruff: ignore[ARG001] should cover nothing and be marked as unused
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
162 | pass
|
help: Remove unused suppression
158 | def f(
159 | foo,
160 | bar,
- ): # ruff: ignore[ARG001] should cover nothing and be marked as unused
161 + ):
162 | pass
163 |
164 |

RUF100 [*] Unused suppression (non-enabled: `F401`)
--> suppressions.py:116:1
--> suppressions.py:167:1
|
114 | # Ensure LAST suppression in file is reported.
115 | # https://github.com/astral-sh/ruff/issues/23235
116 | # ruff:disable[F401]
165 | # Ensure LAST suppression in file is reported.
166 | # https://github.com/astral-sh/ruff/issues/23235
167 | # ruff:disable[F401]
| ^^^^^^^^^^^^^^^^^^^^
117 | print("goodbye")
118 | # ruff:enable[F401]
168 | print("goodbye")
169 | # ruff:enable[F401]
| -------------------
|
help: Remove unused suppression
113 |
114 | # Ensure LAST suppression in file is reported.
115 | # https://github.com/astral-sh/ruff/issues/23235
164 |
165 | # Ensure LAST suppression in file is reported.
166 | # https://github.com/astral-sh/ruff/issues/23235
- # ruff:disable[F401]
116 | print("goodbye")
167 | print("goodbye")
- # ruff:enable[F401]
Loading
Loading