-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Conversation
r? @xFrednet (rustbot has picked a reviewer for you, use r? to override) |
feel very insecure about this 🤦♂️ , is there any side-effect of doing this that I'm not aware of? |
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 |
Thank you~ Oops, my bad, it doesn't, I'll try to fix it |
ifs_same_cond
] to warn same immutable method calls as wellifs_same_cond
] to warn same immutable method calls as well
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:
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 update: I noticed that |
I believe this is already how the lint operates. I thought the question was about structs with field with internal mutability.
That sounds like a perfect approach, IMO 👍 |
ifs_same_cond
] to warn same immutable method calls as wellifs_same_cond
] to warn same immutable method calls as well
08533e2
to
b180be9
Compare
There was a problem hiding this 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. :)
|| path_to_local(caller_expr) | ||
.and_then(|hid| find_binding_init(cx, hid)) | ||
.is_none() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)
@@ -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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
7ee5ade
to
91b515b
Compare
…:ty`; fix configuration of [`ifs_same_cond`]; add some style improvement for [`ifs_same_cond`];
Looks good to me, thank you for the change and nice refactoring. :) @bors r+ |
💔 Test failed - checks-action_test |
|
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+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
fixes: #10272
changelog: Enhancement: [
ifs_same_cond
]: Now also detects immutable method calls.#10350