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 `disallowed_methods` #75

Open 0xLucqs opened 1 month ago

0xLucqs commented 1 month ago

What it does

Denies the configured methods and functions in clippy.toml

Note: Even though this lint is warn-by-default, it will only trigger if methods are defined in the clippy.toml file.

Why is this bad?

Some methods are undesirable in certain contexts, and it’s beneficial to lint for them as needed.

Example

An example clippy.toml configuration:

disallowed-methods = [
    # Can use a string as the path of the disallowed method.
    "core::clone::Clone::clone",
    # Can also use an inline table with a `path` key.
    { path = "core::array::ArrayTCloneImpl::clone" },
    # When using an inline table, can add a `reason` for why the method
    # is disallowed.
    { path = "core::clone::Clone::<Array<T>>::clone", reason = "no cloning arrays" },
]

(not entirely sure about the syntax of this toml but should probably look like that

// Example code where clippy issues a warning
let xs = array![1, 2, 3, 4];
xs.clone(); // Clone::<Array<T>>::clone is disallowed in the config.
// The diagnostic contains the message "no cloning arrays".

let _number = (@1_u32).clone(); // core::clone::Clone::clone is disallowed in the config.

Use instead:

// Example code which does not raise clippy warning
let mut xs = array![]; // ArrayTrait::new is _not_ disallowed in the config.
xs.push(123); // ArrayTrait::push is _not_ disallowed in the config.
Gianfranco99 commented 1 month ago

I'd like to take ownership of this issue. My approach will be to first confirm that the syntax of the clippy.toml file adheres to the intended format for disallowed methods and functions. Specifically, I'll ensure that both string and inline table formats are supported, as shown in the example, and that the reason field is properly handled when provided.

Once the configuration parsing is verified, I'll:

Test Existing Configurations: Create test cases based on the provided examples to ensure Clippy issues warnings for disallowed methods such as Clone::clone, and that custom messages (like "no cloning arrays") are shown when specified in the config.

Extend Linting Capabilities: If needed, I’ll expand the lint to cover edge cases or additional method paths that might not currently be captured, ensuring Clippy correctly warns against all methods listed in clippy.toml.

Ensure Flexibility and Clarity: I will ensure the implementation remains flexible, allowing for future extension of disallowed methods and functions in a way that’s easy to configure in the .toml file.

Documentation & Examples: Update any relevant documentation to clarify how to use this feature, ensuring users can properly configure the clippy.toml file for their specific needs.

I’ll also make sure that any existing functionality continues to work as expected, and I'll write additional tests to validate these changes.

Let me know if this approach works for you, and I’ll get started right away!

onlydustapp[bot] commented 1 month ago

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

od-hunter commented 1 month ago

Hi @0xLucqs , please can I be assigned please?

onlydustapp[bot] commented 1 month ago

Hey @od-hunter! 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

yes @od-hunter

od-hunter commented 1 month ago

@0xLucqs , thanks for assigning me ser. I’d need your assistance as I am not 100% clear on what I am supposed to do please.

0xLucqs commented 1 month ago

hello i think that we'll cancel the issue, did you start anything ?