Fix ICE in unused_rounding
This commit is contained in:
parent
78589ffad2
commit
c1b8bc66e9
@ -1,4 +1,5 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
|
use clippy_utils::source::snippet;
|
||||||
use rustc_ast::ast::{Expr, ExprKind, MethodCall};
|
use rustc_ast::ast::{Expr, ExprKind, MethodCall};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass};
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||||
@ -29,22 +30,16 @@ declare_clippy_lint! {
|
|||||||
}
|
}
|
||||||
declare_lint_pass!(UnusedRounding => [UNUSED_ROUNDING]);
|
declare_lint_pass!(UnusedRounding => [UNUSED_ROUNDING]);
|
||||||
|
|
||||||
fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> {
|
fn is_useless_rounding<'a>(cx: &EarlyContext<'_>, expr: &'a Expr) -> Option<(&'a str, String)> {
|
||||||
if let ExprKind::MethodCall(box MethodCall { seg:name_ident, receiver, .. }) = &expr.kind
|
if let ExprKind::MethodCall(box MethodCall { seg:name_ident, receiver, .. }) = &expr.kind
|
||||||
&& let method_name = name_ident.ident.name.as_str()
|
&& let method_name = name_ident.ident.name.as_str()
|
||||||
&& (method_name == "ceil" || method_name == "round" || method_name == "floor")
|
&& (method_name == "ceil" || method_name == "round" || method_name == "floor")
|
||||||
&& let ExprKind::Lit(token_lit) = &receiver.kind
|
&& let ExprKind::Lit(token_lit) = &receiver.kind
|
||||||
&& token_lit.is_semantic_float() {
|
&& token_lit.is_semantic_float()
|
||||||
let mut f_str = token_lit.symbol.to_string();
|
&& let Ok(f) = token_lit.symbol.as_str().replace('_', "").parse::<f64>() {
|
||||||
let f = f_str.trim_end_matches('_').parse::<f64>().unwrap();
|
(f.fract() == 0.0).then(||
|
||||||
if let Some(suffix) = token_lit.suffix {
|
(method_name, snippet(cx, receiver.span, "..").to_string())
|
||||||
f_str.push_str(suffix.as_str());
|
)
|
||||||
}
|
|
||||||
if f.fract() == 0.0 {
|
|
||||||
Some((method_name, f_str))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@ -52,7 +47,7 @@ fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> {
|
|||||||
|
|
||||||
impl EarlyLintPass for UnusedRounding {
|
impl EarlyLintPass for UnusedRounding {
|
||||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
||||||
if let Some((method_name, float)) = is_useless_rounding(expr) {
|
if let Some((method_name, float)) = is_useless_rounding(cx, expr) {
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
UNUSED_ROUNDING,
|
UNUSED_ROUNDING,
|
||||||
|
@ -11,4 +11,7 @@ fn main() {
|
|||||||
let _ = 3.3_f32.round();
|
let _ = 3.3_f32.round();
|
||||||
let _ = 3.3_f64.round();
|
let _ = 3.3_f64.round();
|
||||||
let _ = 3.0_f32;
|
let _ = 3.0_f32;
|
||||||
|
|
||||||
|
let _ = 3_3.0_0_f32;
|
||||||
|
let _ = 3_3.0_1_f64.round();
|
||||||
}
|
}
|
||||||
|
@ -11,4 +11,7 @@ fn main() {
|
|||||||
let _ = 3.3_f32.round();
|
let _ = 3.3_f32.round();
|
||||||
let _ = 3.3_f64.round();
|
let _ = 3.3_f64.round();
|
||||||
let _ = 3.0_f32.round();
|
let _ = 3.0_f32.round();
|
||||||
|
|
||||||
|
let _ = 3_3.0_0_f32.round();
|
||||||
|
let _ = 3_3.0_1_f64.round();
|
||||||
}
|
}
|
||||||
|
@ -24,5 +24,11 @@ error: used the `round` method with a whole number float
|
|||||||
LL | let _ = 3.0_f32.round();
|
LL | let _ = 3.0_f32.round();
|
||||||
| ^^^^^^^^^^^^^^^ help: remove the `round` method call: `3.0_f32`
|
| ^^^^^^^^^^^^^^^ help: remove the `round` method call: `3.0_f32`
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: used the `round` method with a whole number float
|
||||||
|
--> $DIR/unused_rounding.rs:15:13
|
||||||
|
|
|
||||||
|
LL | let _ = 3_3.0_0_f32.round();
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^ help: remove the `round` method call: `3_3.0_0_f32`
|
||||||
|
|
||||||
|
error: aborting due to 5 previous errors
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user