Skip to content

Commit 1cdb06d

Browse files
committed
Auto merge of rust-lang#10001 - Jarcho:issue_9866, r=llogiq
Fix ICE in `unused_rounding` fixes rust-lang#9866 changelog: `unused_rounding`: Fix ICE when using the `_` separator
2 parents 846c9b8 + c1b8bc6 commit 1cdb06d

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

clippy_lints/src/unused_rounding.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet;
23
use rustc_ast::ast::{Expr, ExprKind, MethodCall};
34
use rustc_errors::Applicability;
45
use rustc_lint::{EarlyContext, EarlyLintPass};
@@ -29,30 +30,24 @@ declare_clippy_lint! {
2930
}
3031
declare_lint_pass!(UnusedRounding => [UNUSED_ROUNDING]);
3132

32-
fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> {
33+
fn is_useless_rounding<'a>(cx: &EarlyContext<'_>, expr: &'a Expr) -> Option<(&'a str, String)> {
3334
if let ExprKind::MethodCall(box MethodCall { seg:name_ident, receiver, .. }) = &expr.kind
3435
&& let method_name = name_ident.ident.name.as_str()
3536
&& (method_name == "ceil" || method_name == "round" || method_name == "floor")
3637
&& let ExprKind::Lit(token_lit) = &receiver.kind
37-
&& token_lit.is_semantic_float() {
38-
let mut f_str = token_lit.symbol.to_string();
39-
let f = f_str.trim_end_matches('_').parse::<f64>().unwrap();
40-
if let Some(suffix) = token_lit.suffix {
41-
f_str.push_str(suffix.as_str());
42-
}
43-
if f.fract() == 0.0 {
44-
Some((method_name, f_str))
45-
} else {
46-
None
47-
}
38+
&& token_lit.is_semantic_float()
39+
&& let Ok(f) = token_lit.symbol.as_str().replace('_', "").parse::<f64>() {
40+
(f.fract() == 0.0).then(||
41+
(method_name, snippet(cx, receiver.span, "..").to_string())
42+
)
4843
} else {
4944
None
5045
}
5146
}
5247

5348
impl EarlyLintPass for UnusedRounding {
5449
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
55-
if let Some((method_name, float)) = is_useless_rounding(expr) {
50+
if let Some((method_name, float)) = is_useless_rounding(cx, expr) {
5651
span_lint_and_sugg(
5752
cx,
5853
UNUSED_ROUNDING,

tests/ui/unused_rounding.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ fn main() {
1111
let _ = 3.3_f32.round();
1212
let _ = 3.3_f64.round();
1313
let _ = 3.0_f32;
14+
15+
let _ = 3_3.0_0_f32;
16+
let _ = 3_3.0_1_f64.round();
1417
}

tests/ui/unused_rounding.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ fn main() {
1111
let _ = 3.3_f32.round();
1212
let _ = 3.3_f64.round();
1313
let _ = 3.0_f32.round();
14+
15+
let _ = 3_3.0_0_f32.round();
16+
let _ = 3_3.0_1_f64.round();
1417
}

tests/ui/unused_rounding.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,11 @@ error: used the `round` method with a whole number float
2424
LL | let _ = 3.0_f32.round();
2525
| ^^^^^^^^^^^^^^^ help: remove the `round` method call: `3.0_f32`
2626

27-
error: aborting due to 4 previous errors
27+
error: used the `round` method with a whole number float
28+
--> $DIR/unused_rounding.rs:15:13
29+
|
30+
LL | let _ = 3_3.0_0_f32.round();
31+
| ^^^^^^^^^^^^^^^^^^^ help: remove the `round` method call: `3_3.0_0_f32`
32+
33+
error: aborting due to 5 previous errors
2834

0 commit comments

Comments
 (0)