Skip to content

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented May 13, 2025

This PR is stacked on #18074 and uses the
separation between CacheMessage and the other diagnostic types added there to
store a Rule directly on CacheMessage and convert it back to a &'static str for use by Diagnostic and DiagnosticMessage.

In the first commit, I added a generated Rule::pascal_name method for converting
back to the Pascal-case name currently used by Diagnostic and DiagnosticMessage,
but in the second commit I used the heck crate to generalize our existing kebab_case
macro to convert the ViolationMetadata::rule_name output to kebab-case and use
kebab-case everywhere. heck was already in our dependency graph because it's what
strum uses internally for case conversions.

I think that's probably preferable long-term, but it might be a breaking change in
the LSP because the lint name is used as the title for a quick fix, if a suggestion is
unavailable. Otherwise Diagnostic::name is only used for debug! logging.

Test Plan

Existing tests

ntBre added 2 commits May 13, 2025 16:40
Summary
--

This PR is stacked on #18074 and uses the
separation between `CacheMessage` and the other diagnostic types added there to
store a `Rule` directly on `CacheMessage` and convert it back to a `&'static
str` for use by `Diagnostic` and `DiagnosticMessage`.

I think what we really want is to use only the kebab-case names everywhere, but
I don't think we can accomplish that with `Diagnostic::new` living in the
`ruff_diagnostics` crate, and the proc macros being called in `ruff_linter`.
`Diagnostic::new` relies on `ViolationMetadata::rule_name`, which just calls
`stringify!(#name)`.

Test Plan
--

Existing tests
@ntBre ntBre added the diagnostics Related to reporting of diagnostics. label May 13, 2025
@github-actions
Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

ℹ️ ecosystem check encountered format errors. (no format changes; 1 project error)

openai/openai-cookbook (error)

warning: Detected debug build without --no-cache.
error: Failed to read examples/gpt4-1_prompting_guide.ipynb: Expected a Jupyter Notebook, which must be internally stored as JSON, but this file isn't valid JSON: expected `,` or `]` at line 580 column 29

Formatter (preview)

ℹ️ ecosystem check encountered format errors. (no format changes; 1 project error)

openai/openai-cookbook (error)

ruff format --preview

warning: Detected debug build without --no-cache.
error: Failed to read examples/gpt4-1_prompting_guide.ipynb: Expected a Jupyter Notebook, which must be internally stored as JSON, but this file isn't valid JSON: expected `,` or `]` at line 580 column 29

@ntBre ntBre marked this pull request as ready for review May 13, 2025 21:26
@ntBre ntBre requested a review from MichaReiser May 13, 2025 21:26
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice speed up.

This is great. I would probably remove the heck dependency again, given that we already had an implementation of kebab_case (also reduces the size of your PR).

My only concern is that name is also used in the GitLab reporter to compute a fingerprint

fn fingerprint(message: &Message, project_path: &str, salt: u64) -> u64 {
let mut hasher = DefaultHasher::new();
salt.hash(&mut hasher);
message.name().hash(&mut hasher);
project_path.hash(&mut hasher);
hasher.finish()
}

This means, all diagnostics will be changed for gitlab users.

I don't think I'm too concerned by this because the implementation already uses std::hash::DefaultHasher which isn't guaranteed to be stable across rustc versions (or even platforms! we should probably fix this).

@MichaReiser MichaReiser added the internal An internal refactor or improvement label May 14, 2025
@ntBre
Copy link
Contributor Author

ntBre commented May 14, 2025

Oh interesting about the gitlab diagnostic. As soon as I saw it was being hashed, I assumed it was okay. From the gitlab docs, it sounds like they shouldn't be too user-facing and only used to deduplicate entries in the merge request widget, so hopefully it's okay.

@ntBre
Copy link
Contributor Author

ntBre commented May 14, 2025

Hm, it does look like we marked a previous change to the hash as breaking: #7159. One other benefit of keeping heck is that we could potentially go the other way back to pascal-case and (temporarily) use that for the gitlab emitter, if we want to avoid a breaking change.

@ntBre ntBre merged commit de3af65 into brent/inline-diagnostic-kind May 14, 2025
34 checks passed
@ntBre ntBre deleted the brent/static-diagnostic-name branch May 14, 2025 20:26
ntBre added a commit that referenced this pull request May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diagnostics Related to reporting of diagnostics. internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants