ruff/crates/ruff_linter/src
Brent Westbrook 9925910a29
Add a ViolationMetadata::rule method (#18234)
Summary
--

This PR adds a macro-generated method to retrieve the `Rule` associated
with a given `Violation` struct, which makes it substantially cheaper
than parsing from the rule name. The rule is then converted to a
`NoqaCode` for storage on the `Message` (and eventually on the new
diagnostic type). The `ViolationMetadata::rule_name` method was now
unused, so the `rule` method replaces it.

Several types had to be moved from the `ruff_diagnostics` crate to the
`ruff_linter` crate to make this work, namely the `Violation` traits and
the old `Diagnostic` type, which had a constructor generic over a
`Violation`.

It's actually a fairly small PR, minus the hundreds of import changes.
The main changes are in these files:

-
[crates/ruff_linter/src/message/mod.rs](https://github.com/astral-sh/ruff/pull/18234/files#diff-139754ea310d75f28307008d21c771a190038bd106efe3b9267cc2d6c0fa0921)
-
[crates/ruff_diagnostics/src/lib.rs](https://github.com/astral-sh/ruff/pull/18234/files#diff-8e8ea5c586935bf21ea439f24253fcfd5955d2cb130f5377c2fa7bfee3ea3a81)
-
[crates/ruff_linter/src/diagnostic.rs](https://github.com/astral-sh/ruff/pull/18234/files#diff-1d0c9aad90d8f9446079c5be5f284150d97797158715bd9729e6f1f70246297a)
-
[crates/ruff_linter/src/lib.rs](https://github.com/astral-sh/ruff/pull/18234/files#diff-eb93ef7e78a612f5fa9145412c75cf6b1a5cefba1c2233e4a11a880a1ce1fbcc)

Test Plan
--

Existing tests
2025-05-28 09:27:09 -04:00
..
checkers Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
comments Update insta snapshots (#14366) 2024-11-15 19:31:15 +01:00
cst Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
docstrings Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
fix Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
importer Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
message Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
registry Use #[expect(lint)] over #[allow(lint)] where possible (#17822) 2025-05-03 21:20:31 +02:00
rules Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
settings Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
snapshots [semantic-syntax-tests] IrrefutableCasePattern, SingleStarredAssignment, WriteToDebug, InvalidExpression (#17748) 2025-05-09 14:54:05 -04:00
codes.rs Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
diagnostic.rs Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
directives.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
doc_lines.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
fs.rs Escape template filenames in glob patterns (#16407) 2025-03-03 09:29:58 -05:00
lib.rs Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
line_width.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
linter.rs Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
locator.rs Split SourceLocation into LineColumn and SourceLocation (#17587) 2025-04-27 11:27:33 +01:00
logging.rs Use #[expect(lint)] over #[allow(lint)] where possible (#17822) 2025-05-03 21:20:31 +02:00
noqa.rs Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
package.rs Detect empty implicit namespace packages (#14236) 2024-11-09 22:03:34 -05:00
packaging.rs Nested namespace packages support (#10541) 2024-03-24 22:53:32 -04:00
preview.rs [flake8-simplify] enable fix in preview mode (SIM117) (#18208) 2025-05-20 08:34:50 -05:00
pyproject_toml.rs Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
registry.rs Sort linters alphabetically (#16168) 2025-02-14 22:05:08 +01:00
renamer.rs Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
rule_redirects.rs [flake8-bandit] Move unsafe-markup-use from RUF035 to S704 (#15957) 2025-03-13 15:37:37 +01:00
rule_selector.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
source_kind.rs Update MSRV to 1.85 and toolchain to 1.87 (#18126) 2025-05-16 09:19:55 +02:00
test.rs Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
text_helpers.rs Handle non-printable characters in diff view (#11687) 2024-06-08 06:22:03 +00:00
upstream_categories.rs Fix pylint upstream categories not showing in docs (#10441) 2024-03-18 01:27:39 +00:00
violation.rs Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00