Based on Cognitive Complexity by G. Ann Campbell.
Getting started
Add complexity
to your Cargo.toml
.
[dependencies]
complexity = "0.1"
syn = "1"
You'll need to import the Complexity
trait, and probably some things from
syn
.
use Complexity;
use ;
Loops and branching increment the complexity by one each. Some syntax structures introduce a "nesting" level which affects certain sub items.
let func: ItemFn = parse_quote! ;
assert_eq!;
Certain structures a rewarded. Particularly a match
statement, which only
increases the complexity by one no matter how many branches there are. (It does
increase the nesting level though.)
let func: ItemFn = parse_quote! ;
assert_eq!;
An example is provided to calculate and nicely print out the cognitive complexity of each function and method in an entire Rust file. See examples/lint-files.rs. You can run it on Rust files like this:
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.