Skip to content

Add Checker::test_on_with to pass opaque data to lints - #684

Open
danielbodorin wants to merge 1 commit into
Kampfkarren:mainfrom
danielbodorin:feature/lint-caller-data
Open

danielbodorin wants to merge 1 commit into
Kampfkarren:mainfrom
danielbodorin:feature/lint-caller-data

Conversation

@danielbodorin

Copy link
Copy Markdown

Closes #683.

What

Adds a way for a program that embeds selene-lib to pass arbitrary, structured, per-check data to a lint without encoding it in the source being linted.

  • AstContext gains an optional opaque (Box<dyn Any>) value plus a typed accessor caller_data::<T>().
  • Checker::test_on_with(&ast, Box<dyn Any>) runs the checker with that value attached; test_on is unchanged and equivalent to supplying no caller data.
let diagnostics = checker.test_on_with(&ast, Box::new(MyData { /* ... */ }));

// in a lint's pass():
if let Some(data) = ast_context.caller_data::<MyData>() {
    // use the caller-provided data
}

Why

Lints receive ast, a Context (per-checker, e.g. the standard library) and an AstContext (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

  • The value is type-erased via Any and stored on AstContext; only the caller and the lint agree on its concrete type. caller_data::<T>() returns None when no data was supplied or the type does not match.
  • test_on and test_on_with share a private run_lints helper, so lint execution is identical either way.
  • No change to the Lint::pass signature, so existing lints are unaffected.

Tests and docs

  • Unit test covering the absent, present, and mismatched-type cases.
  • New "Receiving data from the caller" section in docs/src/contributing.md; CHANGELOG updated.
  • cargo test, cargo fmt --check, and cargo clippy pass; existing behavior is unchanged.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: pass opaque per-check data from the library caller to lints

1 participant