Skip to content

Commit

Permalink
Dont suggest suboptimal_flops unavailable in nostd
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslueg authored and ofeeg committed May 15, 2023
1 parent a321f3d commit 0a73883
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
13 changes: 10 additions & 3 deletions clippy_lints/src/floating_point_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use clippy_utils::consts::{
constant, constant_simple, Constant,
Constant::{Int, F32, F64},
};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::higher;
use clippy_utils::{eq_expr_value, get_parent_expr, in_constant, numeric_literal, peel_blocks, sugg};
use clippy_utils::{
diagnostics::span_lint_and_sugg, eq_expr_value, get_parent_expr, higher, in_constant, is_no_std_crate,
numeric_literal, peel_blocks, sugg,
};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
Expand Down Expand Up @@ -452,6 +453,9 @@ fn is_float_mul_expr<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(&'

// TODO: Fix rust-lang/rust-clippy#4735
fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
if is_no_std_crate(cx) {
return; // The suggested methods are not available in core
}
if let ExprKind::Binary(
Spanned {
node: op @ (BinOpKind::Add | BinOpKind::Sub),
Expand Down Expand Up @@ -566,6 +570,9 @@ fn are_negated<'a>(cx: &LateContext<'_>, expr1: &'a Expr<'a>, expr2: &'a Expr<'a
}

fn check_custom_abs(cx: &LateContext<'_>, expr: &Expr<'_>) {
if is_no_std_crate(cx) {
return; // The suggested methods are not available in core
}
if_chain! {
if let Some(higher::If { cond, then, r#else: Some(r#else) }) = higher::If::hir(expr);
let if_body_expr = peel_blocks(then);
Expand Down
31 changes: 31 additions & 0 deletions tests/ui/floating_point_arithmetic_nostd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#![feature(lang_items, start)]
#![warn(clippy::imprecise_flops)]
#![warn(clippy::suboptimal_flops)]
#![no_std]

// The following should not lint, as the suggested methods {f32,f64}.mul_add()
// and {f32,f64}::abs() are not available in no_std

pub fn mul_add() {
let a: f64 = 1234.567;
let b: f64 = 45.67834;
let c: f64 = 0.0004;
let _ = a * b + c;
}

fn fake_abs1(num: f64) -> f64 {
if num >= 0.0 { num } else { -num }
}

#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
0
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

0 comments on commit 0a73883

Please sign in to comment.