Skip to content

Commit

Permalink
Respect external codes in file-level exemptions
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 3, 2024
1 parent c9931a5 commit d6b6488
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
41 changes: 40 additions & 1 deletion crates/ruff/tests/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ fn deprecated_config_option_overridden_via_cli() {
success: false
exit_code: 1
----- stdout -----
-:1:7: N801 Class name `lowercase` should use CapWords convention
-:1:7: N801 Class name `lowercase` should use CapWords convention
Found 1 error.
----- stderr -----
Expand Down Expand Up @@ -933,3 +933,42 @@ include = ["*.ipy"]

Ok(())
}

#[test]
fn file_noqa_external() -> Result<()> {
let tempdir = TempDir::new()?;
let ruff_toml = tempdir.path().join("ruff.toml");
fs::write(
&ruff_toml,
r#"
[lint]
external = ["AAA"]
"#,
)?;

insta::with_settings!({
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
}, {
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS)
.arg("--config")
.arg(&ruff_toml)
.arg("-")
.pass_stdin(r#"
# flake8: noqa: AAA101, BBB102
import os
"#), @r###"
success: false
exit_code: 1
----- stdout -----
-:3:8: F401 [*] `os` imported but unused
Found 1 error.
[*] 1 fixable with the `--fix` option.
----- stderr -----
warning: Invalid rule code provided to `# ruff: noqa` at -:2: BBB102
"###);
});

Ok(())
}
8 changes: 7 additions & 1 deletion crates/ruff_linter/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ pub(crate) fn check_noqa(
settings: &LinterSettings,
) -> Vec<usize> {
// Identify any codes that are globally exempted (within the current file).
let exemption = FileExemption::try_extract(locator.contents(), comment_ranges, path, locator);
let exemption = FileExemption::try_extract(
locator.contents(),
comment_ranges,
&settings.external,
path,
locator,
);

// Extract all `noqa` directives.
let mut noqa_directives = NoqaDirectives::from_commented_ranges(comment_ranges, path, locator);
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ pub fn add_noqa_to_path(
&diagnostics.0,
&locator,
indexer.comment_ranges(),
&settings.external,
&directives.noqa_line_for,
stylist.line_ending(),
)
Expand Down
18 changes: 17 additions & 1 deletion crates/ruff_linter/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ impl FileExemption {
pub(crate) fn try_extract(
contents: &str,
comment_ranges: &CommentRanges,
external: &[String],
path: &Path,
locator: &Locator,
) -> Option<Self> {
Expand Down Expand Up @@ -263,6 +264,13 @@ impl FileExemption {
}
ParsedFileExemption::Codes(codes) => {
exempt_codes.extend(codes.into_iter().filter_map(|code| {
// Ignore externally-defined rules.
if external
.iter()
.any(|external| code.starts_with(external)) {
return None;
}

if let Ok(rule) = Rule::from_code(get_redirect_target(code).unwrap_or(code))
{
Some(rule.noqa_code())
Expand Down Expand Up @@ -458,6 +466,7 @@ pub(crate) fn add_noqa(
diagnostics: &[Diagnostic],
locator: &Locator,
comment_ranges: &CommentRanges,
external: &[String],
noqa_line_for: &NoqaMapping,
line_ending: LineEnding,
) -> Result<usize> {
Expand All @@ -466,6 +475,7 @@ pub(crate) fn add_noqa(
diagnostics,
locator,
comment_ranges,
external,
noqa_line_for,
line_ending,
);
Expand All @@ -478,6 +488,7 @@ fn add_noqa_inner(
diagnostics: &[Diagnostic],
locator: &Locator,
comment_ranges: &CommentRanges,
external: &[String],
noqa_line_for: &NoqaMapping,
line_ending: LineEnding,
) -> (usize, String) {
Expand All @@ -487,7 +498,8 @@ fn add_noqa_inner(

// Whether the file is exempted from all checks.
// Codes that are globally exempted (within the current file).
let exemption = FileExemption::try_extract(locator.contents(), comment_ranges, path, locator);
let exemption =
FileExemption::try_extract(locator.contents(), comment_ranges, external, path, locator);
let directives = NoqaDirectives::from_commented_ranges(comment_ranges, path, locator);

// Mark any non-ignored diagnostics.
Expand Down Expand Up @@ -1001,6 +1013,7 @@ mod tests {
&[],
&Locator::new(contents),
&CommentRanges::default(),
&[],
&noqa_line_for,
LineEnding::Lf,
);
Expand All @@ -1021,6 +1034,7 @@ mod tests {
&diagnostics,
&Locator::new(contents),
&CommentRanges::default(),
&[],
&noqa_line_for,
LineEnding::Lf,
);
Expand Down Expand Up @@ -1048,6 +1062,7 @@ mod tests {
&diagnostics,
&Locator::new(contents),
&comment_ranges,
&[],
&noqa_line_for,
LineEnding::Lf,
);
Expand Down Expand Up @@ -1075,6 +1090,7 @@ mod tests {
&diagnostics,
&Locator::new(contents),
&comment_ranges,
&[],
&noqa_line_for,
LineEnding::Lf,
);
Expand Down

0 comments on commit d6b6488

Please sign in to comment.