Skip to content

Commit 9925910

Browse files
authored
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
1 parent a3ee6bb commit 9925910

File tree

709 files changed

+931
-888
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

709 files changed

+931
-888
lines changed

Cargo.lock

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ruff/src/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,14 @@ impl FileCache {
349349
.iter()
350350
.map(|msg| {
351351
Message::diagnostic(
352-
msg.rule.into(),
353352
msg.body.clone(),
354353
msg.suggestion.clone(),
355354
msg.range,
356355
msg.fix.clone(),
357356
msg.parent,
358357
file.clone(),
359358
msg.noqa_offset,
359+
msg.rule,
360360
)
361361
})
362362
.collect()

crates/ruff/src/commands/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rayon::prelude::*;
1212
use rustc_hash::FxHashMap;
1313

1414
use ruff_db::panic::catch_unwind;
15-
use ruff_diagnostics::Diagnostic;
15+
use ruff_linter::Diagnostic;
1616
use ruff_linter::message::Message;
1717
use ruff_linter::package::PackageRoot;
1818
use ruff_linter::registry::Rule;

crates/ruff/src/commands/rule.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::ser::SerializeSeq;
66
use serde::{Serialize, Serializer};
77
use strum::IntoEnumIterator;
88

9-
use ruff_diagnostics::FixAvailability;
9+
use ruff_linter::FixAvailability;
1010
use ruff_linter::registry::{Linter, Rule, RuleNamespace};
1111

1212
use crate::args::HelpFormat;

crates/ruff/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use colored::Colorize;
1212
use log::{debug, warn};
1313
use rustc_hash::FxHashMap;
1414

15-
use ruff_diagnostics::Diagnostic;
15+
use ruff_linter::Diagnostic;
1616
use ruff_linter::codes::Rule;
1717
use ruff_linter::linter::{FixTable, FixerResult, LinterResult, ParseSource, lint_fix, lint_only};
1818
use ruff_linter::message::Message;

crates/ruff_dev/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ license = { workspace = true }
1414
ty = { workspace = true }
1515
ty_project = { workspace = true, features = ["schemars"] }
1616
ruff = { workspace = true }
17-
ruff_diagnostics = { workspace = true }
1817
ruff_formatter = { workspace = true }
1918
ruff_linter = { workspace = true, features = ["schemars"] }
2019
ruff_notebook = { workspace = true }

crates/ruff_dev/src/generate_docs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use itertools::Itertools;
1010
use regex::{Captures, Regex};
1111
use strum::IntoEnumIterator;
1212

13-
use ruff_diagnostics::FixAvailability;
13+
use ruff_linter::FixAvailability;
1414
use ruff_linter::registry::{Linter, Rule, RuleNamespace};
1515
use ruff_options_metadata::{OptionEntry, OptionsMetadata};
1616
use ruff_workspace::options::Options;

crates/ruff_dev/src/generate_rules_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::borrow::Cow;
88
use std::fmt::Write;
99
use strum::IntoEnumIterator;
1010

11-
use ruff_diagnostics::FixAvailability;
11+
use ruff_linter::FixAvailability;
1212
use ruff_linter::registry::{Linter, Rule, RuleNamespace};
1313
use ruff_linter::upstream_categories::UpstreamCategoryAndPrefix;
1414
use ruff_options_metadata::OptionsMetadata;

crates/ruff_diagnostics/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,5 @@ doctest = false
1616
[dependencies]
1717
ruff_text_size = { workspace = true }
1818

19-
anyhow = { workspace = true }
20-
log = { workspace = true }
2119
is-macro = { workspace = true }
2220
serde = { workspace = true, optional = true, features = [] }

crates/ruff_diagnostics/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
pub use diagnostic::Diagnostic;
21
pub use edit::Edit;
32
pub use fix::{Applicability, Fix, IsolationLevel};
43
pub use source_map::{SourceMap, SourceMarker};
5-
pub use violation::{AlwaysFixableViolation, FixAvailability, Violation, ViolationMetadata};
64

7-
mod diagnostic;
85
mod edit;
96
mod fix;
107
mod source_map;
11-
mod violation;

0 commit comments

Comments
 (0)