keep-starknet-strange / cairo-lint

A collection of lints to catch common mistakes and improve your Cairo code.
21 stars 35 forks source link

Add `collapsible_match` #73

Open 0xLucqs opened 2 months ago

0xLucqs commented 2 months ago

What it does

Finds nested match or if let expressions where the patterns may be “collapsed” together without adding any branches.

Note that this lint is not intended to find all cases where nested match patterns can be merged, but only cases where merging would most likely make the code more readable.

Why is this bad?

It is unnecessarily verbose and complex.

Example

fn func(opt: Option<Result<u64, felt252>>) {
    let n = match opt {
        Some(n) => match n {
            Ok(n) => n,
            _ => return,
        }
        None => return,
    };
}

Use instead:

fn func(opt: Option<Result<u64, felt252>>) {
    let n = match opt {
        Some(Ok(n)) => n,
        _ => return,
    };
}
0xLucqs commented 2 months ago

@lindsaymoralesb do you want to do that one ?

lindsaymoralesb commented 2 months ago

Yes! Sure @0xLucqs