#linter #lint

nightly nag-toolkit

Toolkit for writing custom cargo-nag lints

1 unstable release

0.1.0 Jan 12, 2023

#79 in #lint

MIT/Apache

6KB
51 lines

Helper crate for creating nag lints.

Usage looks like following:

// Needed to link into rustc
#![feature(rustc_private)]

extern crate rustc_lint;
extern crate rustc_middle;
// Not used directly in this example, but needed for `declare_lints!()` to work.
extern crate rustc_session;

mod my_cool_lint {
    // This is a lint pass. It holds all the lint logic.
    pub(super) struct MyCoolLintPass;

    // This needed for `nag_toolkit` to know how to create your lint pass.
    impl nag_toolkit::NagLint for MyCoolLintPass {
        // There's no need to save `tcx`, you'll have access to it later
        fn new(_tcx: rustc_middle::ty::TyCtxt) -> Self {
            Self
        }
    }

    // You can choose to implement `EarlyLintPass` instead if you want to.
    impl<'tcx> rustc_lint::LateLintPass<'tcx> for MyCoolLintPass {
        // Implement various `check_` methods here.
    }
}

nag_toolkit::declare_lints!(
    // `my_cool_lint` is a name that will be used for `#[allow()]`/`#[deny()]`/etc.
    (lint: my_cool_lint(Warn, "An example lint."));
    // specify `early` instead of `late`, if you implemented `EarlyLintPass`
    (pass: late my_cool_lint::MyCoolLintPass);
);

// As a result you have a function `register`, which you can pass to `nag_driver`:
let _: fn(&rustc_session::Session, &mut rustc_lint::LintStore) = register;
// And a `MY_COOL_LINT: Lint` static that can be used inside of lint passes:
let _: &rustc_lint::Lint = &MY_COOL_LINT;
// It's private by default, but you can put custom visibility before lint name.

Dependencies

~4KB