Skip to content

Commit 708ef79

Browse files
committed
only run flop lints on inherent method calls
1 parent 4f3180a commit 708ef79

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use clippy_utils::consts::Constant::{Int, F32, F64};
22
use clippy_utils::consts::{constant, constant_simple, Constant};
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::{
5-
eq_expr_value, get_parent_expr, higher, in_constant, is_no_std_crate, numeric_literal, peel_blocks, sugg,
5+
eq_expr_value, get_parent_expr, higher, in_constant, is_inherent_method_call, is_no_std_crate, numeric_literal,
6+
peel_blocks, sugg,
67
};
78
use rustc_errors::Applicability;
89
use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
@@ -759,7 +760,7 @@ impl<'tcx> LateLintPass<'tcx> for FloatingPointArithmetic {
759760
if let ExprKind::MethodCall(path, receiver, args, _) = &expr.kind {
760761
let recv_ty = cx.typeck_results().expr_ty(receiver);
761762

762-
if recv_ty.is_floating_point() && !is_no_std_crate(cx) {
763+
if recv_ty.is_floating_point() && !is_no_std_crate(cx) && is_inherent_method_call(cx, expr) {
763764
match path.ident.name.as_str() {
764765
"ln" => check_ln1p(cx, expr, receiver),
765766
"log" => check_log_base(cx, expr, receiver, args),

clippy_utils/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,15 @@ pub fn match_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, path: &[&str])
321321
.map_or(false, |trt_id| match_def_path(cx, trt_id, path))
322322
}
323323

324+
/// Checks if the given method call expression calls an inherent method.
325+
pub fn is_inherent_method_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
326+
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) {
327+
cx.tcx.trait_of_item(method_id).is_none()
328+
} else {
329+
false
330+
}
331+
}
332+
324333
/// Checks if a method is defined in an impl of a diagnostic item
325334
pub fn is_diag_item_method(cx: &LateContext<'_>, def_id: DefId, diag_item: Symbol) -> bool {
326335
if let Some(impl_did) = cx.tcx.impl_of_method(def_id) {

tests/ui/floating_point_log.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,19 @@ fn check_ln1p() {
5555
let _ = (1.0 + x - 2.0).ln();
5656
}
5757

58+
fn issue12881() {
59+
pub trait MyLog {
60+
fn log(&self) -> Self;
61+
}
62+
63+
impl MyLog for f32 {
64+
fn log(&self) -> Self {
65+
4.
66+
}
67+
}
68+
69+
let x = 2.0;
70+
x.log();
71+
}
72+
5873
fn main() {}

tests/ui/floating_point_log.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,19 @@ fn check_ln1p() {
5555
let _ = (1.0 + x - 2.0).ln();
5656
}
5757

58+
fn issue12881() {
59+
pub trait MyLog {
60+
fn log(&self) -> Self;
61+
}
62+
63+
impl MyLog for f32 {
64+
fn log(&self) -> Self {
65+
4.
66+
}
67+
}
68+
69+
let x = 2.0;
70+
x.log();
71+
}
72+
5873
fn main() {}

0 commit comments

Comments
 (0)