Fix suggestion in manual_range_contains when using float

This commit is contained in:
Takayuki Nakata 2020-11-11 22:36:53 +09:00
parent d0858d0f36
commit 5f64867e1d
4 changed files with 27 additions and 3 deletions

View file

@ -222,13 +222,14 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
let name = snippet_with_applicability(cx, name_span, "_", &mut applicability); let name = snippet_with_applicability(cx, name_span, "_", &mut applicability);
let lo = snippet_with_applicability(cx, l_span, "_", &mut applicability); let lo = snippet_with_applicability(cx, l_span, "_", &mut applicability);
let hi = snippet_with_applicability(cx, u_span, "_", &mut applicability); let hi = snippet_with_applicability(cx, u_span, "_", &mut applicability);
let space = if lo.ends_with('.') { " " } else { "" };
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,
MANUAL_RANGE_CONTAINS, MANUAL_RANGE_CONTAINS,
span, span,
&format!("manual `{}::contains` implementation", range_type), &format!("manual `{}::contains` implementation", range_type),
"use", "use",
format!("({}{}{}).contains(&{})", lo, range_op, hi, name), format!("({}{}{}{}).contains(&{})", lo, space, range_op, hi, name),
applicability, applicability,
); );
} else if !combine_and && ord == Some(lord) { } else if !combine_and && ord == Some(lord) {
@ -251,13 +252,14 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
let name = snippet_with_applicability(cx, name_span, "_", &mut applicability); let name = snippet_with_applicability(cx, name_span, "_", &mut applicability);
let lo = snippet_with_applicability(cx, l_span, "_", &mut applicability); let lo = snippet_with_applicability(cx, l_span, "_", &mut applicability);
let hi = snippet_with_applicability(cx, u_span, "_", &mut applicability); let hi = snippet_with_applicability(cx, u_span, "_", &mut applicability);
let space = if lo.ends_with('.') { " " } else { "" };
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,
MANUAL_RANGE_CONTAINS, MANUAL_RANGE_CONTAINS,
span, span,
&format!("manual `!{}::contains` implementation", range_type), &format!("manual `!{}::contains` implementation", range_type),
"use", "use",
format!("!({}{}{}).contains(&{})", lo, range_op, hi, name), format!("!({}{}{}{}).contains(&{})", lo, space, range_op, hi, name),
applicability, applicability,
); );
} }

View file

@ -38,4 +38,9 @@ fn main() {
x >= 8 || x >= 12; x >= 8 || x >= 12;
x < 12 || 12 < x; x < 12 || 12 < x;
x >= 8 || x <= 12; x >= 8 || x <= 12;
// Fix #6315
let y = 3.;
(0. ..1.).contains(&y);
!(0. ..=1.).contains(&y);
} }

View file

@ -38,4 +38,9 @@ fn main() {
x >= 8 || x >= 12; x >= 8 || x >= 12;
x < 12 || 12 < x; x < 12 || 12 < x;
x >= 8 || x <= 12; x >= 8 || x <= 12;
// Fix #6315
let y = 3.;
y >= 0. && y < 1.;
y < 0. || y > 1.;
} }

View file

@ -72,5 +72,17 @@ error: manual `!RangeInclusive::contains` implementation
LL | 999 < x || 1 > x; LL | 999 < x || 1 > x;
| ^^^^^^^^^^^^^^^^ help: use: `!(1..=999).contains(&x)` | ^^^^^^^^^^^^^^^^ help: use: `!(1..=999).contains(&x)`
error: aborting due to 12 previous errors error: manual `Range::contains` implementation
--> $DIR/range_contains.rs:44:5
|
LL | y >= 0. && y < 1.;
| ^^^^^^^^^^^^^^^^^ help: use: `(0. ..1.).contains(&y)`
error: manual `!RangeInclusive::contains` implementation
--> $DIR/range_contains.rs:45:5
|
LL | y < 0. || y > 1.;
| ^^^^^^^^^^^^^^^^ help: use: `!(0. ..=1.).contains(&y)`
error: aborting due to 14 previous errors