#lint #linter #cargo #cargo-subcommand

nightly nag-toolkit

自定义 cargo-nag 代码检查的 Toolkit

1 个不稳定版本

0.1.0 2023年1月12日

#4#lints

MIT/Apache

6KB
51

创建 nag 代码检查的辅助包。

用法如下

// 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.

依赖

~4KB