-
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
New lint: Implement ifs_same_cond_fn #4814
Conversation
What code would you suggest as a replacement? People will want to know how to change their code to silence the error. |
Good question. However, I believe that there is no obvious answer here. In my opinion, implicit mutating within if a.method() == some_value {
// ...
} else if a.method() == some_value {
// ...
} or even if a.method() {
// ...
} else if a.method() {
// ...
} you won't think that However, the code like let a_value = a.method();
if a_value == some_value {
/// ...
}
let b_value = a.method();
if b_value == some_value {
/// ...
} is more likely to be understood correctly. I've created a snippet on the Rust playground to explain what I mean in the code. Surely, this check won't suite everyones needs and there are cases when mutating within That's why this lint is |
And one more small note: even taking mutating functions into account, this case is pretty rare, and most of the time this lint will complain about situations like: if some_vec.len() == 1 {
// ...
} else if some_vec.len() == 1 {
// ...
} and that's the main purpose of the lint. I guess it will be obvious to user that it's an error and he'll be able to resolve this issue himself :) |
@flip1995 r? |
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.
Impl LGTM. Can you add a test, where the arguments of the function/method calls differ?
CHANGELOG.md
Outdated
@@ -8,6 +8,9 @@ document. | |||
|
|||
[3aea860...master](https://github.com/rust-lang/rust-clippy/compare/3aea860...master) | |||
|
|||
* New lints: | |||
* [`ifs_same_cond_fn`] [#4814](https://github.com/rust-lang/rust-clippy/pull/4814) |
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.
This has to be done anyway for many more PRs, so just write the changelog in the PR body.
clippy_lints/src/copies.rs
Outdated
/// … | ||
/// } | ||
/// ``` | ||
pub IFS_SAME_COND_FN, |
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.
SAME_FUNCTIONS_IN_IF_CONDITION
, as per naming conventions. (putting allow
before the lint name should produce an english sentence (more or less))
Can you add a example to the lint doc, that shows how to rewrite such a if chain? |
c6ef9d2
to
c473a3e
Compare
Weird: |
Travis probably fails because of #4825. Don't worry about this. |
Run ./util/dev Revert changelog entry Rename lint to same_functions_in_if_condition and add a doc example Add testcases with different arg in fn invocation
c473a3e
to
bbb8cd4
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.
Thanks! Waiting for the rustup.
Rollup of 4 Pull requests with new lints Rollup of pull requests - #4816 (New lint: zst_offset) - #4814 (New lint: Implement ifs_same_cond_fn) - #4807 (Add `large_stack_arrays` lint) - #4806 (Issue/4623) changelog: add [`zst_offset`] lint changelog: New lint: [`ifs_same_cond_fn`] cahngelog: Add new lint [large_stack_arrays] changelog: added lint [`tabs_in_doc_comments`]
changelog:
Current
ifs_same_cond
lint does not check function calls, since they can have side effects.However, such a code can be considered too implicit and counterintuitive, so I believe that a lint to detect such scenarios may be useful.