Skip to content

Commit a85c8f3

Browse files
committed
Auto merge of #9870 - koka831:unformat-unused-rounding, r=Jarcho
Keep original literal notation in suggestion While I did some investigation of #9866 (I couldn't reproduce it though) I found that `unused_rounding` formats as follows: ```rust 3.0_f64.round() // => 3.0f64 ``` This PR makes them preserve as the original notation. ```rust 3.0_f64.round() // => 3.0_f64 ``` changelog: Suggestion Enhancement: [`unused_rounding`]: The suggestion now preserves the original float literal notation
2 parents d445ced + 1baa6cd commit a85c8f3

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

clippy_lints/src/unused_rounding.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use rustc_ast::ast::{Expr, ExprKind, LitFloatType, LitKind};
1+
use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet};
2+
use rustc_ast::ast::{Expr, ExprKind, LitKind};
33
use rustc_errors::Applicability;
44
use rustc_lint::{EarlyContext, EarlyLintPass};
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -29,19 +29,15 @@ declare_clippy_lint! {
2929
}
3030
declare_lint_pass!(UnusedRounding => [UNUSED_ROUNDING]);
3131

32-
fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> {
32+
fn is_useless_rounding<'a>(cx: &EarlyContext<'a>, expr: &'a Expr) -> Option<(&'a str, String)> {
3333
if let ExprKind::MethodCall(name_ident, receiver, _, _) = &expr.kind
3434
&& let method_name = name_ident.ident.name.as_str()
3535
&& (method_name == "ceil" || method_name == "round" || method_name == "floor")
3636
&& let ExprKind::Lit(spanned) = &receiver.kind
37-
&& let LitKind::Float(symbol, ty) = spanned.kind {
37+
&& let LitKind::Float(symbol, _) = spanned.kind {
3838
let f = symbol.as_str().parse::<f64>().unwrap();
39-
let f_str = symbol.to_string() + if let LitFloatType::Suffixed(ty) = ty {
40-
ty.name_str()
41-
} else {
42-
""
43-
};
4439
if f.fract() == 0.0 {
40+
let f_str = snippet(cx, receiver.span, "..").to_string();
4541
Some((method_name, f_str))
4642
} else {
4743
None
@@ -53,7 +49,7 @@ fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> {
5349

5450
impl EarlyLintPass for UnusedRounding {
5551
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
56-
if let Some((method_name, float)) = is_useless_rounding(expr) {
52+
if let Some((method_name, float)) = is_useless_rounding(cx, expr) {
5753
span_lint_and_sugg(
5854
cx,
5955
UNUSED_ROUNDING,

tests/ui/unused_rounding.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ fn main() {
66
let _ = 1.0f64;
77
let _ = 1.00f32;
88
let _ = 2e-54f64.floor();
9+
10+
// issue9866
11+
let _ = 3.3_f32.round();
12+
let _ = 3.3_f64.round();
13+
let _ = 3.0_f32;
914
}

tests/ui/unused_rounding.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ fn main() {
66
let _ = 1.0f64.floor();
77
let _ = 1.00f32.round();
88
let _ = 2e-54f64.floor();
9+
10+
// issue9866
11+
let _ = 3.3_f32.round();
12+
let _ = 3.3_f64.round();
13+
let _ = 3.0_f32.round();
914
}

tests/ui/unused_rounding.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@ error: used the `round` method with a whole number float
1818
LL | let _ = 1.00f32.round();
1919
| ^^^^^^^^^^^^^^^ help: remove the `round` method call: `1.00f32`
2020

21-
error: aborting due to 3 previous errors
21+
error: used the `round` method with a whole number float
22+
--> $DIR/unused_rounding.rs:13:13
23+
|
24+
LL | let _ = 3.0_f32.round();
25+
| ^^^^^^^^^^^^^^^ help: remove the `round` method call: `3.0_f32`
26+
27+
error: aborting due to 4 previous errors
2228

0 commit comments

Comments
 (0)