ruff/crates/ruff_linter/src
Brent Westbrook ce216c79cc
Remove Message::to_rule (#18447)
## Summary

As the title says, this PR removes the `Message::to_rule` method by
replacing related uses of `Rule` with `NoqaCode` (or the rule's name in
the case of the cache). Where it seemed a `Rule` was really needed, we
convert back to the `Rule` by parsing either the rule name (with
`str::parse`) or the `NoqaCode` (with `Rule::from_code`).

I thought this was kind of like cheating and that it might not resolve
this part of Micha's
[comment](https://github.com/astral-sh/ruff/pull/18391#issuecomment-2933764275):

> because we can't add Rule to Diagnostic or **have it anywhere in our
shared rendering logic**

but after looking again, the only remaining `Rule` conversion in
rendering code is for the SARIF output format. The other two non-test
`Rule` conversions are for caching and writing a fix summary, which I
don't think fall into the shared rendering logic. That leaves the SARIF
format as the only real problem, but maybe we can delay that for now.

The motivation here is that we won't be able to store a `Rule` on the
new `Diagnostic` type, but we should be able to store a `NoqaCode`,
likely as a string.

## Test Plan

Existing tests

##
[Benchmarks](https://codspeed.io/astral-sh/ruff/branches/brent%2Fremove-to-rule)

Almost no perf regression, only -1% on
`linter/default-rules[large/dataset.py]`.

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
2025-06-05 12:48:29 -04:00
..
checkers Implement template strings (#17851) 2025-05-30 15:00:56 -05:00
comments
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 Remove Message::to_rule (#18447) 2025-06-05 12:48:29 -04:00
importer Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00
message Remove Message::to_rule (#18447) 2025-06-05 12:48:29 -04:00
registry Remove Message::to_rule (#18447) 2025-06-05 12:48:29 -04:00
rules Remove Message::to_rule (#18447) 2025-06-05 12:48:29 -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 Remove Message::to_rule (#18447) 2025-06-05 12:48:29 -04:00
diagnostic.rs Add a SourceFile to OldDiagnostic (#18356) 2025-05-30 13:34:38 +00: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 Rename ruff_linter::Diagnostic to OldDiagnostic (#18355) 2025-05-29 15:04:31 -04:00
line_width.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
linter.rs Remove Message::to_rule (#18447) 2025-06-05 12:48:29 -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 Remove Message::to_rule (#18447) 2025-06-05 12:48:29 -04:00
package.rs
packaging.rs
preview.rs [flake8-simplify] enable fix in preview mode (SIM117) (#18208) 2025-05-20 08:34:50 -05:00
pyproject_toml.rs Add a SourceFile to OldDiagnostic (#18356) 2025-05-30 13:34:38 +00:00
registry.rs Remove Message::to_rule (#18447) 2025-06-05 12:48:29 -04: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 Remove Message::to_rule (#18447) 2025-06-05 12:48:29 -04: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 Remove Message::to_rule (#18447) 2025-06-05 12:48:29 -04:00
text_helpers.rs
upstream_categories.rs
violation.rs Add a ViolationMetadata::rule method (#18234) 2025-05-28 09:27:09 -04:00