Closed
Description
fn main() {
let string;
let _x = if let true = true {
""
} else if true {
string = "x".to_owned();
&string
} else {
string = "y".to_owned();
&string
};
}
$ cargo clippy
warning: all if blocks contain the same code at the end
--> src/main.rs:10:5
|
10 | / &string
11 | | };
| |_____^
|
= note: `#[warn(clippy::branches_sharing_code)]` on by default
= note: The end suggestion probably needs some adjustments to use the expression result correctly
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
help: consider moving the end statements out like this
|
10 | }
11 | &string;
|
Clippy's preferred code is the following, which does not compile.
fn main() {
let string;
let _x = if let true = true {
""
} else if true {
string = "x".to_owned();
} else {
string = "y".to_owned();
}
&string;
}
error[E0308]: `if` and `else` have incompatible types
--> src/main.rs:5:12
|
3 | let _x = if let true = true {
| ______________-
4 | | ""
| | -- expected because of this
5 | | } else if true {
| |____________^
6 | || string = "x".to_owned();
7 | || } else {
8 | || string = "y".to_owned();
9 | || }
| || ^
| ||_____|
| |______`if` and `else` have incompatible types
| expected `&str`, found `()`
It's possible that clippy means the following, but I still consider this a false positive because I find the original code clearer than this.
fn main() {
let string;
let _x = if let true = true {
""
} else {
if true {
string = "x".to_owned();
} else {
string = "y".to_owned();
}
&string
};
}
Meta
cargo clippy -V
: clippy 0.1.53 (2e495d2 2021-04-08)rustc -Vv
:rustc 1.53.0-nightly (2e495d2e8 2021-04-08) binary: rustc commit-hash: 2e495d2e845cf27740e3665f718acfd3aa17253e commit-date: 2021-04-08 host: x86_64-unknown-linux-gnu release: 1.53.0-nightly LLVM version: 12.0.0