mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-02 07:22:42 +00:00
fix a false-positive inside const fn in comparison_chain
This commit is contained in:
parent
79b9eb5371
commit
65778fa5e8
@ -1,7 +1,7 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_help;
|
use clippy_utils::diagnostics::span_lint_and_help;
|
||||||
use clippy_utils::ty::implements_trait;
|
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, is_else_clause, paths, SpanlessEq};
|
||||||
use rustc_hir::{BinOpKind, Expr, ExprKind};
|
use rustc_hir::{BinOpKind, Expr, ExprKind, Node};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
|
|
||||||
@ -64,6 +64,10 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if parent_node_is_if_const_fn(cx, expr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Check that there exists at least one explicit else condition
|
// Check that there exists at least one explicit else condition
|
||||||
let (conds, _) = if_sequence(expr);
|
let (conds, _) = if_sequence(expr);
|
||||||
if conds.len() < 2 {
|
if conds.len() < 2 {
|
||||||
@ -123,3 +127,11 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
|
|||||||
fn kind_is_cmp(kind: BinOpKind) -> bool {
|
fn kind_is_cmp(kind: BinOpKind) -> bool {
|
||||||
matches!(kind, BinOpKind::Lt | BinOpKind::Gt | BinOpKind::Eq)
|
matches!(kind, BinOpKind::Lt | BinOpKind::Gt | BinOpKind::Eq)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parent_node_is_if_const_fn(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
||||||
|
match cx.tcx.hir().find(cx.tcx.hir().get_parent_item(expr.hir_id)) {
|
||||||
|
Some(Node::Item(item)) => rustc_mir::const_eval::is_const_fn(cx.tcx, item.def_id.to_def_id()),
|
||||||
|
Some(Node::ImplItem(impl_item)) => rustc_mir::const_eval::is_const_fn(cx.tcx, impl_item.def_id.to_def_id()),
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -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() {}
|
fn main() {}
|
||||||
|
Loading…
Reference in New Issue
Block a user