-use crate::utils::{if_sequence, parent_node_is_if_expr, span_help_and_lint, SpanlessEq};
+use crate::utils::{
+ get_trait_def_id, if_sequence, implements_trait, parent_node_is_if_expr, paths, span_help_and_lint, SpanlessEq,
+};
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_lint_pass, declare_tool_lint};
{
return;
}
+
+ // Check that the type being compared implements `core::cmp::Ord`
+ let ty = cx.tables.expr_ty(lhs1);
+ let is_ord = get_trait_def_id(cx, &paths::ORD).map_or(false, |id| implements_trait(cx, ty, id, &[]));
+
+ if !is_ord {
+ return;
+ }
} else {
// We only care about comparison chains
return;
}
}
+#[allow(clippy::float_cmp)]
+fn g(x: f64, y: f64, z: f64) {
+ // Ignored: f64 doesn't implement Ord
+ if x > y {
+ a()
+ } else if x < y {
+ b()
+ }
+
+ // Ignored: f64 doesn't implement Ord
+ if x > y {
+ a()
+ } else if x < y {
+ b()
+ } else {
+ c()
+ }
+
+ // Ignored: f64 doesn't implement Ord
+ if x > y {
+ a()
+ } else if y > x {
+ b()
+ } else {
+ c()
+ }
+
+ // Ignored: f64 doesn't implement Ord
+ if x > 1.0 {
+ a()
+ } else if x < 1.0 {
+ b()
+ } else if x == 1.0 {
+ c()
+ }
+}
+
+fn h<T: Ord>(x: T, y: T, z: T) {
+ if x > y {
+ a()
+ } else if x < y {
+ b()
+ }
+
+ if x > y {
+ a()
+ } else if x < y {
+ b()
+ } else {
+ c()
+ }
+
+ if x > y {
+ a()
+ } else if y > x {
+ b()
+ } else {
+ c()
+ }
+}
+
fn main() {}
|
= help: Consider rewriting the `if` chain to use `cmp` and `match`.
-error: aborting due to 4 previous errors
+error: `if` chain can be rewritten with `match`
+ --> $DIR/comparison_chain.rs:117:5
+ |
+LL | / if x > y {
+LL | | a()
+LL | | } else if x < y {
+LL | | b()
+LL | | }
+ | |_____^
+ |
+ = help: Consider rewriting the `if` chain to use `cmp` and `match`.
+
+error: `if` chain can be rewritten with `match`
+ --> $DIR/comparison_chain.rs:123:5
+ |
+LL | / if x > y {
+LL | | a()
+LL | | } else if x < y {
+LL | | b()
+LL | | } else {
+LL | | c()
+LL | | }
+ | |_____^
+ |
+ = help: Consider rewriting the `if` chain to use `cmp` and `match`.
+
+error: `if` chain can be rewritten with `match`
+ --> $DIR/comparison_chain.rs:131:5
+ |
+LL | / if x > y {
+LL | | a()
+LL | | } else if y > x {
+LL | | b()
+LL | | } else {
+LL | | c()
+LL | | }
+ | |_____^
+ |
+ = help: Consider rewriting the `if` chain to use `cmp` and `match`.
+
+error: aborting due to 7 previous errors