Skip to content

Commit 0b77c35

Browse files
committed
Auto merge of #6169 - ThibsG:SameFunctionsInIfConditionIgnoreMacro, r=ebroto
Fix FP in `same_functions_in_if_condition` lint about condition as macro Ignore expr that originate from a macro. Fixes: #6168 changelog: none
2 parents de31dda + e212408 commit 0b77c35

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

clippy_lints/src/copies.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{eq_expr_value, SpanlessEq, SpanlessHash};
1+
use crate::utils::{eq_expr_value, in_macro, SpanlessEq, SpanlessHash};
22
use crate::utils::{get_parent_expr, higher, if_sequence, snippet, span_lint_and_note, span_lint_and_then};
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_hir::{Arm, Block, Expr, ExprKind, MatchSource, Pat, PatKind};
@@ -220,6 +220,10 @@ fn lint_same_fns_in_if_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
220220
};
221221

222222
let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool {
223+
// Do not lint if any expr originates from a macro
224+
if in_macro(lhs.span) || in_macro(rhs.span) {
225+
return false;
226+
}
223227
// Do not spawn warning if `IFS_SAME_COND` already produced it.
224228
if eq_expr_value(cx, lhs, rhs) {
225229
return false;

tests/ui/same_functions_in_if_condition.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,14 @@ fn ifs_same_cond_fn() {
7777
}
7878
}
7979

80-
fn main() {}
80+
fn main() {
81+
// macro as condition (see #6168)
82+
let os = if cfg!(target_os = "macos") {
83+
"macos"
84+
} else if cfg!(target_os = "windows") {
85+
"windows"
86+
} else {
87+
"linux"
88+
};
89+
println!("{}", os);
90+
}

0 commit comments

Comments
 (0)