Skip to content

Commit 14f1551

Browse files
committed
Auto merge of #7118 - TaKO8Ki:fix-false-positive-in-comparison-chain, r=giraffate
Fix a false-positive inside const fn in `comparison_chain` closes #7082 changelog: fix a false-positive inside const fn in [`comparison_chain`]
2 parents a55912c + 0dff377 commit 14f1551

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

clippy_lints/src/comparison_chain.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::ty::implements_trait;
3-
use clippy_utils::{get_trait_def_id, if_sequence, is_else_clause, paths, SpanlessEq};
3+
use clippy_utils::{get_trait_def_id, if_sequence, in_constant, is_else_clause, paths, SpanlessEq};
44
use rustc_hir::{BinOpKind, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -64,6 +64,10 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
6464
return;
6565
}
6666

67+
if in_constant(cx, expr.hir_id) {
68+
return;
69+
}
70+
6771
// Check that there exists at least one explicit else condition
6872
let (conds, _) = if_sequence(expr);
6973
if conds.len() < 2 {

tests/ui/comparison_chain.rs

+28
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,32 @@ mod issue_5212 {
203203
}
204204
}
205205

206+
enum Sign {
207+
Negative,
208+
Positive,
209+
Zero,
210+
}
211+
212+
impl Sign {
213+
const fn sign_i8(n: i8) -> Self {
214+
if n == 0 {
215+
Sign::Zero
216+
} else if n > 0 {
217+
Sign::Positive
218+
} else {
219+
Sign::Negative
220+
}
221+
}
222+
}
223+
224+
const fn sign_i8(n: i8) -> Sign {
225+
if n == 0 {
226+
Sign::Zero
227+
} else if n > 0 {
228+
Sign::Positive
229+
} else {
230+
Sign::Negative
231+
}
232+
}
233+
206234
fn main() {}

0 commit comments

Comments
 (0)