Auto merge of #10670 - lukaslueg:issue10634, r=Jarcho

Don't suggest `suboptimal_flops` unavailable in nostd

Fixes #10634

changelog: Enhancement: [`suboptimal_flops`]: Do not suggest `{f32,f64}::abs()` or `{f32,f64}::mul_add()` in a `no_std`-environment.
This commit is contained in:
bors 2023-04-23 13:23:48 +00:00
commit 316d83a4d8
2 changed files with 41 additions and 3 deletions

View File

@ -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};
@ -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),
@ -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);

View File

@ -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() {}