diff --git a/resources/test/fixtures/M001.py b/resources/test/fixtures/M001.py index 310d96de39..1c592e3142 100644 --- a/resources/test/fixtures/M001.py +++ b/resources/test/fixtures/M001.py @@ -15,6 +15,9 @@ def f() -> None: # Invalid d = 1 # noqa: F841, E501 + # Invalid (and unimplemented) + d = 1 # noqa: F841, W191 + # Valid _ = """Lorem ipsum dolor sit amet. diff --git a/src/check_lines.rs b/src/check_lines.rs index 5132228fcf..fdae61beb1 100644 --- a/src/check_lines.rs +++ b/src/check_lines.rs @@ -184,15 +184,15 @@ pub fn check_lines( let mut valid_codes = vec![]; for code in codes { if !matches.contains(&code) { - invalid_codes.push(code); + invalid_codes.push(code.to_string()); } else { - valid_codes.push(code); + valid_codes.push(code.to_string()); } } if !invalid_codes.is_empty() { let mut check = Check::new( - CheckKind::UnusedNOQA(Some(invalid_codes.join(", "))), + CheckKind::UnusedNOQA(Some(invalid_codes)), Range { location: Location::new(row + 1, start + 1), end_location: Location::new(row + 1, end + 1), diff --git a/src/checks.rs b/src/checks.rs index e19bb3c306..564fc06477 100644 --- a/src/checks.rs +++ b/src/checks.rs @@ -1,6 +1,7 @@ use itertools::Itertools; use rustpython_parser::ast::Location; use serde::{Deserialize, Serialize}; +use std::str::FromStr; use strum_macros::{AsRefStr, EnumIter, EnumString}; use crate::ast::checks::Primitive; @@ -237,7 +238,7 @@ pub enum CheckKind { UsePEP604Annotation, SuperCallWithParameters, // Meta - UnusedNOQA(Option), + UnusedNOQA(Option>), } impl CheckCode { @@ -650,9 +651,21 @@ impl CheckKind { "Use `super()` instead of `super(__class__, self)`".to_string() } // Meta - CheckKind::UnusedNOQA(code) => match code { + CheckKind::UnusedNOQA(codes) => match codes { None => "Unused `noqa` directive".to_string(), - Some(code) => format!("Unused `noqa` directive for: {code}"), + Some(codes) => { + let codes = codes + .iter() + .map(|code| { + if CheckCode::from_str(code).is_ok() { + code.to_string() + } else { + format!("{code} (not implemented)") + } + }) + .join(", "); + format!("Unused `noqa` directive for: {codes}") + } }, } } diff --git a/src/linter.rs b/src/linter.rs index 2b1922b72a..90fc6713f6 100644 --- a/src/linter.rs +++ b/src/linter.rs @@ -762,42 +762,6 @@ mod tests { Ok(()) } - #[test] - fn m001() -> Result<()> { - let mut checks = check_path( - Path::new("./resources/test/fixtures/M001.py"), - &settings::Settings::for_rules(vec![CheckCode::M001, CheckCode::E501, CheckCode::F841]), - &fixer::Mode::Generate, - )?; - checks.sort_by_key(|check| check.location); - insta::assert_yaml_snapshot!(checks); - Ok(()) - } - - #[test] - fn init() -> Result<()> { - let mut checks = check_path( - Path::new("./resources/test/fixtures/__init__.py"), - &settings::Settings::for_rules(vec![CheckCode::F821, CheckCode::F822]), - &fixer::Mode::Generate, - )?; - checks.sort_by_key(|check| check.location); - insta::assert_yaml_snapshot!(checks); - Ok(()) - } - - #[test] - fn future_annotations() -> Result<()> { - let mut checks = check_path( - Path::new("./resources/test/fixtures/future_annotations.py"), - &settings::Settings::for_rules(vec![CheckCode::F401, CheckCode::F821]), - &fixer::Mode::Generate, - )?; - checks.sort_by_key(|check| check.location); - insta::assert_yaml_snapshot!(checks); - Ok(()) - } - #[test] fn e999() -> Result<()> { let mut checks = check_path( @@ -1097,4 +1061,40 @@ mod tests { insta::assert_yaml_snapshot!(checks); Ok(()) } + + #[test] + fn m001() -> Result<()> { + let mut checks = check_path( + Path::new("./resources/test/fixtures/M001.py"), + &settings::Settings::for_rules(vec![CheckCode::M001, CheckCode::E501, CheckCode::F841]), + &fixer::Mode::Generate, + )?; + checks.sort_by_key(|check| check.location); + insta::assert_yaml_snapshot!(checks); + Ok(()) + } + + #[test] + fn init() -> Result<()> { + let mut checks = check_path( + Path::new("./resources/test/fixtures/__init__.py"), + &settings::Settings::for_rules(vec![CheckCode::F821, CheckCode::F822]), + &fixer::Mode::Generate, + )?; + checks.sort_by_key(|check| check.location); + insta::assert_yaml_snapshot!(checks); + Ok(()) + } + + #[test] + fn future_annotations() -> Result<()> { + let mut checks = check_path( + Path::new("./resources/test/fixtures/future_annotations.py"), + &settings::Settings::for_rules(vec![CheckCode::F401, CheckCode::F821]), + &fixer::Mode::Generate, + )?; + checks.sort_by_key(|check| check.location); + insta::assert_yaml_snapshot!(checks); + Ok(()) + } } diff --git a/src/snapshots/ruff__linter__tests__m001.snap b/src/snapshots/ruff__linter__tests__m001.snap index 331c76c81f..7c45ade87b 100644 --- a/src/snapshots/ruff__linter__tests__m001.snap +++ b/src/snapshots/ruff__linter__tests__m001.snap @@ -20,7 +20,8 @@ expression: checks column: 18 applied: false - kind: - UnusedNOQA: E501 + UnusedNOQA: + - E501 location: row: 13 column: 10 @@ -37,7 +38,9 @@ expression: checks column: 24 applied: false - kind: - UnusedNOQA: E501 + UnusedNOQA: + - F841 + - E501 location: row: 16 column: 10 @@ -45,7 +48,7 @@ expression: checks row: 16 column: 30 fix: - content: " # noqa: F841" + content: "" location: row: 16 column: 10 @@ -54,54 +57,74 @@ expression: checks column: 30 applied: false - kind: - UnusedNOQA: F841 + UnusedNOQA: + - W191 location: - row: 41 + row: 19 + column: 10 + end_location: + row: 19 + column: 30 + fix: + content: " # noqa: F841" + location: + row: 19 + column: 10 + end_location: + row: 19 + column: 30 + applied: false +- kind: + UnusedNOQA: + - F841 + location: + row: 44 column: 4 end_location: - row: 41 + row: 44 column: 24 fix: content: " # noqa: E501" location: - row: 41 + row: 44 column: 4 end_location: - row: 41 + row: 44 column: 24 applied: false - kind: - UnusedNOQA: E501 + UnusedNOQA: + - E501 location: - row: 49 + row: 52 column: 4 end_location: - row: 49 + row: 52 column: 18 fix: content: "" location: - row: 49 + row: 52 column: 4 end_location: - row: 49 + row: 52 column: 18 applied: false - kind: UnusedNOQA: ~ location: - row: 57 + row: 60 column: 4 end_location: - row: 57 + row: 60 column: 12 fix: content: "" location: - row: 57 + row: 60 column: 4 end_location: - row: 57 + row: 60 column: 12 applied: false