|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_help; |
2 | 2 | use clippy_utils::ty::implements_trait; |
3 | 3 | use clippy_utils::{get_trait_def_id, if_sequence, is_else_clause, paths, SpanlessEq}; |
4 | | -use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 4 | +use rustc_hir::{BinOpKind, Expr, ExprKind, Node}; |
5 | 5 | use rustc_lint::{LateContext, LateLintPass}; |
6 | 6 | use rustc_session::{declare_lint_pass, declare_tool_lint}; |
7 | 7 |
|
@@ -64,6 +64,10 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain { |
64 | 64 | return; |
65 | 65 | } |
66 | 66 |
|
| 67 | + if parent_node_is_if_const_fn(cx, expr) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
67 | 71 | // Check that there exists at least one explicit else condition |
68 | 72 | let (conds, _) = if_sequence(expr); |
69 | 73 | if conds.len() < 2 { |
@@ -123,3 +127,11 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain { |
123 | 127 | fn kind_is_cmp(kind: BinOpKind) -> bool { |
124 | 128 | matches!(kind, BinOpKind::Lt | BinOpKind::Gt | BinOpKind::Eq) |
125 | 129 | } |
| 130 | + |
| 131 | +fn parent_node_is_if_const_fn(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { |
| 132 | + match cx.tcx.hir().find(cx.tcx.hir().get_parent_item(expr.hir_id)) { |
| 133 | + Some(Node::Item(item)) => rustc_mir::const_eval::is_const_fn(cx.tcx, item.def_id.to_def_id()), |
| 134 | + Some(Node::ImplItem(impl_item)) => rustc_mir::const_eval::is_const_fn(cx.tcx, impl_item.def_id.to_def_id()), |
| 135 | + _ => false, |
| 136 | + } |
| 137 | +} |
0 commit comments