5 releases (2 stable)

1.1.0 May 28, 2024
1.0.0 May 28, 2024
0.2.0 May 27, 2024
0.1.1 May 20, 2024
0.1.0 May 20, 2024

#992 in Development tools

Download history 52/week @ 2024-07-29 43/week @ 2024-09-30 2/week @ 2024-10-07

224 downloads per month

MIT license

30KB
650 lines


Custom Rust Linting

Cli Usage

# Install Splint
cargo install splint

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

Integration with Rust Analyzer

Add the following to your settings.json file in vscode or equivalent.
Add -r <path> if you have a non-standard rules file (see below).

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

Rules

The following rule looks for a sequence of .unwrap() anywhere in the file.
You don't need to worry about whitespace, as it uses a parsed stream of tokens from proc_macro2.
If no rules file is provided, splint will look for a .splint.(json|toml) or splint.(json|toml) file in the cwd.

// 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.org/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", ")"]
]

Thanks

Dependencies

~93MB
~2M SLoC