Skip to content

Commit

Permalink
Auto merge of #7118 - TaKO8Ki:fix-false-positive-in-comparison-chain,…
Browse files Browse the repository at this point in the history
… 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`]
  • Loading branch information
bors committed Apr 30, 2021
2 parents a55912c + 0dff377 commit 14f1551
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion clippy_lints/src/comparison_chain.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::ty::implements_trait;
use clippy_utils::{get_trait_def_id, if_sequence, is_else_clause, paths, SpanlessEq};
use clippy_utils::{get_trait_def_id, if_sequence, in_constant, is_else_clause, paths, SpanlessEq};
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
Expand Down Expand Up @@ -64,6 +64,10 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
return;
}

if in_constant(cx, expr.hir_id) {
return;
}

// Check that there exists at least one explicit else condition
let (conds, _) = if_sequence(expr);
if conds.len() < 2 {
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/comparison_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,32 @@ mod issue_5212 {
}
}

enum Sign {
Negative,
Positive,
Zero,
}

impl Sign {
const fn sign_i8(n: i8) -> Self {
if n == 0 {
Sign::Zero
} else if n > 0 {
Sign::Positive
} else {
Sign::Negative
}
}
}

const fn sign_i8(n: i8) -> Sign {
if n == 0 {
Sign::Zero
} else if n > 0 {
Sign::Positive
} else {
Sign::Negative
}
}

fn main() {}

0 comments on commit 14f1551

Please sign in to comment.