Skip to content

Commit 06fa045

Browse files
Rename lint into confusing_method_to_numeric_cast
1 parent c680419 commit 06fa045

10 files changed

+172
-44
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5594,6 +5594,7 @@ Released 2018-09-13
55945594
[`collection_is_never_read`]: https://rust-lang.github.io/rust-clippy/master/index.html#collection_is_never_read
55955595
[`comparison_chain`]: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain
55965596
[`comparison_to_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_to_empty
5597+
[`confusing_method_to_numeric_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#confusing_method_to_numeric_cast
55975598
[`const_is_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#const_is_empty
55985599
[`const_static_lifetime`]: https://rust-lang.github.io/rust-clippy/master/index.html#const_static_lifetime
55995600
[`copy_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#copy_iterator
@@ -6068,7 +6069,6 @@ Released 2018-09-13
60686069
[`possible_missing_comma`]: https://rust-lang.github.io/rust-clippy/master/index.html#possible_missing_comma
60696070
[`precedence`]: https://rust-lang.github.io/rust-clippy/master/index.html#precedence
60706071
[`precedence_bits`]: https://rust-lang.github.io/rust-clippy/master/index.html#precedence_bits
6071-
[`primitive_method_to_numeric_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#primitive_method_to_numeric_cast
60726072
[`print_in_format_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_in_format_impl
60736073
[`print_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_literal
60746074
[`print_stderr`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_stderr

clippy_lints/src/casts/primitive_method_to_numeric_cast.rs renamed to clippy_lints/src/casts/confusing_method_to_numeric_cast.rs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::match_def_path;
32
use clippy_utils::source::snippet_with_applicability;
43
use rustc_errors::Applicability;
54
use rustc_hir::Expr;
65
use rustc_lint::LateContext;
7-
use rustc_middle::ty::{self, Ty};
6+
use rustc_middle::ty::{self, GenericArg, Ty};
7+
use rustc_span::def_id::DefId;
8+
use rustc_span::{Symbol, sym};
89

9-
use super::PRIMITIVE_METHOD_TO_NUMERIC_CAST;
10+
use super::CONFUSING_METHOD_TO_NUMERIC_CAST;
1011

1112
fn get_primitive_ty_name(ty: Ty<'_>) -> Option<&'static str> {
1213
match ty.kind() {
@@ -18,6 +19,37 @@ fn get_primitive_ty_name(ty: Ty<'_>) -> Option<&'static str> {
1819
}
1920
}
2021

22+
fn get_const_name_and_ty_name(
23+
cx: &LateContext<'_>,
24+
method_name: Symbol,
25+
method_def_id: DefId,
26+
generics: &[GenericArg<'_>],
27+
) -> Option<(&'static str, &'static str)> {
28+
let method_name = method_name.as_str();
29+
let diagnostic_name = cx.tcx.get_diagnostic_name(method_def_id);
30+
31+
let ty_name = if diagnostic_name.is_some_and(|diag| diag == sym::cmp_ord_min || diag == sym::cmp_ord_max) {
32+
// We get the type on which the `min`/`max` method of the `Ord` trait is implemented.
33+
if let [ty] = generics
34+
&& let Some(ty) = ty.as_type()
35+
{
36+
get_primitive_ty_name(ty)?
37+
} else {
38+
return None;
39+
}
40+
} else if let Some(impl_id) = cx.tcx.impl_of_method(method_def_id)
41+
&& let Some(ty_name) = get_primitive_ty_name(cx.tcx.type_of(impl_id).instantiate_identity())
42+
&& ["min", "max", "minimum", "maximum", "min_value", "max_value"].contains(&method_name)
43+
{
44+
ty_name
45+
} else {
46+
return None;
47+
};
48+
49+
let const_name = if method_name.starts_with("max") { "MAX" } else { "MIN" };
50+
Some((const_name, ty_name))
51+
}
52+
2153
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
2254
// We allow casts from any function type to any function type.
2355
match cast_to.kind() {
@@ -27,28 +59,21 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
2759

2860
if let ty::FnDef(def_id, generics) = cast_from.kind()
2961
&& let Some(method_name) = cx.tcx.opt_item_name(*def_id)
30-
&& let method_name = method_name.as_str()
31-
&& (method_name == "min" || method_name == "max")
32-
// We get the type on which the `min`/`max` method of the `Ord` trait is implemented.
33-
&& let [ty] = generics.as_slice()
34-
&& let Some(ty) = ty.as_type()
35-
// We get its name in case it's a primitive with an associated MIN/MAX constant.
36-
&& let Some(ty_name) = get_primitive_ty_name(ty)
37-
&& match_def_path(cx, *def_id, &["core", "cmp", "Ord", method_name])
62+
&& let Some((const_name, ty_name)) = get_const_name_and_ty_name(cx, method_name, *def_id, generics.as_slice())
3863
{
3964
let mut applicability = Applicability::MaybeIncorrect;
4065
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "..", &mut applicability);
4166

4267
span_lint_and_then(
4368
cx,
44-
PRIMITIVE_METHOD_TO_NUMERIC_CAST,
69+
CONFUSING_METHOD_TO_NUMERIC_CAST,
4570
expr.span,
4671
format!("casting function pointer `{from_snippet}` to `{cast_to}`"),
4772
|diag| {
4873
diag.span_suggestion_verbose(
4974
expr.span,
5075
"did you mean to use the associated constant?",
51-
format!("{ty_name}::{} as {cast_to}", method_name.to_ascii_uppercase()),
76+
format!("{ty_name}::{const_name} as {cast_to}"),
5277
applicability,
5378
);
5479
},

clippy_lints/src/casts/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ mod cast_sign_loss;
1414
mod cast_slice_different_sizes;
1515
mod cast_slice_from_raw_parts;
1616
mod char_lit_as_u8;
17+
mod confusing_method_to_numeric_cast;
1718
mod fn_to_numeric_cast;
1819
mod fn_to_numeric_cast_any;
1920
mod fn_to_numeric_cast_with_truncation;
2021
mod manual_dangling_ptr;
21-
mod primitive_method_to_numeric_cast;
2222
mod ptr_as_ptr;
2323
mod ptr_cast_constness;
2424
mod ref_as_ptr;
@@ -808,7 +808,7 @@ declare_clippy_lint! {
808808
/// let _ = u16::MAX as usize;
809809
/// ```
810810
#[clippy::version = "1.86.0"]
811-
pub PRIMITIVE_METHOD_TO_NUMERIC_CAST,
811+
pub CONFUSING_METHOD_TO_NUMERIC_CAST,
812812
suspicious,
813813
"casting a primitive method pointer to any integer type"
814814
}
@@ -850,7 +850,7 @@ impl_lint_pass!(Casts => [
850850
REF_AS_PTR,
851851
AS_POINTER_UNDERSCORE,
852852
MANUAL_DANGLING_PTR,
853-
PRIMITIVE_METHOD_TO_NUMERIC_CAST,
853+
CONFUSING_METHOD_TO_NUMERIC_CAST,
854854
]);
855855

856856
impl<'tcx> LateLintPass<'tcx> for Casts {
@@ -875,7 +875,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
875875
ptr_cast_constness::check(cx, expr, cast_from_expr, cast_from, cast_to, self.msrv);
876876
as_ptr_cast_mut::check(cx, expr, cast_from_expr, cast_to);
877877
fn_to_numeric_cast_any::check(cx, expr, cast_from_expr, cast_from, cast_to);
878-
primitive_method_to_numeric_cast::check(cx, expr, cast_from_expr, cast_from, cast_to);
878+
confusing_method_to_numeric_cast::check(cx, expr, cast_from_expr, cast_from, cast_to);
879879
fn_to_numeric_cast::check(cx, expr, cast_from_expr, cast_from, cast_to);
880880
fn_to_numeric_cast_with_truncation::check(cx, expr, cast_from_expr, cast_from, cast_to);
881881
zero_ptr::check(cx, expr, cast_from_expr, cast_to_hir);

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ pub static LINTS: &[&crate::LintInfo] = &[
6464
crate::casts::CAST_SLICE_DIFFERENT_SIZES_INFO,
6565
crate::casts::CAST_SLICE_FROM_RAW_PARTS_INFO,
6666
crate::casts::CHAR_LIT_AS_U8_INFO,
67+
crate::casts::CONFUSING_METHOD_TO_NUMERIC_CAST_INFO,
6768
crate::casts::FN_TO_NUMERIC_CAST_INFO,
6869
crate::casts::FN_TO_NUMERIC_CAST_ANY_INFO,
6970
crate::casts::FN_TO_NUMERIC_CAST_WITH_TRUNCATION_INFO,
7071
crate::casts::MANUAL_DANGLING_PTR_INFO,
71-
crate::casts::PRIMITIVE_METHOD_TO_NUMERIC_CAST_INFO,
7272
crate::casts::PTR_AS_PTR_INFO,
7373
crate::casts::PTR_CAST_CONSTNESS_INFO,
7474
crate::casts::REF_AS_PTR_INFO,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(float_minimum_maximum)]
2+
#![warn(clippy::confusing_method_to_numeric_cast)]
3+
4+
fn main() {
5+
let _ = u16::MAX as usize; //~ confusing_method_to_numeric_cast
6+
let _ = u16::MIN as usize; //~ confusing_method_to_numeric_cast
7+
let _ = u16::MAX as usize; //~ confusing_method_to_numeric_cast
8+
let _ = u16::MIN as usize; //~ confusing_method_to_numeric_cast
9+
10+
let _ = f32::MAX as usize; //~ confusing_method_to_numeric_cast
11+
let _ = f32::MAX as usize; //~ confusing_method_to_numeric_cast
12+
let _ = f32::MIN as usize; //~ confusing_method_to_numeric_cast
13+
let _ = f32::MIN as usize; //~ confusing_method_to_numeric_cast
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(float_minimum_maximum)]
2+
#![warn(clippy::confusing_method_to_numeric_cast)]
3+
4+
fn main() {
5+
let _ = u16::max as usize; //~ confusing_method_to_numeric_cast
6+
let _ = u16::min as usize; //~ confusing_method_to_numeric_cast
7+
let _ = u16::max_value as usize; //~ confusing_method_to_numeric_cast
8+
let _ = u16::min_value as usize; //~ confusing_method_to_numeric_cast
9+
10+
let _ = f32::maximum as usize; //~ confusing_method_to_numeric_cast
11+
let _ = f32::max as usize; //~ confusing_method_to_numeric_cast
12+
let _ = f32::minimum as usize; //~ confusing_method_to_numeric_cast
13+
let _ = f32::min as usize; //~ confusing_method_to_numeric_cast
14+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
error: casting function pointer `u16::max` to `usize`
2+
--> tests/ui/confusing_method_to_numeric_cast.rs:5:13
3+
|
4+
LL | let _ = u16::max as usize;
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::confusing-method-to-numeric-cast` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::confusing_method_to_numeric_cast)]`
9+
help: did you mean to use the associated constant?
10+
|
11+
LL - let _ = u16::max as usize;
12+
LL + let _ = u16::MAX as usize;
13+
|
14+
15+
error: casting function pointer `u16::min` to `usize`
16+
--> tests/ui/confusing_method_to_numeric_cast.rs:6:13
17+
|
18+
LL | let _ = u16::min as usize;
19+
| ^^^^^^^^^^^^^^^^^
20+
|
21+
help: did you mean to use the associated constant?
22+
|
23+
LL - let _ = u16::min as usize;
24+
LL + let _ = u16::MIN as usize;
25+
|
26+
27+
error: casting function pointer `u16::max_value` to `usize`
28+
--> tests/ui/confusing_method_to_numeric_cast.rs:7:13
29+
|
30+
LL | let _ = u16::max_value as usize;
31+
| ^^^^^^^^^^^^^^^^^^^^^^^
32+
|
33+
help: did you mean to use the associated constant?
34+
|
35+
LL - let _ = u16::max_value as usize;
36+
LL + let _ = u16::MAX as usize;
37+
|
38+
39+
error: casting function pointer `u16::min_value` to `usize`
40+
--> tests/ui/confusing_method_to_numeric_cast.rs:8:13
41+
|
42+
LL | let _ = u16::min_value as usize;
43+
| ^^^^^^^^^^^^^^^^^^^^^^^
44+
|
45+
help: did you mean to use the associated constant?
46+
|
47+
LL - let _ = u16::min_value as usize;
48+
LL + let _ = u16::MIN as usize;
49+
|
50+
51+
error: casting function pointer `f32::maximum` to `usize`
52+
--> tests/ui/confusing_method_to_numeric_cast.rs:10:13
53+
|
54+
LL | let _ = f32::maximum as usize;
55+
| ^^^^^^^^^^^^^^^^^^^^^
56+
|
57+
help: did you mean to use the associated constant?
58+
|
59+
LL - let _ = f32::maximum as usize;
60+
LL + let _ = f32::MAX as usize;
61+
|
62+
63+
error: casting function pointer `f32::max` to `usize`
64+
--> tests/ui/confusing_method_to_numeric_cast.rs:11:13
65+
|
66+
LL | let _ = f32::max as usize;
67+
| ^^^^^^^^^^^^^^^^^
68+
|
69+
help: did you mean to use the associated constant?
70+
|
71+
LL - let _ = f32::max as usize;
72+
LL + let _ = f32::MAX as usize;
73+
|
74+
75+
error: casting function pointer `f32::minimum` to `usize`
76+
--> tests/ui/confusing_method_to_numeric_cast.rs:12:13
77+
|
78+
LL | let _ = f32::minimum as usize;
79+
| ^^^^^^^^^^^^^^^^^^^^^
80+
|
81+
help: did you mean to use the associated constant?
82+
|
83+
LL - let _ = f32::minimum as usize;
84+
LL + let _ = f32::MIN as usize;
85+
|
86+
87+
error: casting function pointer `f32::min` to `usize`
88+
--> tests/ui/confusing_method_to_numeric_cast.rs:13:13
89+
|
90+
LL | let _ = f32::min as usize;
91+
| ^^^^^^^^^^^^^^^^^
92+
|
93+
help: did you mean to use the associated constant?
94+
|
95+
LL - let _ = f32::min as usize;
96+
LL + let _ = f32::MIN as usize;
97+
|
98+
99+
error: aborting due to 8 previous errors
100+

tests/ui/primitive_method_to_numeric_cast.fixed

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/ui/primitive_method_to_numeric_cast.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/ui/primitive_method_to_numeric_cast.stderr

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)