Struct marker_api::context::AstContext
source · #[repr(C)]pub struct AstContext<'ast> { /* private fields */ }
Expand description
This context will be passed to each LintPass
call to enable the user
to emit lints and to retrieve nodes by the given ids.
Implementations§
source§impl<'ast> AstContext<'ast>
impl<'ast> AstContext<'ast>
pub fn lint_level_at( &self, lint: &'static Lint, node: impl Into<EmissionNode> ) -> Level
pub fn emit_lint<F>( &self, lint: &'static Lint, node: impl Into<EmissionNode>, msg: impl ToString, span: &Span<'ast>, decorate: F )where F: FnOnce(&mut DiagnosticBuilder<'ast>),
sourcepub fn item(&self, id: ItemId) -> Option<ItemKind<'ast>>
pub fn item(&self, id: ItemId) -> Option<ItemKind<'ast>>
This returns the ItemKind
belonging to the given ItemId
. It can
return None
in special cases depending on the used driver.
Driver information
- Rustc’s driver will always return a valid item.
pub fn body(&self, id: BodyId) -> &Body<'ast>
sourcepub fn resolve_ty_ids(&self, path: &str) -> &[TyDefId]
pub fn resolve_ty_ids(&self, path: &str) -> &[TyDefId]
This function tries to resolve the given path to the corresponding TyDefId
.
The slice might be empty if the path could not be resolved. This could be
due to an error in the path or because the linted crate doesn’t have the
required dependency. The function can also return multiple TyDefId
s,
if there are multiple crates with different versions in the dependency tree.
The returned ids are unordered and, depending on the driver, can also
change during different calls. The slice should not be stored across
check_*
calls.
Here is a simple example, how the method could be used:
// Get the type of an expression and check that it's an ADT
if let SemTyKind::Adt(ty) = expr.ty() {
// Check if the id belongs to the path
if cx.resolve_ty_ids("example::path::Item").contains(&ty.ty_id()) {
// ...
}
}