fix: false positive for option_env! in ifs

Clippy had a false positive for with `ifs_same_cond` when two
if-let expressions have an `option_env!` macro. The fix is similar to the
`env!` macro fix.

The following example had a clippy error:

```rust
if let Some(env1) = option_env!("ENV1") {
    // ...
} else if let Some(env2) = option_env!("ENV2") {
    // ...
}
```

See https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=01b85c61b56ddd900117fb247af04824

changelog: Fix [`ifs_same_cond`] false positive when using `option_env!` in if-let expressions.
This commit is contained in:
Thibaut Vandervelden 2023-07-20 11:49:27 +02:00
parent 0f31fff59e
commit f743fec6b0
3 changed files with 13 additions and 6 deletions

View file

@ -494,10 +494,13 @@ impl HirEqInterExpr<'_, '_, '_> {
loop {
use TokenKind::{BlockComment, LineComment, Whitespace};
if left_data.macro_def_id != right_data.macro_def_id
|| (matches!(left_data.kind, ExpnKind::Macro(MacroKind::Bang, name) if name == sym::cfg)
&& !eq_span_tokens(self.inner.cx, left_data.call_site, right_data.call_site, |t| {
!matches!(t, Whitespace | LineComment { .. } | BlockComment { .. })
}))
|| (matches!(
left_data.kind,
ExpnKind::Macro(MacroKind::Bang, name)
if name == sym::cfg || name == sym::option_env
) && !eq_span_tokens(self.inner.cx, left_data.call_site, right_data.call_site, |t| {
!matches!(t, Whitespace | LineComment { .. } | BlockComment { .. })
}))
{
// Either a different chain of macro calls, or different arguments to the `cfg` macro.
return false;

View file

@ -46,6 +46,10 @@ fn ifs_same_cond() {
// ok, functions
} else if v.len() == 42 {
}
if let Some(env1) = option_env!("ENV1") {
} else if let Some(env2) = option_env!("ENV2") {
}
}
fn issue10272() {

View file

@ -36,13 +36,13 @@ LL | if 2 * a == 1 {
| ^^^^^^^^^^
error: this `if` has the same condition as a previous `if`
--> $DIR/ifs_same_cond.rs:54:15
--> $DIR/ifs_same_cond.rs:58:15
|
LL | } else if a.contains("ah") {
| ^^^^^^^^^^^^^^^^
|
note: same as this
--> $DIR/ifs_same_cond.rs:53:8
--> $DIR/ifs_same_cond.rs:57:8
|
LL | if a.contains("ah") {
| ^^^^^^^^^^^^^^^^