-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from all commits
8a9492a
f0ae2b7
f4ccb06
011bb46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. The docs imply to me that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 [ There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
/// for the generic parameters for determining interior mutability | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ignore-interior-mutability = ["std::cell::Cell"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#![warn(clippy::ifs_same_cond)] | ||
#![allow(clippy::if_same_then_else, clippy::comparison_chain)] | ||
|
||
fn main() {} | ||
|
||
fn issue10272() { | ||
use std::cell::Cell; | ||
|
||
// Because the `ignore-interior-mutability` configuration | ||
// is set to ignore for `std::cell::Cell`, the following `get()` calls | ||
// should trigger warning | ||
let x = Cell::new(true); | ||
if x.get() { | ||
} else if !x.take() { | ||
} else if x.get() { | ||
} else { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: this `if` has the same condition as a previous `if` | ||
--> $DIR/ifs_same_cond.rs:15:15 | ||
| | ||
LL | } else if x.get() { | ||
| ^^^^^^^ | ||
| | ||
note: same as this | ||
--> $DIR/ifs_same_cond.rs:13:8 | ||
| | ||
LL | if x.get() { | ||
| ^^^^^^^ | ||
= note: `-D clippy::ifs-same-cond` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|
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 notThere 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 :)