Add Checker::test_on_with to pass opaque data to lints - #684
Open
danielbodorin wants to merge 1 commit into
Open
danielbodorin wants to merge 1 commit into
danielbodorin wants to merge 1 commit into
Conversation
Let a library caller hand a lint arbitrary, structured, per-check data that isn't part of the source being linted: - `AstContext` gains an optional type-erased value plus a typed accessor, `caller_data::<T>()`, returning `None` when absent or mistyped. - `Checker::test_on_with(&ast, Box<dyn Any>)` attaches it; `test_on` is unchanged (equivalent to supplying no caller data). There is no change to the `Lint::pass` signature, so existing lints are unaffected. Documented in contributing.md with a CHANGELOG entry and a unit test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #683.
What
Adds a way for a program that embeds
selene-libto pass arbitrary, structured, per-check data to a lint without encoding it in the source being linted.AstContextgains an optional opaque (Box<dyn Any>) value plus a typed accessorcaller_data::<T>().Checker::test_on_with(&ast, Box<dyn Any>)runs the checker with that value attached;test_onis unchanged and equivalent to supplying no caller data.Why
Lints receive
ast, aContext(per-checker, e.g. the standard library) and anAstContext(per-file, e.g. scopes), but there is no channel for the caller to hand a lint its own per-file information (metadata the embedding program knows that isn't in the source). This adds a typed, opaque one.How
Anyand stored onAstContext; only the caller and the lint agree on its concrete type.caller_data::<T>()returnsNonewhen no data was supplied or the type does not match.test_onandtest_on_withshare a privaterun_lintshelper, so lint execution is identical either way.Lint::passsignature, so existing lints are unaffected.Tests and docs
docs/src/contributing.md; CHANGELOG updated.cargo test,cargo fmt --check, andcargo clippypass; existing behavior is unchanged.