Skip to content

fix legacy_numeric_constants suggestion when call is wrapped in parens #15191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions clippy_lints/src/legacy_numeric_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<'tcx> LateLintPass<'tcx> for LegacyNumericConstants {
};

// `std::<integer>::<CONST>` check
let (span, sugg, msg) = if let QPath::Resolved(None, path) = qpath
let (span, (sugg_span, sugg), msg) = if let QPath::Resolved(None, path) = qpath
&& let Some(def_id) = path.res.opt_def_id()
&& is_numeric_const(cx, def_id)
&& let def_path = cx.get_def_path(def_id)
Expand All @@ -118,21 +118,24 @@ impl<'tcx> LateLintPass<'tcx> for LegacyNumericConstants {
{
(
expr.span,
format!("{mod_name}::{name}"),
(expr.span, format!("{mod_name}::{name}")),
"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(),
qpath.span(),
(
par_expr.span,
format!("{}::{}", mod_name, name[..=2].to_ascii_uppercase()),
),
"usage of a legacy numeric method",
)
} else {
Expand All @@ -145,7 +148,7 @@ impl<'tcx> LateLintPass<'tcx> for LegacyNumericConstants {
{
span_lint_hir_and_then(cx, LEGACY_NUMERIC_CONSTANTS, expr.hir_id, span, msg, |diag| {
diag.span_suggestion_verbose(
span,
sugg_span,
"use the associated constant instead",
sugg,
Applicability::MaybeIncorrect,
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/legacy_numeric_constants.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/legacy_numeric_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
86 changes: 73 additions & 13 deletions tests/ui/legacy_numeric_constants.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand All @@ -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
|
Expand All @@ -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
|
Expand All @@ -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
|
Expand All @@ -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
|
Expand Down Expand Up @@ -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:6
|
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)];
| ^^^^^^^^^^^^^^
Expand All @@ -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:6
|
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:21
|
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:7
|
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;
| ^^^^^^^^^^^^^
Expand All @@ -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

Loading