Skip to content

Commit 2823487

Browse files
Respect lint.exclude in ruff check --add-noqa (#13427)
## Summary Closes #13423.
1 parent 910fac7 commit 2823487

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

crates/ruff/src/commands/add_noqa.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use ruff_linter::linter::add_noqa_to_path;
1010
use ruff_linter::source_kind::SourceKind;
1111
use ruff_linter::warn_user_once;
1212
use ruff_python_ast::{PySourceType, SourceType};
13-
use ruff_workspace::resolver::{python_files_in_path, PyprojectConfig, ResolvedFile};
13+
use ruff_workspace::resolver::{
14+
match_exclusion, python_files_in_path, PyprojectConfig, ResolvedFile,
15+
};
1416

1517
use crate::args::ConfigArguments;
1618

@@ -57,6 +59,15 @@ pub(crate) fn add_noqa(
5759
.and_then(|parent| package_roots.get(parent))
5860
.and_then(|package| *package);
5961
let settings = resolver.resolve(path);
62+
if (settings.file_resolver.force_exclude || !resolved_file.is_root())
63+
&& match_exclusion(
64+
resolved_file.path(),
65+
resolved_file.file_name(),
66+
&settings.linter.exclude,
67+
)
68+
{
69+
return None;
70+
}
6071
let source_kind = match SourceKind::from_path(path, source_type) {
6172
Ok(Some(source_kind)) => source_kind,
6273
Ok(None) => return None,

crates/ruff/tests/lint.rs

+52
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,58 @@ print(
16191619
Ok(())
16201620
}
16211621

1622+
#[test]
1623+
fn add_noqa_exclude() -> Result<()> {
1624+
let tempdir = TempDir::new()?;
1625+
let ruff_toml = tempdir.path().join("ruff.toml");
1626+
fs::write(
1627+
&ruff_toml,
1628+
r#"
1629+
[lint]
1630+
exclude = ["excluded.py"]
1631+
select = ["RUF015"]
1632+
"#,
1633+
)?;
1634+
1635+
let test_path = tempdir.path().join("noqa.py");
1636+
1637+
fs::write(
1638+
&test_path,
1639+
r#"
1640+
def first_square():
1641+
return [x * x for x in range(20)][0]
1642+
"#,
1643+
)?;
1644+
1645+
let exclude_path = tempdir.path().join("excluded.py");
1646+
1647+
fs::write(
1648+
&exclude_path,
1649+
r#"
1650+
def first_square():
1651+
return [x * x for x in range(20)][0]
1652+
"#,
1653+
)?;
1654+
1655+
insta::with_settings!({
1656+
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
1657+
}, {
1658+
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
1659+
.current_dir(tempdir.path())
1660+
.args(STDIN_BASE_OPTIONS)
1661+
.args(["--add-noqa"]), @r###"
1662+
success: true
1663+
exit_code: 0
1664+
----- stdout -----
1665+
1666+
----- stderr -----
1667+
Added 1 noqa directive.
1668+
"###);
1669+
});
1670+
1671+
Ok(())
1672+
}
1673+
16221674
/// Infer `3.11` from `requires-python` in `pyproject.toml`.
16231675
#[test]
16241676
fn requires_python() -> Result<()> {

0 commit comments

Comments
 (0)