many-to-one 1/9: Rename Rule::code to Rule::noqa_code

Post this commit series several codes can be mapped to a single rule,
this commit therefore renames Rule::code to Rule::noqa_code,
which is the code that --add-noqa will add to ignore a rule.
This commit is contained in:
Martin Fischer 2023-01-30 06:30:36 +01:00 committed by Charlie Marsh
parent 502ce80c91
commit d451c7a506
36 changed files with 66 additions and 62 deletions

View file

@ -55,13 +55,13 @@ pub fn check_noqa(
}); });
match noqa { match noqa {
(Directive::All(..), matches) => { (Directive::All(..), matches) => {
matches.push(diagnostic.kind.rule().code()); matches.push(diagnostic.kind.rule().noqa_code());
ignored.push(index); ignored.push(index);
continue; continue;
} }
(Directive::Codes(.., codes), matches) => { (Directive::Codes(.., codes), matches) => {
if noqa::includes(diagnostic.kind.rule(), codes) { if noqa::includes(diagnostic.kind.rule(), codes) {
matches.push(diagnostic.kind.rule().code()); matches.push(diagnostic.kind.rule().noqa_code());
ignored.push(index); ignored.push(index);
continue; continue;
} }
@ -82,12 +82,12 @@ pub fn check_noqa(
.or_insert_with(|| (noqa::extract_noqa_directive(lines[noqa_lineno - 1]), vec![])); .or_insert_with(|| (noqa::extract_noqa_directive(lines[noqa_lineno - 1]), vec![]));
match noqa { match noqa {
(Directive::All(..), matches) => { (Directive::All(..), matches) => {
matches.push(diagnostic.kind.rule().code()); matches.push(diagnostic.kind.rule().noqa_code());
ignored.push(index); ignored.push(index);
} }
(Directive::Codes(.., codes), matches) => { (Directive::Codes(.., codes), matches) => {
if noqa::includes(diagnostic.kind.rule(), codes) { if noqa::includes(diagnostic.kind.rule(), codes) {
matches.push(diagnostic.kind.rule().code()); matches.push(diagnostic.kind.rule().noqa_code());
ignored.push(index); ignored.push(index);
} }
} }
@ -128,7 +128,7 @@ pub fn check_noqa(
let mut self_ignore = false; let mut self_ignore = false;
for code in codes { for code in codes {
let code = get_redirect_target(code).unwrap_or(code); let code = get_redirect_target(code).unwrap_or(code);
if code == Rule::UnusedNOQA.code() { if code == Rule::UnusedNOQA.noqa_code() {
self_ignore = true; self_ignore = true;
break; break;
} }

View file

@ -66,7 +66,7 @@ impl Serialize for SerializeRuleAsCode {
where where
S: serde::Serializer, S: serde::Serializer,
{ {
serializer.serialize_str(self.0.code()) serializer.serialize_str(self.0.noqa_code())
} }
} }

View file

@ -72,7 +72,7 @@ pub fn extract_noqa_directive(line: &str) -> Directive {
/// Returns `true` if the string list of `codes` includes `code` (or an alias /// Returns `true` if the string list of `codes` includes `code` (or an alias
/// thereof). /// thereof).
pub fn includes(needle: &Rule, haystack: &[&str]) -> bool { pub fn includes(needle: &Rule, haystack: &[&str]) -> bool {
let needle: &str = needle.code(); let needle: &str = needle.noqa_code();
haystack haystack
.iter() .iter()
.any(|candidate| needle == get_redirect_target(candidate).unwrap_or(candidate)) .any(|candidate| needle == get_redirect_target(candidate).unwrap_or(candidate))
@ -205,7 +205,7 @@ fn add_noqa_inner(
output.push_str(" # noqa: "); output.push_str(" # noqa: ");
// Add codes. // Add codes.
let codes: Vec<&str> = rules.iter().map(|r| r.code()).collect(); let codes: Vec<&str> = rules.iter().map(|r| r.noqa_code()).collect();
let suffix = codes.join(", "); let suffix = codes.join(", ");
output.push_str(&suffix); output.push_str(&suffix);
output.push_str(line_ending); output.push_str(line_ending);
@ -230,7 +230,7 @@ fn add_noqa_inner(
// Add codes. // Add codes.
let codes: Vec<&str> = rules let codes: Vec<&str> = rules
.iter() .iter()
.map(|r| r.code()) .map(|r| r.noqa_code())
.chain(existing.into_iter()) .chain(existing.into_iter())
.sorted_unstable() .sorted_unstable()
.collect(); .collect();

View file

@ -895,7 +895,7 @@ mod tests {
fn check_code_serialization() { fn check_code_serialization() {
for rule in Rule::iter() { for rule in Rule::iter() {
assert!( assert!(
Rule::from_code(rule.code()).is_ok(), Rule::from_code(rule.noqa_code()).is_ok(),
"{rule:?} could not be round-trip serialized." "{rule:?} could not be round-trip serialized."
); );
} }
@ -904,7 +904,7 @@ mod tests {
#[test] #[test]
fn test_linter_parse_code() { fn test_linter_parse_code() {
for rule in Rule::iter() { for rule in Rule::iter() {
let code = rule.code(); let code = rule.noqa_code();
let (linter, rest) = let (linter, rest) =
Linter::parse_code(code).unwrap_or_else(|| panic!("couldn't parse {code:?}")); Linter::parse_code(code).unwrap_or_else(|| panic!("couldn't parse {code:?}"));
assert_eq!(code, format!("{}{rest}", linter.common_prefix())); assert_eq!(code, format!("{}{rest}", linter.common_prefix()));

View file

@ -15,7 +15,7 @@ mod tests {
#[test_case(Rule::CommentedOutCode, Path::new("ERA001.py"); "ERA001")] #[test_case(Rule::CommentedOutCode, Path::new("ERA001.py"); "ERA001")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("eradicate").join(path).as_path(), Path::new("eradicate").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -23,7 +23,7 @@ mod tests {
#[test_case(Rule::SysVersionCmpStr10, Path::new("YTT302.py"); "YTT302")] #[test_case(Rule::SysVersionCmpStr10, Path::new("YTT302.py"); "YTT302")]
#[test_case(Rule::SysVersionSlice1Referenced, Path::new("YTT303.py"); "YTT303")] #[test_case(Rule::SysVersionSlice1Referenced, Path::new("YTT303.py"); "YTT303")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_2020").join(path).as_path(), Path::new("flake8_2020").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -35,7 +35,7 @@ mod tests {
#[test_case(Rule::TryExceptPass, Path::new("S110.py"); "S110")] #[test_case(Rule::TryExceptPass, Path::new("S110.py"); "S110")]
#[test_case(Rule::TryExceptContinue, Path::new("S112.py"); "S112")] #[test_case(Rule::TryExceptContinue, Path::new("S112.py"); "S112")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_bandit").join(path).as_path(), Path::new("flake8_bandit").join(path).as_path(),
&Settings::for_rule(rule_code), &Settings::for_rule(rule_code),

View file

@ -14,7 +14,7 @@ mod tests {
#[test_case(Rule::BlindExcept, Path::new("BLE.py"); "BLE001")] #[test_case(Rule::BlindExcept, Path::new("BLE.py"); "BLE001")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_blind_except").join(path).as_path(), Path::new("flake8_blind_except").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -16,7 +16,7 @@ mod tests {
#[test_case(Rule::BooleanDefaultValueInFunctionDefinition, Path::new("FBT.py"); "FBT002")] #[test_case(Rule::BooleanDefaultValueInFunctionDefinition, Path::new("FBT.py"); "FBT002")]
#[test_case(Rule::BooleanPositionalValueInFunctionCall, Path::new("FBT.py"); "FBT003")] #[test_case(Rule::BooleanPositionalValueInFunctionCall, Path::new("FBT.py"); "FBT003")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_boolean_trap").join(path).as_path(), Path::new("flake8_boolean_trap").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -43,7 +43,7 @@ mod tests {
#[test_case(Rule::RaiseWithoutFromInsideExcept, Path::new("B904.py"); "B904")] #[test_case(Rule::RaiseWithoutFromInsideExcept, Path::new("B904.py"); "B904")]
#[test_case(Rule::ZipWithoutExplicitStrict, Path::new("B905.py"); "B905")] #[test_case(Rule::ZipWithoutExplicitStrict, Path::new("B905.py"); "B905")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_bugbear").join(path).as_path(), Path::new("flake8_bugbear").join(path).as_path(),
&Settings::for_rule(rule_code), &Settings::for_rule(rule_code),

View file

@ -19,7 +19,7 @@ mod tests {
#[test_case(Rule::BuiltinArgumentShadowing, Path::new("A002.py"); "A002")] #[test_case(Rule::BuiltinArgumentShadowing, Path::new("A002.py"); "A002")]
#[test_case(Rule::BuiltinAttributeShadowing, Path::new("A003.py"); "A003")] #[test_case(Rule::BuiltinAttributeShadowing, Path::new("A003.py"); "A003")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_builtins").join(path).as_path(), Path::new("flake8_builtins").join(path).as_path(),
&Settings::for_rule(rule_code), &Settings::for_rule(rule_code),
@ -34,7 +34,7 @@ mod tests {
fn builtins_ignorelist(rule_code: Rule, path: &Path) -> Result<()> { fn builtins_ignorelist(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!( let snapshot = format!(
"{}_{}_builtins_ignorelist", "{}_{}_builtins_ignorelist",
rule_code.code(), rule_code.noqa_code(),
path.to_string_lossy() path.to_string_lossy()
); );

View file

@ -31,7 +31,7 @@ mod tests {
#[test_case(Rule::UnnecessaryMap, Path::new("C417.py"); "C417")] #[test_case(Rule::UnnecessaryMap, Path::new("C417.py"); "C417")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_comprehensions").join(path).as_path(), Path::new("flake8_comprehensions").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -22,7 +22,7 @@ mod tests {
#[test_case(Rule::CallDateToday, Path::new("DTZ011.py"); "DTZ011")] #[test_case(Rule::CallDateToday, Path::new("DTZ011.py"); "DTZ011")]
#[test_case(Rule::CallDateFromtimestamp, Path::new("DTZ012.py"); "DTZ012")] #[test_case(Rule::CallDateFromtimestamp, Path::new("DTZ012.py"); "DTZ012")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_datetimez").join(path).as_path(), Path::new("flake8_datetimez").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -15,7 +15,7 @@ mod tests {
#[test_case(Rule::Debugger, Path::new("T100.py"); "T100")] #[test_case(Rule::Debugger, Path::new("T100.py"); "T100")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_debugger").join(path).as_path(), Path::new("flake8_debugger").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -16,7 +16,7 @@ mod tests {
#[test_case(Rule::ModelWithoutDunderStr, Path::new("DJ008.py"); "DJ008")] #[test_case(Rule::ModelWithoutDunderStr, Path::new("DJ008.py"); "DJ008")]
#[test_case(Rule::NonLeadingReceiverDecorator, Path::new("DJ013.py"); "DJ013")] #[test_case(Rule::NonLeadingReceiverDecorator, Path::new("DJ013.py"); "DJ013")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_django").join(path).as_path(), Path::new("flake8_django").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -17,7 +17,7 @@ mod tests {
#[test_case(Rule::MultiLineImplicitStringConcatenation, Path::new("ISC.py"); "ISC002")] #[test_case(Rule::MultiLineImplicitStringConcatenation, Path::new("ISC.py"); "ISC002")]
#[test_case(Rule::ExplicitStringConcatenation, Path::new("ISC.py"); "ISC003")] #[test_case(Rule::ExplicitStringConcatenation, Path::new("ISC.py"); "ISC003")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_implicit_str_concat").join(path).as_path(), Path::new("flake8_implicit_str_concat").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),
@ -30,7 +30,11 @@ mod tests {
#[test_case(Rule::MultiLineImplicitStringConcatenation, Path::new("ISC.py"); "ISC002")] #[test_case(Rule::MultiLineImplicitStringConcatenation, Path::new("ISC.py"); "ISC002")]
#[test_case(Rule::ExplicitStringConcatenation, Path::new("ISC.py"); "ISC003")] #[test_case(Rule::ExplicitStringConcatenation, Path::new("ISC.py"); "ISC003")]
fn multiline(rule_code: Rule, path: &Path) -> Result<()> { fn multiline(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("multiline_{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!(
"multiline_{}_{}",
rule_code.noqa_code(),
path.to_string_lossy()
);
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_implicit_str_concat").join(path).as_path(), Path::new("flake8_implicit_str_concat").join(path).as_path(),
&settings::Settings { &settings::Settings {

View file

@ -20,7 +20,7 @@ mod tests {
#[test_case(Rule::PreferListBuiltin, Path::new("PIE807.py"); "PIE807")] #[test_case(Rule::PreferListBuiltin, Path::new("PIE807.py"); "PIE807")]
#[test_case(Rule::PreferUniqueEnums, Path::new("PIE796.py"); "PIE796")] #[test_case(Rule::PreferUniqueEnums, Path::new("PIE796.py"); "PIE796")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_pie").join(path).as_path(), Path::new("flake8_pie").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -15,7 +15,7 @@ mod tests {
#[test_case(Rule::PrintFound, Path::new("T201.py"); "T201")] #[test_case(Rule::PrintFound, Path::new("T201.py"); "T201")]
#[test_case(Rule::PPrintFound, Path::new("T203.py"); "T203")] #[test_case(Rule::PPrintFound, Path::new("T203.py"); "T203")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_print").join(path).as_path(), Path::new("flake8_print").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -19,7 +19,7 @@ mod tests {
#[test_case(Rule::UnrecognizedPlatformName, Path::new("PYI008.pyi"))] #[test_case(Rule::UnrecognizedPlatformName, Path::new("PYI008.pyi"))]
#[test_case(Rule::UnrecognizedPlatformName, Path::new("PYI008.py"))] #[test_case(Rule::UnrecognizedPlatformName, Path::new("PYI008.py"))]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_pyi").join(path).as_path(), Path::new("flake8_pyi").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -25,7 +25,7 @@ mod tests {
#[test_case(Rule::SuperfluousElseContinue, Path::new("RET507.py"); "RET507")] #[test_case(Rule::SuperfluousElseContinue, Path::new("RET507.py"); "RET507")]
#[test_case(Rule::SuperfluousElseBreak, Path::new("RET508.py"); "RET508")] #[test_case(Rule::SuperfluousElseBreak, Path::new("RET508.py"); "RET508")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_return").join(path).as_path(), Path::new("flake8_return").join(path).as_path(),
&Settings::for_rule(rule_code), &Settings::for_rule(rule_code),

View file

@ -39,7 +39,7 @@ mod tests {
#[test_case(Rule::DictGetWithDefault, Path::new("SIM401.py"); "SIM401")] #[test_case(Rule::DictGetWithDefault, Path::new("SIM401.py"); "SIM401")]
#[test_case(Rule::IfWithSameArms, Path::new("SIM114.py"); "SIM114")] #[test_case(Rule::IfWithSameArms, Path::new("SIM114.py"); "SIM114")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_simplify").join(path).as_path(), Path::new("flake8_simplify").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -21,7 +21,7 @@ mod tests {
#[test_case(Rule::UnusedStaticMethodArgument, Path::new("ARG.py"); "ARG004")] #[test_case(Rule::UnusedStaticMethodArgument, Path::new("ARG.py"); "ARG004")]
#[test_case(Rule::UnusedLambdaArgument, Path::new("ARG.py"); "ARG005")] #[test_case(Rule::UnusedLambdaArgument, Path::new("ARG.py"); "ARG005")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_unused_arguments").join(path).as_path(), Path::new("flake8_unused_arguments").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -56,7 +56,7 @@ mod tests {
#[test_case(Rule::PathlibPyPath, Path::new("py_path_1.py"); "PTH024_1")] #[test_case(Rule::PathlibPyPath, Path::new("py_path_1.py"); "PTH024_1")]
#[test_case(Rule::PathlibPyPath, Path::new("py_path_2.py"); "PTH024_2")] #[test_case(Rule::PathlibPyPath, Path::new("py_path_2.py"); "PTH024_2")]
fn rules_pypath(rule_code: Rule, path: &Path) -> Result<()> { fn rules_pypath(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_use_pathlib").join(path).as_path(), Path::new("flake8_use_pathlib").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -251,7 +251,7 @@ mod tests {
#[test_case(Rule::UseOfInplaceArgument, Path::new("PD002.py"); "PD002")] #[test_case(Rule::UseOfInplaceArgument, Path::new("PD002.py"); "PD002")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("pandas_vet").join(path).as_path(), Path::new("pandas_vet").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -31,7 +31,7 @@ mod tests {
#[test_case(Rule::CamelcaseImportedAsAcronym, Path::new("N817.py"); "N817")] #[test_case(Rule::CamelcaseImportedAsAcronym, Path::new("N817.py"); "N817")]
#[test_case(Rule::ErrorSuffixOnExceptionName, Path::new("N818.py"); "N818")] #[test_case(Rule::ErrorSuffixOnExceptionName, Path::new("N818.py"); "N818")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("pep8_naming").join(path).as_path(), Path::new("pep8_naming").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -44,7 +44,7 @@ mod tests {
#[test_case(Rule::TypeComparison, Path::new("E721.py"))] #[test_case(Rule::TypeComparison, Path::new("E721.py"))]
#[test_case(Rule::UselessSemicolon, Path::new("E70.py"))] #[test_case(Rule::UselessSemicolon, Path::new("E70.py"))]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("pycodestyle").join(path).as_path(), Path::new("pycodestyle").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),
@ -77,7 +77,7 @@ mod tests {
#[test_case(Rule::WhitespaceBeforeCloseBracket, Path::new("E20.py"))] #[test_case(Rule::WhitespaceBeforeCloseBracket, Path::new("E20.py"))]
#[test_case(Rule::WhitespaceBeforePunctuation, Path::new("E20.py"))] #[test_case(Rule::WhitespaceBeforePunctuation, Path::new("E20.py"))]
fn logical(rule_code: Rule, path: &Path) -> Result<()> { fn logical(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("pycodestyle").join(path).as_path(), Path::new("pycodestyle").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -68,7 +68,7 @@ mod tests {
#[test_case(Rule::EscapeSequenceInDocstring, Path::new("D.py"); "D301")] #[test_case(Rule::EscapeSequenceInDocstring, Path::new("D.py"); "D301")]
#[test_case(Rule::TripleSingleQuotes, Path::new("D.py"); "D300")] #[test_case(Rule::TripleSingleQuotes, Path::new("D.py"); "D300")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("pydocstyle").join(path).as_path(), Path::new("pydocstyle").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -111,7 +111,7 @@ mod tests {
#[test_case(Rule::UnusedAnnotation, Path::new("F842.py"); "F842")] #[test_case(Rule::UnusedAnnotation, Path::new("F842.py"); "F842")]
#[test_case(Rule::RaiseNotImplemented, Path::new("F901.py"); "F901")] #[test_case(Rule::RaiseNotImplemented, Path::new("F901.py"); "F901")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("pyflakes").join(path).as_path(), Path::new("pyflakes").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -19,7 +19,7 @@ mod tests {
#[test_case(Rule::BlanketTypeIgnore, Path::new("PGH003_0.py"); "PGH003_0")] #[test_case(Rule::BlanketTypeIgnore, Path::new("PGH003_0.py"); "PGH003_0")]
#[test_case(Rule::BlanketNOQA, Path::new("PGH004_0.py"); "PGH004_0")] #[test_case(Rule::BlanketNOQA, Path::new("PGH004_0.py"); "PGH004_0")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("pygrep-hooks").join(path).as_path(), Path::new("pygrep-hooks").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -46,7 +46,7 @@ mod tests {
#[test_case(Rule::BadStrStripCall, Path::new("bad_str_strip_call.py"); "PLE01310")] #[test_case(Rule::BadStrStripCall, Path::new("bad_str_strip_call.py"); "PLE01310")]
#[test_case(Rule::YieldInInit, Path::new("yield_in_init.py"); "PLE0100")] #[test_case(Rule::YieldInInit, Path::new("yield_in_init.py"); "PLE0100")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("pylint").join(path).as_path(), Path::new("pylint").join(path).as_path(),
&Settings::for_rules(vec![rule_code]), &Settings::for_rules(vec![rule_code]),

View file

@ -19,7 +19,7 @@ mod tests {
#[test_case(Rule::KeywordArgumentBeforeStarArgument, Path::new("RUF004.py"); "RUF004")] #[test_case(Rule::KeywordArgumentBeforeStarArgument, Path::new("RUF004.py"); "RUF004")]
#[test_case(Rule::UnpackInsteadOfConcatenatingToCollectionLiteral, Path::new("RUF005.py"); "RUF005")] #[test_case(Rule::UnpackInsteadOfConcatenatingToCollectionLiteral, Path::new("RUF005.py"); "RUF005")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> { fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path( let diagnostics = test_path(
Path::new("ruff").join(path).as_path(), Path::new("ruff").join(path).as_path(),
&settings::Settings::for_rule(rule_code), &settings::Settings::for_rule(rule_code),

View file

@ -22,17 +22,17 @@ struct Explanation<'a> {
/// Explain a `Rule` to the user. /// Explain a `Rule` to the user.
pub fn rule(rule: &Rule, format: HelpFormat) -> Result<()> { pub fn rule(rule: &Rule, format: HelpFormat) -> Result<()> {
let (linter, _) = Linter::parse_code(rule.code()).unwrap(); let (linter, _) = Linter::parse_code(rule.noqa_code()).unwrap();
let mut stdout = BufWriter::new(io::stdout().lock()); let mut stdout = BufWriter::new(io::stdout().lock());
let mut output = String::new(); let mut output = String::new();
match format { match format {
HelpFormat::Text | HelpFormat::Pretty => { HelpFormat::Text | HelpFormat::Pretty => {
output.push_str(&format!("# {} ({})", rule.as_ref(), rule.code())); output.push_str(&format!("# {} ({})", rule.as_ref(), rule.noqa_code()));
output.push('\n'); output.push('\n');
output.push('\n'); output.push('\n');
let (linter, _) = Linter::parse_code(rule.code()).unwrap(); let (linter, _) = Linter::parse_code(rule.noqa_code()).unwrap();
output.push_str(&format!("Derived from the **{}** linter.", linter.name())); output.push_str(&format!("Derived from the **{}** linter.", linter.name()));
output.push('\n'); output.push('\n');
output.push('\n'); output.push('\n');
@ -58,7 +58,7 @@ pub fn rule(rule: &Rule, format: HelpFormat) -> Result<()> {
} }
HelpFormat::Json => { HelpFormat::Json => {
output.push_str(&serde_json::to_string_pretty(&Explanation { output.push_str(&serde_json::to_string_pretty(&Explanation {
code: rule.code(), code: rule.noqa_code(),
linter: linter.name(), linter: linter.name(),
summary: rule.message_formats()[0], summary: rule.message_formats()[0],
})?); })?);

View file

@ -67,7 +67,7 @@ impl Serialize for SerializeRuleAsCode<'_> {
where where
S: serde::Serializer, S: serde::Serializer,
{ {
serializer.serialize_str(self.0.code()) serializer.serialize_str(self.0.noqa_code())
} }
} }
@ -221,7 +221,7 @@ impl Printer {
message.kind.body() message.kind.body()
)); ));
let mut case = TestCase::new( let mut case = TestCase::new(
format!("org.ruff.{}", message.kind.rule().code()), format!("org.ruff.{}", message.kind.rule().noqa_code()),
status, status,
); );
let file_path = Path::new(filename); let file_path = Path::new(filename);
@ -313,14 +313,14 @@ impl Printer {
":", ":",
message.location.column(), message.location.column(),
":", ":",
message.kind.rule().code(), message.kind.rule().noqa_code(),
message.kind.body(), message.kind.body(),
); );
writeln!( writeln!(
stdout, stdout,
"::error title=Ruff \ "::error title=Ruff \
({}),file={},line={},col={},endLine={},endColumn={}::{}", ({}),file={},line={},col={},endLine={},endColumn={}::{}",
message.kind.rule().code(), message.kind.rule().noqa_code(),
message.filename, message.filename,
message.location.row(), message.location.row(),
message.location.column(), message.location.column(),
@ -341,9 +341,9 @@ impl Printer {
.iter() .iter()
.map(|message| { .map(|message| {
json!({ json!({
"description": format!("({}) {}", message.kind.rule().code(), message.kind.body()), "description": format!("({}) {}", message.kind.rule().noqa_code(), message.kind.body()),
"severity": "major", "severity": "major",
"fingerprint": message.kind.rule().code(), "fingerprint": message.kind.rule().noqa_code(),
"location": { "location": {
"path": message.filename, "path": message.filename,
"lines": { "lines": {
@ -366,7 +366,7 @@ impl Printer {
"{}:{}: [{}] {}", "{}:{}: [{}] {}",
relativize_path(Path::new(&message.filename)), relativize_path(Path::new(&message.filename)),
message.location.row(), message.location.row(),
message.kind.rule().code(), message.kind.rule().noqa_code(),
message.kind.body(), message.kind.body(),
); );
writeln!(stdout, "{label}")?; writeln!(stdout, "{label}")?;
@ -394,7 +394,7 @@ impl Printer {
let statistics = violations let statistics = violations
.iter() .iter()
.map(|rule| ExpandedStatistics { .map(|rule| ExpandedStatistics {
code: rule.code(), code: rule.noqa_code(),
count: diagnostics count: diagnostics
.messages .messages
.iter() .iter()
@ -538,7 +538,7 @@ impl Display for CodeAndBody<'_> {
write!( write!(
f, f,
"{code} {autofix}{body}", "{code} {autofix}{body}",
code = self.0.kind.rule().code().red().bold(), code = self.0.kind.rule().noqa_code().red().bold(),
autofix = format_args!("[{}] ", "*".cyan()), autofix = format_args!("[{}] ", "*".cyan()),
body = self.0.kind.body(), body = self.0.kind.body(),
) )
@ -546,7 +546,7 @@ impl Display for CodeAndBody<'_> {
write!( write!(
f, f,
"{code} {body}", "{code} {body}",
code = self.0.kind.rule().code().red().bold(), code = self.0.kind.rule().noqa_code().red().bold(),
body = self.0.kind.body(), body = self.0.kind.body(),
) )
} }
@ -601,7 +601,7 @@ fn print_message<T: Write>(
source: &source.contents, source: &source.contents,
line_start: message.location.row(), line_start: message.location.row(),
annotations: vec![SourceAnnotation { annotations: vec![SourceAnnotation {
label: message.kind.rule().code(), label: message.kind.rule().noqa_code(),
annotation_type: AnnotationType::Error, annotation_type: AnnotationType::Error,
range: source.range, range: source.range,
}], }],
@ -656,7 +656,7 @@ fn print_fixed<T: Write>(stdout: &mut T, fixed: &FxHashMap<String, FixTable>) ->
writeln!( writeln!(
stdout, stdout,
" {count:>num_digits$} × {} ({})", " {count:>num_digits$} × {} ({})",
rule.code().red().bold(), rule.noqa_code().red().bold(),
rule.as_ref(), rule.as_ref(),
)?; )?;
} }
@ -706,7 +706,7 @@ fn print_grouped_message<T: Write>(
source: &source.contents, source: &source.contents,
line_start: message.location.row(), line_start: message.location.row(),
annotations: vec![SourceAnnotation { annotations: vec![SourceAnnotation {
label: message.kind.rule().code(), label: message.kind.rule().noqa_code(),
annotation_type: AnnotationType::Error, annotation_type: AnnotationType::Error,
range: source.range, range: source.range,
}], }],

View file

@ -21,11 +21,11 @@ pub fn main(args: &Args) -> Result<()> {
for rule in Rule::iter() { for rule in Rule::iter() {
if let Some(explanation) = rule.explanation() { if let Some(explanation) = rule.explanation() {
let mut output = String::new(); let mut output = String::new();
output.push_str(&format!("# {} ({})", rule.as_ref(), rule.code())); output.push_str(&format!("# {} ({})", rule.as_ref(), rule.noqa_code()));
output.push('\n'); output.push('\n');
output.push('\n'); output.push('\n');
let (linter, _) = Linter::parse_code(rule.code()).unwrap(); let (linter, _) = Linter::parse_code(rule.noqa_code()).unwrap();
output.push_str(&format!("Derived from the **{}** linter.", linter.name())); output.push_str(&format!("Derived from the **{}** linter.", linter.name()));
output.push('\n'); output.push('\n');
output.push('\n'); output.push('\n');

View file

@ -39,7 +39,7 @@ fn generate_table(table_out: &mut String, rules: impl IntoIterator<Item = Rule>)
#[allow(clippy::or_fun_call)] #[allow(clippy::or_fun_call)]
table_out.push_str(&format!( table_out.push_str(&format!(
"| {} | {} | {} | {} |", "| {} | {} | {} | {} |",
rule.code(), rule.noqa_code(),
rule.explanation() rule.explanation()
.is_some() .is_some()
.then_some(format_args!("[{rule_name}]({URL_PREFIX}/{rule_name}/)",)) .then_some(format_args!("[{rule_name}]({URL_PREFIX}/{rule_name}/)",))

View file

@ -106,7 +106,7 @@ pub fn define_rule_mapping(mapping: &Mapping) -> proc_macro2::TokenStream {
match self { #rule_autofixable_match_arms } match self { #rule_autofixable_match_arms }
} }
pub fn code(&self) -> &'static str { pub fn noqa_code(&self) -> &'static str {
match self { #rule_code_match_arms } match self { #rule_code_match_arms }
} }