complexity

Crates.iocomplexity
lib.rscomplexity
version0.2.0
sourcesrc
created_at2020-04-19 07:45:45.533771
updated_at2020-04-26 15:54:49.780445
descriptionCalculate cognitive complexity of Rust code.
homepagehttps://github.com/rossmacarthur/complexity
repositoryhttps://github.com/rossmacarthur/complexity
max_upload_size
id231854
size54,261
Ross MacArthur (rossmacarthur)

documentation

README

complexity

Calculate cognitive complexity of Rust code.

Crates.io version Documentation Build status

Based on Cognitive Complexity by G. Ann Campbell.

Getting started

Add complexity to your Cargo.toml.

[dependencies]
complexity = "0.2"
syn = "1"

You'll need to bring the Complexity trait into scope, and probably some things from syn.

use complexity::Complexity;
use syn::{Expr, parse_quote};

Complexity of expressions and other syn types is as simple as calling .complexity() on an instance of that type.

let expr: Expr = parse_quote! {
    for element in iterable { // +1
        if something {        // +2 (nesting = 1)
            do_something();
        }
    }
};
assert_eq!(expr.complexity(), 3);

Examples

The implementation of cognitive complexity in this crate is heavily based on Cognitive Complexity by G. Ann Campbell. And reading it would be beneficial to understanding how the complexity index is calculated.

Loops and structures that introduce branching increment the complexity by one each. Some syntax structures introduce a "nesting" level which increases some expressions complexity by that nesting level in addition to their regular increment. In the example below we see how two nested loops and an if statement can produce quite a high complexity of 7.

use complexity::Complexity;
use syn::{ItemFn, parse_quote};

let func: ItemFn = parse_quote! {
    fn sum_of_primes(max: u64) -> u64 {
        let mut total = 0;
        'outer: for i in 1..=max {   // +1
            for j in 2..i {          // +2 (nesting = 1)
                if i % j == 0 {      // +3 (nesting = 2)
                    continue 'outer; // +1
                }
            }
            total += i;
        }
    }
};
assert_eq!(func.complexity(), 7);

But some structures are 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.) In the example below we see how even though there are a lot of branches in the code (which would contribute a lot to a more traditional cyclomatic complexity measurement), the complexity is quite low at 1.

use complexity::Complexity;
use syn::{ItemFn, parse_quote};

let func: ItemFn = parse_quote! {
    fn get_words(number: u64) -> &str {
        match number {       // +1
            1 => "one",
            2 => "a couple",
            3 => "a few",
            _ => "lots",
        }
    }
};
assert_eq!(func.complexity(), 1);

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:

cargo run --example lint-files -- src/

License

Licensed under either of

at your option.

Commit count: 23

cargo fmt