Skip to content

Commit 408b615

Browse files
committed
Auto merge of rust-lang#6320 - giraffate:fix_suggestion_in_manual_range_contains_using_float, r=llogiq
Fix suggestion in `manual_range_contains` when using float Fix rust-lang#6315 changelog: Fix suggestion in `manual_range_contains` when using float
2 parents cf7b4b0 + 5f64867 commit 408b615

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

clippy_lints/src/ranges.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,14 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
222222
let name = snippet_with_applicability(cx, name_span, "_", &mut applicability);
223223
let lo = snippet_with_applicability(cx, l_span, "_", &mut applicability);
224224
let hi = snippet_with_applicability(cx, u_span, "_", &mut applicability);
225+
let space = if lo.ends_with('.') { " " } else { "" };
225226
span_lint_and_sugg(
226227
cx,
227228
MANUAL_RANGE_CONTAINS,
228229
span,
229230
&format!("manual `{}::contains` implementation", range_type),
230231
"use",
231-
format!("({}{}{}).contains(&{})", lo, range_op, hi, name),
232+
format!("({}{}{}{}).contains(&{})", lo, space, range_op, hi, name),
232233
applicability,
233234
);
234235
} else if !combine_and && ord == Some(lord) {
@@ -251,13 +252,14 @@ fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'
251252
let name = snippet_with_applicability(cx, name_span, "_", &mut applicability);
252253
let lo = snippet_with_applicability(cx, l_span, "_", &mut applicability);
253254
let hi = snippet_with_applicability(cx, u_span, "_", &mut applicability);
255+
let space = if lo.ends_with('.') { " " } else { "" };
254256
span_lint_and_sugg(
255257
cx,
256258
MANUAL_RANGE_CONTAINS,
257259
span,
258260
&format!("manual `!{}::contains` implementation", range_type),
259261
"use",
260-
format!("!({}{}{}).contains(&{})", lo, range_op, hi, name),
262+
format!("!({}{}{}{}).contains(&{})", lo, space, range_op, hi, name),
261263
applicability,
262264
);
263265
}

tests/ui/range_contains.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ fn main() {
3838
x >= 8 || x >= 12;
3939
x < 12 || 12 < x;
4040
x >= 8 || x <= 12;
41+
42+
// Fix #6315
43+
let y = 3.;
44+
(0. ..1.).contains(&y);
45+
!(0. ..=1.).contains(&y);
4146
}

tests/ui/range_contains.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ fn main() {
3838
x >= 8 || x >= 12;
3939
x < 12 || 12 < x;
4040
x >= 8 || x <= 12;
41+
42+
// Fix #6315
43+
let y = 3.;
44+
y >= 0. && y < 1.;
45+
y < 0. || y > 1.;
4146
}

tests/ui/range_contains.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,17 @@ error: manual `!RangeInclusive::contains` implementation
7272
LL | 999 < x || 1 > x;
7373
| ^^^^^^^^^^^^^^^^ help: use: `!(1..=999).contains(&x)`
7474

75-
error: aborting due to 12 previous errors
75+
error: manual `Range::contains` implementation
76+
--> $DIR/range_contains.rs:44:5
77+
|
78+
LL | y >= 0. && y < 1.;
79+
| ^^^^^^^^^^^^^^^^^ help: use: `(0. ..1.).contains(&y)`
80+
81+
error: manual `!RangeInclusive::contains` implementation
82+
--> $DIR/range_contains.rs:45:5
83+
|
84+
LL | y < 0. || y > 1.;
85+
| ^^^^^^^^^^^^^^^^ help: use: `!(0. ..=1.).contains(&y)`
86+
87+
error: aborting due to 14 previous errors
7688

0 commit comments

Comments
 (0)