Auto merge of #10350 - J-ZhengLi:issue_10272, r=xFrednet
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](https://github.com/rust-lang/rust-clippy/pull/10350) <!-- changelog_checked -->
This commit is contained in:
commit
ff843ac9ae
11 changed files with 196 additions and 58 deletions
1
tests/ui-toml/ifs_same_cond/clippy.toml
Normal file
1
tests/ui-toml/ifs_same_cond/clippy.toml
Normal file
|
|
@ -0,0 +1 @@
|
|||
ignore-interior-mutability = ["std::cell::Cell"]
|
||||
18
tests/ui-toml/ifs_same_cond/ifs_same_cond.rs
Normal file
18
tests/ui-toml/ifs_same_cond/ifs_same_cond.rs
Normal file
|
|
@ -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 {
|
||||
}
|
||||
}
|
||||
15
tests/ui-toml/ifs_same_cond/ifs_same_cond.stderr
Normal file
15
tests/ui-toml/ifs_same_cond/ifs_same_cond.stderr
Normal file
|
|
@ -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
|
||||
|
||||
|
|
@ -43,4 +43,30 @@ fn ifs_same_cond() {
|
|||
}
|
||||
}
|
||||
|
||||
fn issue10272() {
|
||||
let a = String::from("ha");
|
||||
if a.contains("ah") {
|
||||
} else if a.contains("ah") {
|
||||
// Trigger this lint
|
||||
} else if a.contains("ha") {
|
||||
} else if a == "wow" {
|
||||
}
|
||||
|
||||
let p: *mut i8 = std::ptr::null_mut();
|
||||
if p.is_null() {
|
||||
} else if p.align_offset(0) == 0 {
|
||||
} else if p.is_null() {
|
||||
// ok, p is mutable pointer
|
||||
} else {
|
||||
}
|
||||
|
||||
let x = std::cell::Cell::new(true);
|
||||
if x.get() {
|
||||
} else if !x.take() {
|
||||
} else if x.get() {
|
||||
// ok, x is interior mutable type
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -35,5 +35,17 @@ note: same as this
|
|||
LL | if 2 * a == 1 {
|
||||
| ^^^^^^^^^^
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
error: this `if` has the same condition as a previous `if`
|
||||
--> $DIR/ifs_same_cond.rs:49:15
|
||||
|
|
||||
LL | } else if a.contains("ah") {
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
|
||||
note: same as this
|
||||
--> $DIR/ifs_same_cond.rs:48:8
|
||||
|
|
||||
LL | if a.contains("ah") {
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue