Struct marker_api::context::MarkerContext
source · #[repr(C)]pub struct MarkerContext<'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> MarkerContext<'ast>
impl<'ast> MarkerContext<'ast>
sourcepub fn ast(&self) -> &AstMap<'ast>
pub fn ast(&self) -> &AstMap<'ast>
Returns an AstMap
instance, which can be used to retrieve AST nodes
by their ids.
sourcepub fn emit_lint(
&self,
lint: &'static Lint,
node: impl EmissionNode<'ast>,
msg: impl Into<String>
) -> DiagnosticBuilder<'ast>
pub fn emit_lint( &self, lint: &'static Lint, node: impl EmissionNode<'ast>, msg: impl Into<String> ) -> DiagnosticBuilder<'ast>
This function is used to emit a lint.
Every lint emission, is bound to one specific node in the AST. This
node is used to check the lint level and is the default Span
of
the diagnostic message. See EmissionNode
for more information.
The Span
can be overwritten with DiagnosticBuilder::span
.
The message parameter, will be the main message of the created diagnostic. This message and all messages emitted as part of the created diagnostic should start with a lower letter, according to rustc’s dev guide.
The function will return a DiagnosticBuilder
which can be used to decorate
the diagnostic message, with notes and help messages. These customizations can
be moved into a conditional closure, to improve performance under some circumstances.
See DiagnosticBuilder::decorate
for more information.
The diagnostic message will be emitted when the builder instance is dropped.
Example 1
cx.emit_lint(LINT, node, "<lint message>");
The code above will roughly generate the following error message:
warning: <lint message> <-- The message that is set by this function
--> path/file.rs:1:1
|
1 | node
| ^^^^
|
Example 2
cx.emit_lint(LINT, node, "<lint message>").help("<text>");
The DiagnosticBuilder::help
will add a help message like this:
warning: <lint message>
--> path/file.rs:1:1
|
1 | node
| ^^^^
|
= help: <text> <-- The added help message
Example 3
Adding a help message using DiagnosticBuilder::decorate
:
cx.emit_lint(LINT, node, "<lint message>").decorate(|diag| {
// This closure is only called, if the diagnostic will be emitted.
// Here you can create a beautiful help message.
diag.help("<text>");
});
This will create the same help message as in example 2, but it will be faster if the lint is suppressed. The emitted message would look like this:
warning: <lint message>
--> path/file.rs:1:1
|
1 | node
| ^^^^
|
= help: <text> <-- The added help message
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()) {
// ...
}
}