#[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>

source

pub fn ast(&self) -> &AstMap<'ast>

Returns an AstMap instance, which can be used to retrieve AST nodes by their ids.

source

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
source

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 TyDefIds, 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()) {
        // ...
    }
}

Trait Implementations§

source§

impl<'ast> Debug for MarkerContext<'ast>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'ast> RefUnwindSafe for MarkerContext<'ast>

§

impl<'ast> Send for MarkerContext<'ast>

§

impl<'ast> Sync for MarkerContext<'ast>

§

impl<'ast> Unpin for MarkerContext<'ast>

§

impl<'ast> UnwindSafe for MarkerContext<'ast>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.