about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDan Johnson <ComputerDruid@gmail.com>2025-06-17 17:21:44 -0700
committerDan Johnson <ComputerDruid@gmail.com>2025-07-10 11:56:23 -0700
commite30313cc6b140e0a7f88e85bf9ec9440bfb4c82e (patch)
tree445ad9e9994a7d90cec66268b71c98c9daf9e9a7
parent19c1c709054ea1964d942259c5c33ad6489cd1e0 (diff)
fix `legacy_numeric_constants` suggestion when call is wrapped in parens
-rw-r--r--clippy_lints/src/legacy_numeric_constants.rs8
-rw-r--r--tests/ui/legacy_numeric_constants.fixed15
-rw-r--r--tests/ui/legacy_numeric_constants.rs15
-rw-r--r--tests/ui/legacy_numeric_constants.stderr86
4 files changed, 107 insertions, 17 deletions
diff --git a/clippy_lints/src/legacy_numeric_constants.rs b/clippy_lints/src/legacy_numeric_constants.rs
index b3c63f022d3..6a81db73edf 100644
--- a/clippy_lints/src/legacy_numeric_constants.rs
+++ b/clippy_lints/src/legacy_numeric_constants.rs
@@ -122,17 +122,17 @@ impl<'tcx> LateLintPass<'tcx> for LegacyNumericConstants {
                 "usage of a legacy numeric constant",
             )
         // `<integer>::xxx_value` check
-        } else if let QPath::TypeRelative(_, last_segment) = qpath
+        } else if let QPath::TypeRelative(mod_path, last_segment) = qpath
             && let Some(def_id) = cx.qpath_res(qpath, expr.hir_id).opt_def_id()
             && let Some(par_expr) = get_parent_expr(cx, expr)
             && let ExprKind::Call(_, []) = par_expr.kind
             && is_integer_method(cx, def_id)
         {
             let name = last_segment.ident.name.as_str();
-
+            let mod_name = clippy_utils::source::snippet(cx, mod_path.span, "_");
             (
-                last_segment.ident.span.with_hi(par_expr.span.hi()),
-                name[..=2].to_ascii_uppercase(),
+                par_expr.span,
+                format!("{}::{}", mod_name, name[..=2].to_ascii_uppercase()),
                 "usage of a legacy numeric method",
             )
         } else {
diff --git a/tests/ui/legacy_numeric_constants.fixed b/tests/ui/legacy_numeric_constants.fixed
index 30bb549a9d6..5f3c3308de8 100644
--- a/tests/ui/legacy_numeric_constants.fixed
+++ b/tests/ui/legacy_numeric_constants.fixed
@@ -79,9 +79,24 @@ fn main() {
     f64::consts::E;
     b!();
 
+    std::primitive::i32::MAX;
+    //~^ ERROR: usage of a legacy numeric method
+    //~| HELP: use the associated constant instead
     [(0, "", i128::MAX)];
     //~^ ERROR: usage of a legacy numeric constant
     //~| HELP: use the associated constant instead
+    i32::MAX;
+    //~^ ERROR: usage of a legacy numeric method
+    //~| HELP: use the associated constant instead
+    assert_eq!(0, -i32::MAX);
+    //~^ ERROR: usage of a legacy numeric method
+    //~| HELP: use the associated constant instead
+    i128::MAX;
+    //~^ ERROR: usage of a legacy numeric constant
+    //~| HELP: use the associated constant instead
+    u32::MAX;
+    //~^ ERROR: usage of a legacy numeric method
+    //~| HELP: use the associated constant instead
 }
 
 #[warn(clippy::legacy_numeric_constants)]
diff --git a/tests/ui/legacy_numeric_constants.rs b/tests/ui/legacy_numeric_constants.rs
index d3878199055..0bb27532b41 100644
--- a/tests/ui/legacy_numeric_constants.rs
+++ b/tests/ui/legacy_numeric_constants.rs
@@ -79,9 +79,24 @@ fn main() {
     f64::consts::E;
     b!();
 
+    <std::primitive::i32>::max_value();
+    //~^ ERROR: usage of a legacy numeric method
+    //~| HELP: use the associated constant instead
     [(0, "", std::i128::MAX)];
     //~^ ERROR: usage of a legacy numeric constant
     //~| HELP: use the associated constant instead
+    (i32::max_value());
+    //~^ ERROR: usage of a legacy numeric method
+    //~| HELP: use the associated constant instead
+    assert_eq!(0, -(i32::max_value()));
+    //~^ ERROR: usage of a legacy numeric method
+    //~| HELP: use the associated constant instead
+    (std::i128::MAX);
+    //~^ ERROR: usage of a legacy numeric constant
+    //~| HELP: use the associated constant instead
+    (<u32>::max_value());
+    //~^ ERROR: usage of a legacy numeric method
+    //~| HELP: use the associated constant instead
 }
 
 #[warn(clippy::legacy_numeric_constants)]
diff --git a/tests/ui/legacy_numeric_constants.stderr b/tests/ui/legacy_numeric_constants.stderr
index 4d69b8165a3..41e94fc8aae 100644
--- a/tests/ui/legacy_numeric_constants.stderr
+++ b/tests/ui/legacy_numeric_constants.stderr
@@ -72,10 +72,10 @@ LL |     u32::MAX;
    |     +++++
 
 error: usage of a legacy numeric method
-  --> tests/ui/legacy_numeric_constants.rs:50:10
+  --> tests/ui/legacy_numeric_constants.rs:50:5
    |
 LL |     i32::max_value();
-   |          ^^^^^^^^^^^
+   |     ^^^^^^^^^^^^^^^^
    |
 help: use the associated constant instead
    |
@@ -84,10 +84,10 @@ LL +     i32::MAX;
    |
 
 error: usage of a legacy numeric method
-  --> tests/ui/legacy_numeric_constants.rs:53:9
+  --> tests/ui/legacy_numeric_constants.rs:53:5
    |
 LL |     u8::max_value();
-   |         ^^^^^^^^^^^
+   |     ^^^^^^^^^^^^^^^
    |
 help: use the associated constant instead
    |
@@ -96,10 +96,10 @@ LL +     u8::MAX;
    |
 
 error: usage of a legacy numeric method
-  --> tests/ui/legacy_numeric_constants.rs:56:9
+  --> tests/ui/legacy_numeric_constants.rs:56:5
    |
 LL |     u8::min_value();
-   |         ^^^^^^^^^^^
+   |     ^^^^^^^^^^^^^^^
    |
 help: use the associated constant instead
    |
@@ -120,10 +120,10 @@ LL +     u8::MIN;
    |
 
 error: usage of a legacy numeric method
-  --> tests/ui/legacy_numeric_constants.rs:62:27
+  --> tests/ui/legacy_numeric_constants.rs:62:5
    |
 LL |     ::std::primitive::u8::min_value();
-   |                           ^^^^^^^^^^^
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
 help: use the associated constant instead
    |
@@ -132,10 +132,10 @@ LL +     ::std::primitive::u8::MIN;
    |
 
 error: usage of a legacy numeric method
-  --> tests/ui/legacy_numeric_constants.rs:65:26
+  --> tests/ui/legacy_numeric_constants.rs:65:5
    |
 LL |     std::primitive::i32::max_value();
-   |                          ^^^^^^^^^^^
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
 help: use the associated constant instead
    |
@@ -171,8 +171,20 @@ LL -                 let x = std::u64::MAX;
 LL +                 let x = u64::MAX;
    |
 
+error: usage of a legacy numeric method
+  --> tests/ui/legacy_numeric_constants.rs:82:5
+   |
+LL |     <std::primitive::i32>::max_value();
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+help: use the associated constant instead
+   |
+LL -     <std::primitive::i32>::max_value();
+LL +     std::primitive::i32::MAX;
+   |
+
 error: usage of a legacy numeric constant
-  --> tests/ui/legacy_numeric_constants.rs:82:14
+  --> tests/ui/legacy_numeric_constants.rs:85:14
    |
 LL |     [(0, "", std::i128::MAX)];
    |              ^^^^^^^^^^^^^^
@@ -183,8 +195,56 @@ LL -     [(0, "", std::i128::MAX)];
 LL +     [(0, "", i128::MAX)];
    |
 
+error: usage of a legacy numeric method
+  --> tests/ui/legacy_numeric_constants.rs:88:5
+   |
+LL |     (i32::max_value());
+   |     ^^^^^^^^^^^^^^^^^^
+   |
+help: use the associated constant instead
+   |
+LL -     (i32::max_value());
+LL +     i32::MAX;
+   |
+
+error: usage of a legacy numeric method
+  --> tests/ui/legacy_numeric_constants.rs:91:20
+   |
+LL |     assert_eq!(0, -(i32::max_value()));
+   |                    ^^^^^^^^^^^^^^^^^^
+   |
+help: use the associated constant instead
+   |
+LL -     assert_eq!(0, -(i32::max_value()));
+LL +     assert_eq!(0, -i32::MAX);
+   |
+
+error: usage of a legacy numeric constant
+  --> tests/ui/legacy_numeric_constants.rs:94:5
+   |
+LL |     (std::i128::MAX);
+   |     ^^^^^^^^^^^^^^^^
+   |
+help: use the associated constant instead
+   |
+LL -     (std::i128::MAX);
+LL +     i128::MAX;
+   |
+
+error: usage of a legacy numeric method
+  --> tests/ui/legacy_numeric_constants.rs:97:5
+   |
+LL |     (<u32>::max_value());
+   |     ^^^^^^^^^^^^^^^^^^^^
+   |
+help: use the associated constant instead
+   |
+LL -     (<u32>::max_value());
+LL +     u32::MAX;
+   |
+
 error: usage of a legacy numeric constant
-  --> tests/ui/legacy_numeric_constants.rs:116:5
+  --> tests/ui/legacy_numeric_constants.rs:131:5
    |
 LL |     std::u32::MAX;
    |     ^^^^^^^^^^^^^
@@ -195,5 +255,5 @@ LL -     std::u32::MAX;
 LL +     u32::MAX;
    |
 
-error: aborting due to 16 previous errors
+error: aborting due to 21 previous errors