Skip to content

Commit 7c5095c

Browse files
committed
Auto merge of rust-lang#11195 - thvdveld:fix-option-env-ifs-equal-cond, r=Manishearth
fix: false positive for `option_env!` in `ifs_same_cond` 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: [`ifs_same_cond`]: fix false positive when using `option_env!` in if-let expressions.
2 parents fca1f9a + f743fec commit 7c5095c

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

clippy_utils/src/hir_utils.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,13 @@ impl HirEqInterExpr<'_, '_, '_> {
494494
loop {
495495
use TokenKind::{BlockComment, LineComment, Whitespace};
496496
if left_data.macro_def_id != right_data.macro_def_id
497-
|| (matches!(left_data.kind, ExpnKind::Macro(MacroKind::Bang, name) if name == sym::cfg)
498-
&& !eq_span_tokens(self.inner.cx, left_data.call_site, right_data.call_site, |t| {
499-
!matches!(t, Whitespace | LineComment { .. } | BlockComment { .. })
500-
}))
497+
|| (matches!(
498+
left_data.kind,
499+
ExpnKind::Macro(MacroKind::Bang, name)
500+
if name == sym::cfg || name == sym::option_env
501+
) && !eq_span_tokens(self.inner.cx, left_data.call_site, right_data.call_site, |t| {
502+
!matches!(t, Whitespace | LineComment { .. } | BlockComment { .. })
503+
}))
501504
{
502505
// Either a different chain of macro calls, or different arguments to the `cfg` macro.
503506
return false;

tests/ui/ifs_same_cond.rs

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ fn ifs_same_cond() {
4646
// ok, functions
4747
} else if v.len() == 42 {
4848
}
49+
50+
if let Some(env1) = option_env!("ENV1") {
51+
} else if let Some(env2) = option_env!("ENV2") {
52+
}
4953
}
5054

5155
fn issue10272() {

tests/ui/ifs_same_cond.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ LL | if 2 * a == 1 {
3636
| ^^^^^^^^^^
3737

3838
error: this `if` has the same condition as a previous `if`
39-
--> $DIR/ifs_same_cond.rs:54:15
39+
--> $DIR/ifs_same_cond.rs:58:15
4040
|
4141
LL | } else if a.contains("ah") {
4242
| ^^^^^^^^^^^^^^^^
4343
|
4444
note: same as this
45-
--> $DIR/ifs_same_cond.rs:53:8
45+
--> $DIR/ifs_same_cond.rs:57:8
4646
|
4747
LL | if a.contains("ah") {
4848
| ^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)