#代码检查 #规则 #源代码 #分析器

bin+lib splint

自定义 Rust 代码检查

5 个版本 (2 个稳定版)

1.1.0 2024 年 5 月 28 日
1.0.0 2024 年 5 月 28 日
0.2.0 2024 年 5 月 27 日
0.1.1 2024 年 5 月 20 日
0.1.0 2024 年 5 月 20 日

#927 in 开发工具

Download history 309/week @ 2024-05-17 490/week @ 2024-05-24 52/week @ 2024-05-31 4/week @ 2024-06-07 3/week @ 2024-06-28 19/week @ 2024-07-05 49/week @ 2024-07-26 3/week @ 2024-08-02

每月下载 52 次

MIT 许可证

30KB
650


自定义 Rust 代码检查

命令行使用

# Install Splint
cargo install splint

# Run splint
splint [-r <rules.(json|toml)>] src/**/*.rs # Splint only works on rust files

Rust 分析器集成

将以下内容添加到您的 vscode 或等效编辑器的 settings.json 文件中。
如果您有一个非标准规则文件(见下文),请添加 -r <path>

{
    // ...
    "rust-analyzer.check.overrideCommand": [
        "splint",
        "-qa",
        "**/*.rs"
    ],
    "rust-analyzer.cargo.buildScripts.overrideCommand": [
    
        "splint",
        "-qa",
        "**/*.rs"
    ],
    // ...
}

规则

以下规则会在文件中的任何位置寻找 .unwrap() 调用序列。
由于它使用来自 proc_macro2 的解析令牌流,所以无需担心空白字符。
如果没有提供规则文件,splint 将在当前工作目录 (cwd) 中查找 .splint.(json|toml)splint.(json|toml) 文件。

// JSON
{
    "rules": {
        "Disallow Unwrap": {
        /* The name of your lint  */                        "name": "Disallow Unwrap",
        /* Reasoning for the lint */                        "description": "`.unwrap()` should be discouraged where possible, as it leads to less than usefull panics.",
        /* (optional) Describe a fix or alternative */      "help": "Favour '?' for Results, or handling with unwrap_or(). At the least give some diagnostics with .expect()",
        /* (optional) Link to more information */           "more": "https://doc.rust-lang.net.cn/std/result/enum.Result.html#method.unwrap",
        /* Whether or not this lint should panic*/          "fails": false,
        /* A replacement for the match */                   "replace": ".expect(\"...\")",
        /* The inclusive range highlighted */               "range": [0, 3], // In this case . -> )
        /* Type/Value matching */                           "pattern": [
        /* Type is one of Punct/Ident/Delim */                  ["Punct", "."],
        /* Where Punctuation handles punctuation, */            ["Ident", "unwrap"],
        /* Delim brackets, and Ident other strings/ */          ["Delim", "("],
        /* Regex in value is defined by surrounding '/' */      ["Delim", ")"]
        /* The value can also be `null` */                  ]
        }
    }
}
# TOML
[rules."Disallow Unwrap"]
name = "Disallow Unwrap"
description = "`.unwrap()` should be discouraged where possible, as it leads to less than usefull panics."
help = "Favour '?' for Results, or handling with unwrap_or(). At the least give some diagnostics with .expect()"
fails = false
range = [0, 3]
pattern = [
    ["Punct", "."],
    ["Ident", "unwrap"],
    ["Delim", "("],
    ["Delim", ")"]
]

感谢

依赖项

~89MB
~1.5M SLoC