Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new lint that disallow renaming parameters in trait functions #11540

Merged
merged 3 commits into from
May 12, 2024

Conversation

J-ZhengLi
Copy link
Member

@J-ZhengLi J-ZhengLi commented Sep 20, 2023

fixes: #11443
fixes: #486

changelog: add new lint [renamed_function_params]

Note that the lint name is not final, because I have a bad reputation in naming things, and I don't trust myself.

@rustbot
Copy link
Collaborator

rustbot commented Sep 20, 2023

r? @Jarcho

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 20, 2023
@xFrednet
Copy link
Member

I haven't looked at the implementation, so this might already be implemented: I would recommend that it allows removing leading underscores from the names. Here is an example, of what I mean:

trait Foo {
    fn foo(_param: u32) {}
}

struct FooImpl;

impl Foo for FooImpl {
    //     vvvvv This should be allowed
    fn foo(param: u32) {
        println!("{param}");
    }
}

Potentially, it could also allow adding an underscore, but here I'm uncertain if that's a good idea. Still, this would be the example for that case:

trait Bar {
    fn bar(param: u32) {
        println!("{param}");
    }
}

struct BarImpl;

impl Bar for BarImpl {
    //     vvvvvv Maybe this could be allowed?
    fn bar(_param: u32) {}
}

@J-ZhengLi
Copy link
Member Author

I haven't looked at the implementation, so this might already be implemented: I would recommend that it allows removing leading underscores from the names. Here is an example, of what I mean:

trait Foo {
    fn foo(_param: u32) {}
}

struct FooImpl;

impl Foo for FooImpl {
    //     vvvvv This should be allowed
    fn foo(param: u32) {
        println!("{param}");
    }
}

Potentially, it could also allow adding an underscore, but here I'm uncertain if that's a good idea. Still, this would be the example for that case:

trait Bar {
    fn bar(param: u32) {
        println!("{param}");
    }
}

struct BarImpl;

impl Bar for BarImpl {
    //     vvvvvv Maybe this could be allowed?
    fn bar(_param: u32) {}
}

It should already be implemented :D
in fact any name starts with underscore will be skipped, that includes names in trait def and trait impl

Copy link
Member

@Centri3 Centri3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor nits.

I still think we should restrict this to some traits (by default) using an attribute like clippy::cant_rename (bikeshedding pending!). We can add this to many libstd traits and crate authors can opt into this if they really want. Consistency isn't as big of a deal when the trait is rarely used already.

clippy_lints/src/functions/renamed_function_params.rs Outdated Show resolved Hide resolved
clippy_lints/src/functions/renamed_function_params.rs Outdated Show resolved Hide resolved
clippy_lints/src/functions/renamed_function_params.rs Outdated Show resolved Hide resolved
clippy_lints/src/functions/renamed_function_params.rs Outdated Show resolved Hide resolved
clippy_lints/src/functions/renamed_function_params.rs Outdated Show resolved Hide resolved
clippy_lints/src/functions/renamed_function_params.rs Outdated Show resolved Hide resolved
@J-ZhengLi J-ZhengLi force-pushed the issue11443 branch 3 times, most recently from d1fc8d5 to 1d4f27b Compare September 25, 2023 02:55
@bors
Copy link
Collaborator

bors commented Oct 8, 2023

☔ The latest upstream changes (presumably #11550) made this pull request unmergeable. Please resolve the merge conflicts.

@Jarcho
Copy link
Contributor

Jarcho commented Oct 30, 2023

Since you've already looked at this. r? @Centri3

@rustbot rustbot assigned Centri3 and unassigned Jarcho Oct 30, 2023
@J-ZhengLi
Copy link
Member Author

J-ZhengLi commented Jan 18, 2024

Perhaps this was forgotten 😭 , even I couldn't remember what I did

Plus, seems like she's busy btw? so... umm @rustbot r? @rust-lang/clippy

@rustbot rustbot assigned Manishearth and unassigned Centri3 Jan 18, 2024
@J-ZhengLi J-ZhengLi force-pushed the issue11443 branch 2 times, most recently from ec3b457 to 5a03c9b Compare March 28, 2024 08:30
@J-ZhengLi
Copy link
Member Author

some extra info: there are 2644 occurance of this lint after running lintcheck --recursive

@Manishearth
Copy link
Member

r? clippy

still busy

@rustbot rustbot assigned xFrednet and unassigned Manishearth Mar 29, 2024
@xFrednet
Copy link
Member

Sure, it's on my todo list for the next week :D

Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the implementation, this looks good to me. Time to get the FCP started.

@rustbot label +S-final-comment-period -S-waiting-on-author

FCP: https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/FCP.20.60renamed_function_params.60/near/431407447

@rustbot rustbot added the S-final-comment-period Status: final comment period it will be merged unless new objections are raised (~1 week) label Apr 4, 2024
@xFrednet xFrednet removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 4, 2024
@xFrednet
Copy link
Member

On zulip it was mentioned that this lint should ideally have a configuration to ignore some lints, like From, TryFrom, and FromStr. Once that is done I believe we can move forward with this. There was also this suggestion:

It looks a bit similar to disallowed_names. Perhaps the name could be disallow_parameter_renames or disallow_renames?

I like the current name, but can understand the reasoning :)

@xFrednet xFrednet added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Apr 12, 2024
@J-ZhengLi
Copy link
Member Author

@xFrednet Finally got some time working on this~ I've added a configuration for it, but again... not confident about that config's name, which is ignored-traits for now, if there's better name plz let me know~

some time I really wish there's an AI that could tell me what should I name my variables/functions/lint/config lol


use super::RENAMED_FUNCTION_PARAMS;

static IGNORED_TRAIT_IDS: OnceLock<DefIdSet> = OnceLock::new();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I should use OnceLock here, haven't seen them in other lints, maybe there's some kind of concern? idk

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe some other lints store this in the lint pass struct instead. The IDs can then be collected in fn check_crate(). I would prefer that solution over having a static object. Otherwise, I don't believe there is a reason against OnceLock

Copy link
Member Author

@J-ZhengLi J-ZhengLi May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, I just thought it might be redundant to having a list of trait paths, and a set of their ids stored in the same struct

nvm, it does seems very common

Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two NITs that should hopefully be easy to fix and then this should be good to go :D

(Sorry for the delay with the review, my past few weeks have been busy)

/// - By default, the following traits are ignored: `From`, `TryFrom`, `FromStr`
/// - `".."` can be used as part of the list to indicate that the configured values should be appended to the
/// default configuration of Clippy. By default, any configuration will replace the default value.
(ignored_traits: Vec<String> = DEFAULT_IGNORED_TRAITS.iter().map(ToString::to_string).collect()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is too general, maybe allow_renamed_params_for or renamed_function_params_alllowed_for?

@@ -671,6 +688,7 @@ fn deserialize(file: &SourceFile) -> TryConf {
extend_vec_if_indicator_present(&mut conf.conf.doc_valid_idents, DEFAULT_DOC_VALID_IDENTS);
extend_vec_if_indicator_present(&mut conf.conf.disallowed_names, DEFAULT_DISALLOWED_NAMES);
extend_vec_if_indicator_present(&mut conf.conf.allowed_prefixes, DEFAULT_ALLOWED_PREFIXES);
extend_vec_if_indicator_present(&mut conf.conf.ignored_traits, DEFAULT_IGNORED_TRAITS);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! :D


use super::RENAMED_FUNCTION_PARAMS;

static IGNORED_TRAIT_IDS: OnceLock<DefIdSet> = OnceLock::new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe some other lints store this in the lint pass struct instead. The IDs can then be collected in fn check_crate(). I would prefer that solution over having a static object. Otherwise, I don't believe there is a reason against OnceLock

apply review suggestions by @Centri3:
use multi suggestion;
change output message format;
add macro expansion check & tests;
@J-ZhengLi
Copy link
Member Author

J-ZhengLi commented May 7, 2024

(Sorry for the delay with the review, my past few weeks have been busy)

no worries, me too XD

ping @xFrednet

@xFrednet xFrednet removed the S-final-comment-period Status: final comment period it will be merged unless new objections are raised (~1 week) label May 12, 2024
@xFrednet
Copy link
Member

In Clippy's realm, a rule takes flight,
No renaming parameters, to keep code tight.
Thanks for the effort, I merge with delight.

=^.^=

@bors
Copy link
Collaborator

bors commented May 12, 2024

📌 Commit 2358189 has been approved by xFrednet

It is now in the queue for this repository.

bors added a commit that referenced this pull request May 12, 2024
add new lint that disallow renaming parameters in trait functions

fixes: #11443
fixes: #486

changelog: add new lint [`renamed_function_params`]

Note that the lint name is not final, because I have a bad reputation in naming things, and I don't trust myself.
@bors
Copy link
Collaborator

bors commented May 12, 2024

⌛ Testing commit 2358189 with merge d47bc8a...

@bors
Copy link
Collaborator

bors commented May 12, 2024

💔 Test failed - checks-action_test

@J-ZhengLi
Copy link
Member Author

J-ZhengLi commented May 12, 2024

ahh, forgot the cargo collect-metadata again, third times a charm I guess~

@xFrednet
Copy link
Member

xFrednet commented May 12, 2024

:P

Feel free to r=me after the collection :)

@bors delegate+

@bors
Copy link
Collaborator

bors commented May 12, 2024

✌️ @J-ZhengLi, you can now approve this pull request!

If @xFrednet told you to "r=me" after making some further change, please make that change, then do @bors r=@xFrednet

@J-ZhengLi
Copy link
Member Author

should be good now

@bors r=@xFrednet

@bors
Copy link
Collaborator

bors commented May 12, 2024

📌 Commit 46659ac has been approved by xFrednet

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented May 12, 2024

⌛ Testing commit 46659ac with merge 7cfb9a0...

@bors
Copy link
Collaborator

bors commented May 12, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: xFrednet
Pushing 7cfb9a0 to master...

@bors bors merged commit 7cfb9a0 into rust-lang:master May 12, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint to disallow renaming function parameters lint against renaming of method arguments in trait impls
7 participants