Skip to content

Commit

Permalink
Annotate RUF100 messages with unmatched, disabled, and unknown codes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Dec 24, 2022
1 parent 4888afd commit 9a7331b
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 137 deletions.
4 changes: 2 additions & 2 deletions resources/test/fixtures/ruff/RUF100.py
Expand Up @@ -15,8 +15,8 @@ def f() -> None:
# Invalid
d = 1 # noqa: F841, E501

# Invalid (and unimplemented)
d = 1 # noqa: F841, W191
# Invalid (and unimplemented or not enabled)
d = 1 # noqa: F841, W191, F821

# Invalid (but external)
d = 1 # noqa: F841, V101
Expand Down
42 changes: 33 additions & 9 deletions src/checkers/noqa.rs
@@ -1,11 +1,13 @@
//! `NoQA` enforcement and validation.

use std::str::FromStr;

use nohash_hasher::IntMap;
use rustpython_parser::ast::Location;

use crate::ast::types::Range;
use crate::autofix::Fix;
use crate::checks::{Check, CheckCode, CheckKind, CODE_REDIRECTS};
use crate::checks::{Check, CheckCode, CheckKind, UnusedCodes, CODE_REDIRECTS};
use crate::noqa;
use crate::noqa::{is_file_exempt, Directive};
use crate::settings::{flags, Settings};
Expand Down Expand Up @@ -98,18 +100,29 @@ pub fn check_noqa(
}
}
Directive::Codes(spaces, start, end, codes) => {
let mut invalid_codes = vec![];
let mut disabled_codes = vec![];
let mut unknown_codes = vec![];
let mut unmatched_codes = vec![];
let mut valid_codes = vec![];
let mut self_ignore = false;
for code in codes {
let code = CODE_REDIRECTS.get(code).map_or(code, AsRef::as_ref);
if code == CheckCode::RUF100.as_ref() {
self_ignore = true;
break;
}

if matches.contains(&code) || settings.external.contains(code) {
valid_codes.push(code);
} else {
if matches.contains(&code) || settings.external.contains(code) {
valid_codes.push(code);
if let Ok(check_code) = CheckCode::from_str(code) {
if settings.enabled.contains(&check_code) {
unmatched_codes.push(code);
} else {
disabled_codes.push(code);
}
} else {
invalid_codes.push(code);
unknown_codes.push(code);
}
}
}
Expand All @@ -118,14 +131,25 @@ pub fn check_noqa(
continue;
}

if !invalid_codes.is_empty() {
if !(disabled_codes.is_empty()
&& unknown_codes.is_empty()
&& unmatched_codes.is_empty())
{
let mut check = Check::new(
CheckKind::UnusedNOQA(Some(
invalid_codes
CheckKind::UnusedNOQA(Some(UnusedCodes {
disabled: disabled_codes
.iter()
.map(|code| (*code).to_string())
.collect(),
unknown: unknown_codes
.iter()
.map(|code| (*code).to_string())
.collect(),
unmatched: unmatched_codes
.iter()
.map(|code| (*code).to_string())
.collect(),
)),
})),
Range {
location: Location::new(row + 1, start),
end_location: Location::new(row + 1, end),
Expand Down

0 comments on commit 9a7331b

Please sign in to comment.