only run flop lints on inherent method calls

This commit is contained in:
y21 2024-06-03 21:31:33 +02:00
parent 4f3180adac
commit 708ef7955d
4 changed files with 42 additions and 2 deletions

View File

@ -2,7 +2,8 @@
use clippy_utils::consts::{constant, constant_simple, Constant};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::{
eq_expr_value, get_parent_expr, higher, in_constant, is_no_std_crate, numeric_literal, peel_blocks, sugg,
eq_expr_value, get_parent_expr, higher, in_constant, is_inherent_method_call, is_no_std_crate, numeric_literal,
peel_blocks, sugg,
};
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
@ -759,7 +760,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if let ExprKind::MethodCall(path, receiver, args, _) = &expr.kind {
let recv_ty = cx.typeck_results().expr_ty(receiver);
if recv_ty.is_floating_point() && !is_no_std_crate(cx) {
if recv_ty.is_floating_point() && !is_no_std_crate(cx) && is_inherent_method_call(cx, expr) {
match path.ident.name.as_str() {
"ln" => check_ln1p(cx, expr, receiver),
"log" => check_log_base(cx, expr, receiver, args),

View File

@ -321,6 +321,15 @@ pub fn match_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, path: &[&str])
.map_or(false, |trt_id| match_def_path(cx, trt_id, path))
}
/// Checks if the given method call expression calls an inherent method.
pub fn is_inherent_method_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) {
cx.tcx.trait_of_item(method_id).is_none()
} else {
false
}
}
/// Checks if a method is defined in an impl of a diagnostic item
pub fn is_diag_item_method(cx: &LateContext<'_>, def_id: DefId, diag_item: Symbol) -> bool {
if let Some(impl_did) = cx.tcx.impl_of_method(def_id) {

View File

@ -55,4 +55,19 @@ fn check_ln1p() {
let _ = (1.0 + x - 2.0).ln();
}
fn issue12881() {
pub trait MyLog {
fn log(&self) -> Self;
}
impl MyLog for f32 {
fn log(&self) -> Self {
4.
}
}
let x = 2.0;
x.log();
}
fn main() {}

View File

@ -55,4 +55,19 @@ fn check_ln1p() {
let _ = (1.0 + x - 2.0).ln();
}
fn issue12881() {
pub trait MyLog {
fn log(&self) -> Self;
}
impl MyLog for f32 {
fn log(&self) -> Self {
4.
}
}
let x = 2.0;
x.log();
}
fn main() {}