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? |
CHANGELOG.md
Outdated
| [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.
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.
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
flip1995
left a comment
There was a problem hiding this comment.
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_condlint 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.