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

enhance [ifs_same_cond] to warn same immutable method calls as well #10350

Merged
merged 4 commits into from
Mar 14, 2023

Conversation

J-ZhengLi
Copy link
Member

@J-ZhengLi J-ZhengLi commented Feb 16, 2023

fixes: #10272


changelog: Enhancement: [ifs_same_cond]: Now also detects immutable method calls.
#10350

@rustbot
Copy link
Collaborator

rustbot commented Feb 16, 2023

r? @xFrednet

(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 Feb 16, 2023
@J-ZhengLi
Copy link
Member Author

J-ZhengLi commented Feb 16, 2023

feel very insecure about this 🤦‍♂️ , is there any side-effect of doing this that I'm not aware of?

@PatchMixolydic
Copy link
Contributor

I'm not familiar with the method behind this check, but if it only checks the mutability of the binding, this might lead to false positives with interior mutable types:

use std::cell::Cell;

fn main() {
    let x = Cell::new(true);

    if !x.get() {
        unreachable!("x should be true");
    } else if !x.take() {
        unreachable!("x should've been true");
    } else if !x.get() {
        println!("OK");
    } else {
        unreachable!("x should be false");
    }
}

(Of course, it's possible that this is covered by SpanlessEq::eq_expr and I completely missed it ^^;)

@J-ZhengLi
Copy link
Member Author

J-ZhengLi commented Feb 17, 2023

I'm not familiar with the method behind this check, but if it only checks the mutability of the binding, this might lead to false positives with interior mutable types:

use std::cell::Cell;

fn main() {
    let x = Cell::new(true);

    if !x.get() {
        unreachable!("x should be true");
    } else if !x.take() {
        unreachable!("x should've been true");
    } else if !x.get() {
        println!("OK");
    } else {
        unreachable!("x should be false");
    }
}

(Of course, it's possible that this is covered by SpanlessEq::eq_expr and I completely missed it ^^;)

Thank you~ I just tested your example but it didn't trigger any warning, so I guess eq_expr somehow does cover this scenario. 😄

Oops, my bad, it doesn't, I'll try to fix it

@J-ZhengLi J-ZhengLi changed the title enhance [ifs_same_cond] to warn same immutable method calls as well [WIP] enhance [ifs_same_cond] to warn same immutable method calls as well Feb 17, 2023
@J-ZhengLi J-ZhengLi marked this pull request as draft February 17, 2023 01:31
@xFrednet
Copy link
Member

Hey, thank you for the PR. I would have also brought up the point of inner mutability. Thank you, @PatchMixolydic, for the comment.

I see three solutions:

  1. Scan the called method for inner mutability. This will only work for local methods, as external ones are not loaded by rustc AFAIK, and will be complex with little benefit.
  2. Create an allow list, which would avoid false positives, but would also be less flexible
  3. Add a configuration value that lints these methods by default, but can be turned off, if the project uses a lot of inner mutability.

From these options, I recommend the last one. You can take a look at this link, if you want to implement this solution: https://doc.rust-lang.org/clippy/development/adding_lints.html#adding-configuration-to-a-lint

You are welcome to reach out if you have any questions :)

@J-ZhengLi
Copy link
Member Author

J-ZhengLi commented Feb 20, 2023

Hey, thank you for the PR. I would have also brought up the point of inner mutability. Thank you, @PatchMixolydic, for the comment.

I see three solutions:

1. Scan the called method for inner mutability. This will only work for local methods, as external ones are not loaded by rustc AFAIK, and will be complex with little benefit.

2. Create an allow list, which would avoid false positives, but would also be less flexible

3. Add a configuration value that lints these methods by default, but can be turned off, if the project uses a lot of inner mutability.

From these options, I recommend the last one. You can take a look at this link, if you want to implement this solution: https://doc.rust-lang.org/clippy/development/adding_lints.html#adding-configuration-to-a-lint

You are welcome to reach out if you have any questions :)

well I do have a question, what if get the type of the caller instead and skip the check of Cell, RefCell types and mutable pointers completely, would that be achievable?

update: I noticed that mutable_key_type already got a configuration called ignore_interior_mutability, which seems allowing people to "whitelisting" certain types, maybe I could try using the same conf directly.

@xFrednet
Copy link
Member

xFrednet commented Feb 21, 2023

what if get the type of the caller instead

I believe this is already how the lint operates. I thought the question was about structs with field with internal mutability.

update: I noticed that mutable_key_type already got a configuration called ignore_interior_mutability, which seems allowing people to "whitelisting" certain types, maybe I could try using the same conf directly.

That sounds like a perfect approach, IMO 👍

@J-ZhengLi J-ZhengLi changed the title [WIP] enhance [ifs_same_cond] to warn same immutable method calls as well enhance [ifs_same_cond] to warn same immutable method calls as well Feb 24, 2023
@J-ZhengLi J-ZhengLi marked this pull request as ready for review February 24, 2023 02:49
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.

The implementation looks good overall. I have some small suggestions regarding the style and type resolution, but they should hopefully be simple to fix. :)

clippy_lints/src/copies.rs Outdated Show resolved Hide resolved
clippy_lints/src/copies.rs Outdated Show resolved Hide resolved
Comment on lines +586 to +585
|| path_to_local(caller_expr)
.and_then(|hid| find_binding_init(cx, hid))
.is_none()
Copy link
Member

Choose a reason for hiding this comment

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

Could you explain this check? I understand that it checks if the caller is a local value and if an init expression is present. But why should this be ignored if this is the case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because the documentation of find_binding_init says: ... Return None if the binding is mutable..., I thought it was pretty convenient lol, unless there are more optimized way to check whether a variable is mutable or not

Copy link
Member

Choose a reason for hiding this comment

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

Ah, okay, that's interesting. I understand why. I guess this is a good heuristic. It will have some false negatives, but those are better than false positives :)

clippy_lints/src/copies.rs Outdated Show resolved Hide resolved
tests/ui-toml/ifs_same_cond/clippy.toml Outdated Show resolved Hide resolved
@@ -437,7 +437,7 @@ define_Conf! {
///
/// The maximum size of the `Err`-variant in a `Result` returned from a function
(large_error_threshold: u64 = 128),
/// Lint: MUTABLE_KEY_TYPE.
/// Lint: MUTABLE_KEY_TYPE, IFS_SAME_COND.
///
/// A list of paths to types that should be treated like `Arc`, i.e. ignored but
Copy link
Member

Choose a reason for hiding this comment

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

The docs imply to me that Arc and other pointer types like Cell are included in the list, even if they aren't part of the default configuration. Do you maybe want to add them?

Copy link
Member Author

@J-ZhengLi J-ZhengLi Mar 7, 2023

Choose a reason for hiding this comment

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

Oh wait, I think I did the opposite of what that config was designed to do... the name of that config implies: "these types has inner mutability but will be ignored, so lint those anyway".

I got confused by the name lol, I thought it means skiping lint checks for them.

I realize it when the uitests for [mut_key] fails after I added them.

Copy link
Member

Choose a reason for hiding this comment

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

Now that you say it, it's a bit confusing 😅. I guess we'll leave it like this now, since it's not new.

@J-ZhengLi J-ZhengLi marked this pull request as draft March 7, 2023 12:32
@J-ZhengLi J-ZhengLi marked this pull request as ready for review March 8, 2023 02:57
@J-ZhengLi J-ZhengLi force-pushed the issue_10272 branch 4 times, most recently from 7ee5ade to 91b515b Compare March 8, 2023 06:38
…:ty`;

fix configuration of [`ifs_same_cond`];

add some style improvement for [`ifs_same_cond`];
@xFrednet
Copy link
Member

Looks good to me, thank you for the change and nice refactoring. :)

@bors r+

@bors
Copy link
Contributor

bors commented Mar 13, 2023

📌 Commit f4ccb06 has been approved by xFrednet

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Mar 13, 2023

⌛ Testing commit f4ccb06 with merge 2c9f74c...

bors added a commit that referenced this pull request Mar 13, 2023
enhance [`ifs_same_cond`] to warn same immutable method calls as well

fixes: #10272

---

changelog: Enhancement: [`ifs_same_cond`]: Now also detects immutable method calls.
[#10350](#10350)
<!-- changelog_checked -->
@bors
Copy link
Contributor

bors commented Mar 13, 2023

💔 Test failed - checks-action_test

@J-ZhengLi
Copy link
Member Author

J-ZhengLi commented Mar 14, 2023

Ahh, looks like some book test is failing, that's new for me, what should I do 😱

@xFrednet
Copy link
Member

Right, we've added that recently, and I still forget about mentioning that. Luckily we have bors as our bouncer ^^. Thank you for fixing the CI :)

@bors r+

@bors
Copy link
Contributor

bors commented Mar 14, 2023

📌 Commit 011bb46 has been approved by xFrednet

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Mar 14, 2023

⌛ Testing commit 011bb46 with merge ff843ac...

@bors
Copy link
Contributor

bors commented Mar 14, 2023

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

@bors bors merged commit ff843ac into rust-lang:master Mar 14, 2023
@J-ZhengLi J-ZhengLi deleted the issue_10272 branch April 7, 2023 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ifs_same_cond bails out on equal methods that return bool
5 participants