From 805503c19a6b74c5803e10123077997e29a0da37 Mon Sep 17 00:00:00 2001 From: Amethyst Reese Date: Wed, 7 Jan 2026 17:23:18 -0800 Subject: [PATCH] [`ruff`] Improve fix title for `RUF102` invalid rule code (#22100) ## Summary Updates the fix title for RUF102 to either specify which rule code to remove, or clarify that the entire suppression comment should be removed. ## Test Plan Updated test snapshots. --- .../src/rules/ruff/rules/invalid_rule_code.rs | 9 +++++++- ..._rules__ruff__tests__RUF102_RUF102.py.snap | 22 +++++++++---------- ...sts__invalid_rule_code_external_rules.snap | 18 +++++++-------- ...ules__ruff__tests__range_suppressions.snap | 4 ++-- crates/ruff_linter/src/suppression.rs | 7 ++++++ 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/crates/ruff_linter/src/rules/ruff/rules/invalid_rule_code.rs b/crates/ruff_linter/src/rules/ruff/rules/invalid_rule_code.rs index f8b24d6d86..46d392f1e3 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/invalid_rule_code.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/invalid_rule_code.rs @@ -52,6 +52,7 @@ impl InvalidRuleCodeKind { pub(crate) struct InvalidRuleCode { pub(crate) rule_code: String, pub(crate) kind: InvalidRuleCodeKind, + pub(crate) whole_comment: bool, } impl AlwaysFixableViolation for InvalidRuleCode { @@ -65,7 +66,11 @@ impl AlwaysFixableViolation for InvalidRuleCode { } fn fix_title(&self) -> String { - "Remove the rule code".to_string() + if self.whole_comment { + format!("Remove the {} comment", self.kind.as_str()) + } else { + format!("Remove the rule code `{}`", self.rule_code) + } } } @@ -122,6 +127,7 @@ fn all_codes_invalid_diagnostic( .collect::>() .join(", "), kind: InvalidRuleCodeKind::Noqa, + whole_comment: true, }, directive.range(), ) @@ -139,6 +145,7 @@ fn some_codes_are_invalid_diagnostic( InvalidRuleCode { rule_code: invalid_code.to_string(), kind: InvalidRuleCodeKind::Noqa, + whole_comment: false, }, invalid_code.range(), ) diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF102_RUF102.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF102_RUF102.py.snap index 9c2cb5a1f7..6ddd82315d 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF102_RUF102.py.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF102_RUF102.py.snap @@ -10,7 +10,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID123 3 | # External code 4 | import re # noqa: V123 | -help: Remove the rule code +help: Remove the `# noqa` comment 1 | # Invalid code - import os # noqa: INVALID123 2 + import os @@ -28,7 +28,7 @@ RUF102 [*] Invalid rule code in `# noqa`: V123 5 | # Valid noqa 6 | import sys # noqa: E402 | -help: Remove the rule code +help: Remove the `# noqa` comment 1 | # Invalid code 2 | import os # noqa: INVALID123 3 | # External code @@ -48,7 +48,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID456 8 | from itertools import product # Preceeding comment # noqa: INVALID789 9 | # Succeeding comment | -help: Remove the rule code +help: Remove the rule code `INVALID456` 4 | import re # noqa: V123 5 | # Valid noqa 6 | import sys # noqa: E402 @@ -68,7 +68,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID789 9 | # Succeeding comment 10 | import math # noqa: INVALID000 # Succeeding comment | -help: Remove the rule code +help: Remove the `# noqa` comment 5 | # Valid noqa 6 | import sys # noqa: E402 7 | from functools import cache # Preceeding comment # noqa: F401, INVALID456 @@ -88,7 +88,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID000 11 | # Mixed valid and invalid 12 | from typing import List # noqa: F401, INVALID123 | -help: Remove the rule code +help: Remove the `# noqa` comment 7 | from functools import cache # Preceeding comment # noqa: F401, INVALID456 8 | from itertools import product # Preceeding comment # noqa: INVALID789 9 | # Succeeding comment @@ -108,7 +108,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID123 13 | # Test for multiple invalid 14 | from collections import defaultdict # noqa: INVALID100, INVALID200, F401 | -help: Remove the rule code +help: Remove the rule code `INVALID123` 9 | # Succeeding comment 10 | import math # noqa: INVALID000 # Succeeding comment 11 | # Mixed valid and invalid @@ -128,7 +128,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID100 15 | # Test for preserving valid codes when fixing 16 | from itertools import chain # noqa: E402, INVALID300, F401 | -help: Remove the rule code +help: Remove the rule code `INVALID100` 11 | # Mixed valid and invalid 12 | from typing import List # noqa: F401, INVALID123 13 | # Test for multiple invalid @@ -148,7 +148,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID200 15 | # Test for preserving valid codes when fixing 16 | from itertools import chain # noqa: E402, INVALID300, F401 | -help: Remove the rule code +help: Remove the rule code `INVALID200` 11 | # Mixed valid and invalid 12 | from typing import List # noqa: F401, INVALID123 13 | # Test for multiple invalid @@ -168,7 +168,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID300 17 | # Test for mixed code types 18 | import json # noqa: E402, INVALID400, V100 | -help: Remove the rule code +help: Remove the rule code `INVALID300` 13 | # Test for multiple invalid 14 | from collections import defaultdict # noqa: INVALID100, INVALID200, F401 15 | # Test for preserving valid codes when fixing @@ -188,7 +188,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID400 19 | # Test for rule redirects 20 | import pandas as pd # noqa: TCH002 | -help: Remove the rule code +help: Remove the rule code `INVALID400` 15 | # Test for preserving valid codes when fixing 16 | from itertools import chain # noqa: E402, INVALID300, F401 17 | # Test for mixed code types @@ -207,7 +207,7 @@ RUF102 [*] Invalid rule code in `# noqa`: V100 19 | # Test for rule redirects 20 | import pandas as pd # noqa: TCH002 | -help: Remove the rule code +help: Remove the rule code `V100` 15 | # Test for preserving valid codes when fixing 16 | from itertools import chain # noqa: E402, INVALID300, F401 17 | # Test for mixed code types diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__invalid_rule_code_external_rules.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__invalid_rule_code_external_rules.snap index e9b7890bf3..381464a306 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__invalid_rule_code_external_rules.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__invalid_rule_code_external_rules.snap @@ -10,7 +10,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID123 3 | # External code 4 | import re # noqa: V123 | -help: Remove the rule code +help: Remove the `# noqa` comment 1 | # Invalid code - import os # noqa: INVALID123 2 + import os @@ -28,7 +28,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID456 8 | from itertools import product # Preceeding comment # noqa: INVALID789 9 | # Succeeding comment | -help: Remove the rule code +help: Remove the rule code `INVALID456` 4 | import re # noqa: V123 5 | # Valid noqa 6 | import sys # noqa: E402 @@ -48,7 +48,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID789 9 | # Succeeding comment 10 | import math # noqa: INVALID000 # Succeeding comment | -help: Remove the rule code +help: Remove the `# noqa` comment 5 | # Valid noqa 6 | import sys # noqa: E402 7 | from functools import cache # Preceeding comment # noqa: F401, INVALID456 @@ -68,7 +68,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID000 11 | # Mixed valid and invalid 12 | from typing import List # noqa: F401, INVALID123 | -help: Remove the rule code +help: Remove the `# noqa` comment 7 | from functools import cache # Preceeding comment # noqa: F401, INVALID456 8 | from itertools import product # Preceeding comment # noqa: INVALID789 9 | # Succeeding comment @@ -88,7 +88,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID123 13 | # Test for multiple invalid 14 | from collections import defaultdict # noqa: INVALID100, INVALID200, F401 | -help: Remove the rule code +help: Remove the rule code `INVALID123` 9 | # Succeeding comment 10 | import math # noqa: INVALID000 # Succeeding comment 11 | # Mixed valid and invalid @@ -108,7 +108,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID100 15 | # Test for preserving valid codes when fixing 16 | from itertools import chain # noqa: E402, INVALID300, F401 | -help: Remove the rule code +help: Remove the rule code `INVALID100` 11 | # Mixed valid and invalid 12 | from typing import List # noqa: F401, INVALID123 13 | # Test for multiple invalid @@ -128,7 +128,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID200 15 | # Test for preserving valid codes when fixing 16 | from itertools import chain # noqa: E402, INVALID300, F401 | -help: Remove the rule code +help: Remove the rule code `INVALID200` 11 | # Mixed valid and invalid 12 | from typing import List # noqa: F401, INVALID123 13 | # Test for multiple invalid @@ -148,7 +148,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID300 17 | # Test for mixed code types 18 | import json # noqa: E402, INVALID400, V100 | -help: Remove the rule code +help: Remove the rule code `INVALID300` 13 | # Test for multiple invalid 14 | from collections import defaultdict # noqa: INVALID100, INVALID200, F401 15 | # Test for preserving valid codes when fixing @@ -168,7 +168,7 @@ RUF102 [*] Invalid rule code in `# noqa`: INVALID400 19 | # Test for rule redirects 20 | import pandas as pd # noqa: TCH002 | -help: Remove the rule code +help: Remove the rule code `INVALID400` 15 | # Test for preserving valid codes when fixing 16 | from itertools import chain # noqa: E402, INVALID300, F401 17 | # Test for mixed code types diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__range_suppressions.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__range_suppressions.snap index 264df075d2..9aab0ae36e 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__range_suppressions.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__range_suppressions.snap @@ -552,7 +552,7 @@ RUF102 [*] Invalid rule code in suppression: YF829 97 | # ruff: enable[YF829] | ----- | -help: Remove the rule code +help: Remove the suppression comment 90 | 91 | def f(): 92 | # Unknown rule codes @@ -578,7 +578,7 @@ RUF102 [*] Invalid rule code in suppression: RQW320 | ------ 97 | # ruff: enable[YF829] | -help: Remove the rule code +help: Remove the rule code `RQW320` 91 | def f(): 92 | # Unknown rule codes 93 | # ruff: disable[YF829] diff --git a/crates/ruff_linter/src/suppression.rs b/crates/ruff_linter/src/suppression.rs index 856e65699b..aa888c36d7 100644 --- a/crates/ruff_linter/src/suppression.rs +++ b/crates/ruff_linter/src/suppression.rs @@ -91,6 +91,12 @@ pub(crate) struct Suppression { comments: DisableEnableComments, } +impl Suppression { + fn codes(&self) -> &[TextRange] { + &self.comments.disable_comment().codes + } +} + #[derive(Debug)] pub(crate) enum DisableEnableComments { /// An implicitly closed disable comment without a matching enable comment. @@ -201,6 +207,7 @@ impl Suppressions { InvalidRuleCode { rule_code: suppression.code.to_string(), kind: InvalidRuleCodeKind::Suppression, + whole_comment: suppression.codes().len() == 1, }, ); }