Skip to content

Commit

Permalink
Auto merge of #6320 - giraffate:fix_suggestion_in_manual_range_contai…
Browse files Browse the repository at this point in the history
…ns_using_float, r=llogiq

Fix suggestion in `manual_range_contains` when using float

Fix #6315

changelog: Fix suggestion in `manual_range_contains` when using float
  • Loading branch information
bors committed Nov 14, 2020
2 parents cf7b4b0 + 5f64867 commit 408b615
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
6 changes: 4 additions & 2 deletions clippy_lints/src/ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 lo = snippet_with_applicability(cx, l_span, "_", &mut applicability);
let hi = snippet_with_applicability(cx, u_span, "_", &mut applicability);
let space = if lo.ends_with('.') { " " } else { "" };
span_lint_and_sugg(
cx,
MANUAL_RANGE_CONTAINS,
span,
&format!("manual `{}::contains` implementation", range_type),
"use",
format!("({}{}{}).contains(&{})", lo, range_op, hi, name),
format!("({}{}{}{}).contains(&{})", lo, space, range_op, hi, name),
applicability,
);
} else if !combine_and && ord == Some(lord) {
Expand All @@ -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 lo = snippet_with_applicability(cx, l_span, "_", &mut applicability);
let hi = snippet_with_applicability(cx, u_span, "_", &mut applicability);
let space = if lo.ends_with('.') { " " } else { "" };
span_lint_and_sugg(
cx,
MANUAL_RANGE_CONTAINS,
span,
&format!("manual `!{}::contains` implementation", range_type),
"use",
format!("!({}{}{}).contains(&{})", lo, range_op, hi, name),
format!("!({}{}{}{}).contains(&{})", lo, space, range_op, hi, name),
applicability,
);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/range_contains.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ fn main() {
x >= 8 || x >= 12;
x < 12 || 12 < x;
x >= 8 || x <= 12;

// Fix #6315
let y = 3.;
(0. ..1.).contains(&y);
!(0. ..=1.).contains(&y);
}
5 changes: 5 additions & 0 deletions tests/ui/range_contains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ fn main() {
x >= 8 || x >= 12;
x < 12 || 12 < x;
x >= 8 || x <= 12;

// Fix #6315
let y = 3.;
y >= 0. && y < 1.;
y < 0. || y > 1.;
}
14 changes: 13 additions & 1 deletion tests/ui/range_contains.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,17 @@ error: manual `!RangeInclusive::contains` implementation
LL | 999 < x || 1 > 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

0 comments on commit 408b615

Please sign in to comment.