12 个版本 (5 个重大更新)

0.5.0 2023 年 12 月 28 日
0.4.3 2023 年 12 月 2 日
0.4.2 2023 年 11 月 25 日
0.3.0 2023 年 10 月 5 日
0.0.0-placeholder2022 年 11 月 4 日

120开发工具

Download history 3242/week @ 2024-04-23 1786/week @ 2024-04-30 1386/week @ 2024-05-07 2256/week @ 2024-05-14 1442/week @ 2024-05-21 1863/week @ 2024-05-28 2083/week @ 2024-06-04 1937/week @ 2024-06-11 1277/week @ 2024-06-18 741/week @ 2024-06-25 1194/week @ 2024-07-02 1069/week @ 2024-07-09 1135/week @ 2024-07-16 2139/week @ 2024-07-23 2818/week @ 2024-07-30 2469/week @ 2024-08-06

8,612 每月下载量
4 crates 中使用

MIT/Apache

295KB
6K SLoC

Marker API

Crates.io License: MIT OR Apache-2.0

marker_api 提供了 AST 及所有相关类型表示,用于为 Marker(Rust 的实验性代码检查接口)创建自定义 lint crates

注意

Marker 处于早期开发阶段,一些功能尚未实现,API 仍不稳定。

有关限制和计划功能的列表,请参阅 Marker 的 Readme

目标

  • 稳定性:Marker 的 API 设计侧重于稳定性和可扩展性。目标是实现向后兼容性,以便任何在 1.0.0 版本之后编写的 lint 都能编译并在多年后继续工作。
  • 可用性:Marker 的 API 侧重于可用性,在保证 Marker 的稳定性前提下。类型遵循常见的设计模式和命名约定,让您可以直接关注 lint 逻辑。
  • 驱动程序无关:每次代码分析都需要一个驱动程序来解析代码并提供更多信息。Marker 的 API 被设计为驱动程序无关,允许它支持未来的编译器和潜在的 IDE。(目前,rustc 是唯一可用的驱动程序)

用法

本节将介绍如何设置自己的 lint crate。如果您只想运行自定义的 lint,请检查 Marker 的 CLI 接口 cargo_marker。本节的其余部分假设您已安装 cargo_marker

模板

开始的最简单方法是使用 Marker 的 lint crate 模板,它已经包含了所有依赖项、示例代码和可用的测试设置。

手动设置

Cargo.toml

要开始使用,请创建一个新的Rust包,编译成库(cargo init --lib)。之后,编辑Cargo.toml,将包编译成动态库,并将marker_api作为依赖项包含进来。您只需将以下内容添加到您的Cargo.toml文件中

[lib]
crate-type = ["cdylib"]

[dependencies]
marker_api = "0.5.0"
marker_utils = "0.5.0"

src/lib.rs

lint包需要提供一个LintPass特质的实现,并使用实现类型调用marker_api::export_lint_pass宏。以下是一个最小模板

use marker_api::prelude::*;
use marker_api::{LintPass, LintPassInfo, LintPassInfoBuilder};

// This is the struct that will implement the `LintPass` trait.
#[derive(Default)]
struct MyLintPass;

// This macro allow Marker to load the lint crate. Only one lint pass can be
// exported per lint crate.
marker_api::export_lint_pass!(MyLintPass);

// This macro declares a new lint, that can later be emitted
marker_api::declare_lint! {
    /// # What it does
    /// Here you can explain what your lint does. The description supports normal
    /// markdown.
    ///
    /// # Example
    /// ```rs
    /// // Bad example
    /// ```
    ///
    /// Use instead:
    /// ```rs
    /// // Good example
    /// ```
    MY_LINT,
    Warn,
}

// This is the actual `LintPass` implementation, which will be called by Marker.
impl LintPass for MyLintPass {
    fn info(&self) -> LintPassInfo {
        LintPassInfoBuilder::new(Box::new([MY_LINT])).build()
    }
}

现在您可以在LintPass特质中实现不同的check_*函数。

UI测试

要自动测试您的lint,您可能想查看marker_uitest包。

就是这样。祝您lint愉快!

贡献

我们非常欢迎贡献!如果您遇到任何问题或对改进有建议,请查看Marker的GitHub仓库

许可证

版权(c)2022-2023 Rust-Marker

Rust-Marker根据MIT许可证或Apache许可证(版本2.0)的条款分发。

请参阅LICENSE-APACHELICENSE-MIT

依赖项