Change NoqaCode
to a single string with an offset
#18736
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR explores two potential and closely related
NoqaCode
refactors. The first, in the first two commits, replaces the currentNoqaCode(&'static str, &'static str)
withNoqaCode(&'static str, usize)
, where theusize
is the index where the prefix and suffix separate. As detailed in this comment, it's not really possible to get a nice, single&'static str
, but we can hack around it with aLazyLock
to callformat!
in astatic
:ruff/crates/ruff_macros/src/map_codes.rs
Lines 293 to 295 in 7ff37f4
In the third commit, I just tried
NoqaCode(String, usize)
, which meansNoqaCode
can no longer beCopy
, but is otherwise a bit more natural.Test Plan
Codspeed benchmarks on this PR. The
&'static str
version didn't show any difference, but theString
version shows up to a 3% regression. Still, either of these representations should be a bit better if we end up needing to callRule::from_code
since they will avoid having to allocate a new string just to pass it there. A single string should be easier to store onruff_db::Diagnostic
too.ruff/crates/ruff_linter/src/registry.rs
Lines 19 to 21 in 9cb0243