keep-starknet-strange / cairo-lint

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

Add `manual_unwrap_or_default` #67

Closed 0xLucqs closed 1 month ago

0xLucqs commented 1 month ago

What it does

Checks if a match or if let expression can be simplified using .unwrap_or_default().

Why is this bad?

It can be done in one call with .unwrap_or_default().

Example

let x: Option<Array<u32>> = Some(array![]);
let y: Array<u32> = match x {
    Some(v) => v,
    None => Default::default(),
};

let x: Option<Array<u32>> = Some(ArrayTrait::new());
let y: Vec<Array<u32>> = if let Some(v) = x {
    v
} else {
    Default::default()
};

Use instead:

let x: Option<Array<u32>> = Some(array![]);
let y: Array<u32> = x.unwrap_or_default();

let x: Option<Array<u32>> = Some(ArrayTrait::new());
let y: Array<u32> = x.unwrap_or_default();

(the examples are for arrays but it can work with any type that implements default

JoE11-y commented 1 month ago

Hi @0xLucqs can I help out with this?

onlydustapp[bot] commented 1 month ago

Hey @JoE11-y! Thanks for showing interest. We've created an application for you to contribute to Cairo lint. Go check it out on OnlyDust!

0xLucqs commented 1 month ago

@JoE11-y sure, do you have any idea on how to solve this ?

Benjtalkshow commented 1 month ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

Hello, Can I work on this task? I will be glad if this task is assigned to me.

JoE11-y commented 1 month ago

@JoE11-y sure, do you have any idea on how to solve this ?

Yes I can. Will take reference from here.

https://rust-lang.github.io/rust-clippy/master/index.html#/manual_unwrap_or_default

Plus I have extensive XP working with Cairo.

lindsaymoralesb commented 1 month ago

Hi @0xLucqs I'd like to hop into this issue if still available. I'd use the clippy and given examples as a reference to see how it should behave/work and also I already have the exp on this project with the collapsible_if_else issue i solved before.

onlydustapp[bot] commented 1 month ago

Hey @lindsaymoralesb! Thanks for showing interest. We've created an application for you to contribute to Cairo lint. Go check it out on OnlyDust!

chachaleo commented 1 month ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I have contributed to a lot of projects in Starknet, I have experience in Rust

How I plan on tackling this issue

Read the code base and merged PR and implement in a similar way

0xLucqs commented 1 month ago

okay @JoE11-y i'll assign the issue to you

0xLucqs commented 1 month ago

@chachaleo @lindsaymoralesb @Benjtalkshow i'll create other issues for you