Skip to content

Commit

Permalink
Change lint name to suboptimal_flops
Browse files Browse the repository at this point in the history
  • Loading branch information
krishna-veerareddy committed Feb 24, 2020
1 parent bc03f46 commit 6dacb1a
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -1349,6 +1349,7 @@ Released 2018-09-13
[`string_lit_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_lit_as_bytes
[`string_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_to_string
[`struct_excessive_bools`]: https://rust-lang.github.io/rust-clippy/master/index.html#struct_excessive_bools
[`suboptimal_flops`]: https://rust-lang.github.io/rust-clippy/master/index.html#suboptimal_flops
[`suspicious_arithmetic_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_arithmetic_impl
[`suspicious_assignment_formatting`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_assignment_formatting
[`suspicious_else_formatting`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_else_formatting
Expand Down
39 changes: 19 additions & 20 deletions clippy_lints/src/floating_point_arithmetic.rs
Expand Up @@ -4,10 +4,10 @@ use crate::consts::{
};
use crate::utils::*;
use if_chain::if_chain;
use rustc_hir::*;
use rustc_lint::{LateContext, LateLintPass};
use rustc::ty;
use rustc_errors::Applicability;
use rustc_hir::*;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use std::f32::consts as f32_consts;
use std::f64::consts as f64_consts;
Expand All @@ -16,11 +16,10 @@ use syntax::ast;

declare_clippy_lint! {
/// **What it does:** Looks for floating-point expressions that
/// can be expressed using built-in methods to improve accuracy,
/// performance and/or succinctness.
/// can be expressed using built-in methods to improve both
/// accuracy and performance.
///
/// **Why is this bad?** Negatively affects accuracy, performance
/// and/or readability.
/// **Why is this bad?** Negatively impacts accuracy and performance.
///
/// **Known problems:** None
///
Expand Down Expand Up @@ -59,16 +58,16 @@ declare_clippy_lint! {
/// let _ = a.exp_m1();
/// let _ = a.powi(2);
/// ```
pub FLOATING_POINT_IMPROVEMENTS,
pub SUBOPTIMAL_FLOPS,
nursery,
"looks for improvements to floating-point expressions"
"usage of sub-optimal floating point operations"
}

declare_lint_pass!(FloatingPointArithmetic => [FLOATING_POINT_IMPROVEMENTS]);
declare_lint_pass!(FloatingPointArithmetic => [SUBOPTIMAL_FLOPS]);

// Returns the specialized log method for a given base if base is constant
// and is one of 2, 10 and e
fn get_specialized_log_method(cx: &LateContext<'_, '_>, base: &Expr) -> Option<&'static str> {
fn get_specialized_log_method(cx: &LateContext<'_, '_>, base: &Expr<'_>) -> Option<&'static str> {
if let Some((value, _)) = constant(cx, cx.tables, base) {
if F32(2.0) == value || F64(2.0) == value {
return Some("log2");
Expand Down Expand Up @@ -124,7 +123,7 @@ fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>])
if let Some(method) = get_specialized_log_method(cx, &args[1]) {
span_lint_and_sugg(
cx,
FLOATING_POINT_IMPROVEMENTS,
SUBOPTIMAL_FLOPS,
expr.span,
"logarithm for bases 2, 10 and e can be computed more accurately",
"consider using",
Expand All @@ -136,7 +135,7 @@ fn check_log_base(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>])

// TODO: Lint expressions of the form `(x + y).ln()` where y > 1 and
// suggest usage of `(x + (y - 1)).ln_1p()` instead
fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
if_chain! {
if let ExprKind::Binary(op, ref lhs, ref rhs) = &args[0].kind;
if op.node == BinOpKind::Add;
Expand All @@ -149,7 +148,7 @@ fn check_ln1p(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {

span_lint_and_sugg(
cx,
FLOATING_POINT_IMPROVEMENTS,
SUBOPTIMAL_FLOPS,
expr.span,
"ln(1 + x) can be computed more accurately",
"consider using",
Expand Down Expand Up @@ -185,7 +184,7 @@ fn get_integer_from_float_constant(value: &Constant) -> Option<i64> {
}
}

fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
// Check receiver
if let Some((value, _)) = constant(cx, cx.tables, &args[0]) {
let method;
Expand All @@ -200,7 +199,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {

span_lint_and_sugg(
cx,
FLOATING_POINT_IMPROVEMENTS,
SUBOPTIMAL_FLOPS,
expr.span,
"exponent for bases 2 and e can be computed more accurately",
"consider using",
Expand All @@ -223,7 +222,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {
} else if let Some(exponent) = get_integer_from_float_constant(&value) {
span_lint_and_sugg(
cx,
FLOATING_POINT_IMPROVEMENTS,
SUBOPTIMAL_FLOPS,
expr.span,
"exponentiation with integer powers can be computed more efficiently",
"consider using",
Expand All @@ -238,7 +237,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {

span_lint_and_sugg(
cx,
FLOATING_POINT_IMPROVEMENTS,
SUBOPTIMAL_FLOPS,
expr.span,
help,
"consider using",
Expand All @@ -250,7 +249,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr, args: &HirVec<Expr>) {

// TODO: Lint expressions of the form `x.exp() - y` where y > 1
// and suggest usage of `x.exp_m1() - (y - 1)` instead
fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
if_chain! {
if let ExprKind::Binary(op, ref lhs, ref rhs) = expr.kind;
if op.node == BinOpKind::Sub;
Expand All @@ -263,7 +262,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
then {
span_lint_and_sugg(
cx,
FLOATING_POINT_IMPROVEMENTS,
SUBOPTIMAL_FLOPS,
expr.span,
"(e.pow(x) - 1) can be computed more accurately",
"consider using",
Expand All @@ -278,7 +277,7 @@ fn check_expm1(cx: &LateContext<'_, '_>, expr: &Expr) {
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
if let ExprKind::MethodCall(ref path, _, args) = &expr.kind {
let recv_ty = cx.tables.expr_ty(&args[0]);

Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/lib.rs
Expand Up @@ -538,6 +538,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&fallible_impl_from::FALLIBLE_IMPL_FROM,
&float_literal::EXCESSIVE_PRECISION,
&float_literal::LOSSY_FLOAT_LITERAL,
&floating_point_arithmetic::SUBOPTIMAL_FLOPS,
&format::USELESS_FORMAT,
&formatting::POSSIBLE_MISSING_COMMA,
&formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING,
Expand Down Expand Up @@ -1649,7 +1650,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
LintId::of(&attrs::EMPTY_LINE_AFTER_OUTER_ATTR),
LintId::of(&fallible_impl_from::FALLIBLE_IMPL_FROM),
LintId::of(&floating_point_arithmetic::FLOATING_POINT_IMPROVEMENTS),
LintId::of(&floating_point_arithmetic::SUBOPTIMAL_FLOPS),
LintId::of(&missing_const_for_fn::MISSING_CONST_FOR_FN),
LintId::of(&mul_add::MANUAL_MUL_ADD),
LintId::of(&mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL),
Expand Down
7 changes: 7 additions & 0 deletions src/lintlist/mod.rs
Expand Up @@ -1960,6 +1960,13 @@ pub const ALL_LINTS: [Lint; 357] = [
deprecation: None,
module: "excessive_bools",
},
Lint {
name: "suboptimal_flops",
group: "nursery",
desc: "usage of sub-optimal floating point operations",
deprecation: None,
module: "floating_point_arithmetic",
},
Lint {
name: "suspicious_arithmetic_impl",
group: "correctness",
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/floating_point_exp.rs
@@ -1,4 +1,4 @@
#![warn(clippy::floating_point_improvements)]
#![warn(clippy::suboptimal_flops)]

fn main() {
let x = 2f32;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/floating_point_exp.stderr
Expand Up @@ -4,7 +4,7 @@ error: (e.pow(x) - 1) can be computed more accurately
LL | let _ = x.exp() - 1.0;
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
|
= note: `-D clippy::floating-point-improvements` implied by `-D warnings`
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`

error: (e.pow(x) - 1) can be computed more accurately
--> $DIR/floating_point_exp.rs:6:13
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/floating_point_log.rs
@@ -1,5 +1,5 @@
#![allow(dead_code)]
#![warn(clippy::floating_point_improvements)]
#![warn(clippy::suboptimal_flops)]

const TWO: f32 = 2.0;
const E: f32 = std::f32::consts::E;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/floating_point_log.stderr
Expand Up @@ -4,7 +4,7 @@ error: logarithm for bases 2, 10 and e can be computed more accurately
LL | let _ = x.log(2f32);
| ^^^^^^^^^^^ help: consider using: `x.log2()`
|
= note: `-D clippy::floating-point-improvements` implied by `-D warnings`
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`

error: logarithm for bases 2, 10 and e can be computed more accurately
--> $DIR/floating_point_log.rs:10:13
Expand Down
6 changes: 1 addition & 5 deletions tests/ui/floating_point_powf.rs
@@ -1,4 +1,4 @@
#![warn(clippy::floating_point_improvements)]
#![warn(clippy::suboptimal_flops)]

fn main() {
let x = 3f32;
Expand All @@ -14,8 +14,6 @@ fn main() {
let _ = x.powf(-2.0);
let _ = x.powf(2.1);
let _ = x.powf(-2.1);
let _ = x.powf(16_777_217.0);
let _ = x.powf(-16_777_217.0);

let x = 3f64;
let _ = 2f64.powf(x);
Expand All @@ -30,6 +28,4 @@ fn main() {
let _ = x.powf(-2.0);
let _ = x.powf(2.1);
let _ = x.powf(-2.1);
let _ = x.powf(9_007_199_254_740_993.0);
let _ = x.powf(-9_007_199_254_740_993.0);
}
22 changes: 11 additions & 11 deletions tests/ui/floating_point_powf.stderr
Expand Up @@ -4,7 +4,7 @@ error: exponent for bases 2 and e can be computed more accurately
LL | let _ = 2f32.powf(x);
| ^^^^^^^^^^^^ help: consider using: `x.exp2()`
|
= note: `-D clippy::floating-point-improvements` implied by `-D warnings`
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`

error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:6:13
Expand Down Expand Up @@ -61,61 +61,61 @@ LL | let _ = x.powf(-2.0);
| ^^^^^^^^^^^^ help: consider using: `x.powi(-2)`

error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:21:13
--> $DIR/floating_point_powf.rs:19:13
|
LL | let _ = 2f64.powf(x);
| ^^^^^^^^^^^^ help: consider using: `x.exp2()`

error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:22:13
--> $DIR/floating_point_powf.rs:20:13
|
LL | let _ = 2f64.powf(3.1);
| ^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp2()`

error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:23:13
--> $DIR/floating_point_powf.rs:21:13
|
LL | let _ = 2f64.powf(-3.1);
| ^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp2()`

error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:24:13
--> $DIR/floating_point_powf.rs:22:13
|
LL | let _ = std::f64::consts::E.powf(x);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.exp()`

error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:25:13
--> $DIR/floating_point_powf.rs:23:13
|
LL | let _ = std::f64::consts::E.powf(3.1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp()`

error: exponent for bases 2 and e can be computed more accurately
--> $DIR/floating_point_powf.rs:26:13
--> $DIR/floating_point_powf.rs:24:13
|
LL | let _ = std::f64::consts::E.powf(-3.1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp()`

error: square-root of a number can be computed more efficiently and accurately
--> $DIR/floating_point_powf.rs:27:13
--> $DIR/floating_point_powf.rs:25:13
|
LL | let _ = x.powf(1.0 / 2.0);
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.sqrt()`

error: cube-root of a number can be computed more accurately
--> $DIR/floating_point_powf.rs:28:13
--> $DIR/floating_point_powf.rs:26:13
|
LL | let _ = x.powf(1.0 / 3.0);
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.cbrt()`

error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:29:13
--> $DIR/floating_point_powf.rs:27:13
|
LL | let _ = x.powf(2.0);
| ^^^^^^^^^^^ help: consider using: `x.powi(2)`

error: exponentiation with integer powers can be computed more efficiently
--> $DIR/floating_point_powf.rs:30:13
--> $DIR/floating_point_powf.rs:28:13
|
LL | let _ = x.powf(-2.0);
| ^^^^^^^^^^^^ help: consider using: `x.powi(-2)`
Expand Down

0 comments on commit 6dacb1a

Please sign in to comment.