forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#8866 - botahamec:unused-rounding, r=llogiq
Add new lint `[unused_rounding]` fixes rust-lang#39 changelog: added a ``[`unused_rounding`]`` lint to check for the rounding of whole-number literals
- Loading branch information
Showing
8 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use rustc_ast::ast::{Expr, ExprKind, LitFloatType, LitKind}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// | ||
/// Detects cases where a whole-number literal float is being rounded, using | ||
/// the `floor`, `ceil`, or `round` methods. | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// This is unnecessary and confusing to the reader. Doing this is probably a mistake. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// let x = 1f32.ceil(); | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// let x = 1f32; | ||
/// ``` | ||
#[clippy::version = "1.62.0"] | ||
pub UNUSED_ROUNDING, | ||
nursery, | ||
"Uselessly rounding a whole number floating-point literal" | ||
} | ||
declare_lint_pass!(UnusedRounding => [UNUSED_ROUNDING]); | ||
|
||
fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> { | ||
if let ExprKind::MethodCall(name_ident, args, _) = &expr.kind | ||
&& let method_name = name_ident.ident.name.as_str() | ||
&& (method_name == "ceil" || method_name == "round" || method_name == "floor") | ||
&& !args.is_empty() | ||
&& let ExprKind::Lit(spanned) = &args[0].kind | ||
&& let LitKind::Float(symbol, ty) = spanned.kind { | ||
let f = symbol.as_str().parse::<f64>().unwrap(); | ||
let f_str = symbol.to_string() + if let LitFloatType::Suffixed(ty) = ty { | ||
ty.name_str() | ||
} else { | ||
"" | ||
}; | ||
if f.fract() == 0.0 { | ||
Some((method_name, f_str)) | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
impl EarlyLintPass for UnusedRounding { | ||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { | ||
if let Some((method_name, float)) = is_useless_rounding(expr) { | ||
span_lint_and_sugg( | ||
cx, | ||
UNUSED_ROUNDING, | ||
expr.span, | ||
&format!("used the `{}` method with a whole number float", method_name), | ||
&format!("remove the `{}` method call", method_name), | ||
float, | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// run-rustfix | ||
#![warn(clippy::unused_rounding)] | ||
|
||
fn main() { | ||
let _ = 1f32; | ||
let _ = 1.0f64; | ||
let _ = 1.00f32; | ||
let _ = 2e-54f64.floor(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// run-rustfix | ||
#![warn(clippy::unused_rounding)] | ||
|
||
fn main() { | ||
let _ = 1f32.ceil(); | ||
let _ = 1.0f64.floor(); | ||
let _ = 1.00f32.round(); | ||
let _ = 2e-54f64.floor(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: used the `ceil` method with a whole number float | ||
--> $DIR/unused_rounding.rs:5:13 | ||
| | ||
LL | let _ = 1f32.ceil(); | ||
| ^^^^^^^^^^^ help: remove the `ceil` method call: `1f32` | ||
| | ||
= note: `-D clippy::unused-rounding` implied by `-D warnings` | ||
|
||
error: used the `floor` method with a whole number float | ||
--> $DIR/unused_rounding.rs:6:13 | ||
| | ||
LL | let _ = 1.0f64.floor(); | ||
| ^^^^^^^^^^^^^^ help: remove the `floor` method call: `1.0f64` | ||
|
||
error: used the `round` method with a whole number float | ||
--> $DIR/unused_rounding.rs:7:13 | ||
| | ||
LL | let _ = 1.00f32.round(); | ||
| ^^^^^^^^^^^^^^^ help: remove the `round` method call: `1.00f32` | ||
|
||
error: aborting due to 3 previous errors | ||
|