Skip to content

Commit e30313c

Browse files
committed
fix legacy_numeric_constants suggestion when call is wrapped in parens
1 parent 19c1c70 commit e30313c

File tree

4 files changed

+107
-17
lines changed

4 files changed

+107
-17
lines changed

clippy_lints/src/legacy_numeric_constants.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,17 @@ impl<'tcx> LateLintPass<'tcx> for LegacyNumericConstants {
122122
"usage of a legacy numeric constant",
123123
)
124124
// `<integer>::xxx_value` check
125-
} else if let QPath::TypeRelative(_, last_segment) = qpath
125+
} else if let QPath::TypeRelative(mod_path, last_segment) = qpath
126126
&& let Some(def_id) = cx.qpath_res(qpath, expr.hir_id).opt_def_id()
127127
&& let Some(par_expr) = get_parent_expr(cx, expr)
128128
&& let ExprKind::Call(_, []) = par_expr.kind
129129
&& is_integer_method(cx, def_id)
130130
{
131131
let name = last_segment.ident.name.as_str();
132-
132+
let mod_name = clippy_utils::source::snippet(cx, mod_path.span, "_");
133133
(
134-
last_segment.ident.span.with_hi(par_expr.span.hi()),
135-
name[..=2].to_ascii_uppercase(),
134+
par_expr.span,
135+
format!("{}::{}", mod_name, name[..=2].to_ascii_uppercase()),
136136
"usage of a legacy numeric method",
137137
)
138138
} else {

tests/ui/legacy_numeric_constants.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,24 @@ fn main() {
7979
f64::consts::E;
8080
b!();
8181

82+
std::primitive::i32::MAX;
83+
//~^ ERROR: usage of a legacy numeric method
84+
//~| HELP: use the associated constant instead
8285
[(0, "", i128::MAX)];
8386
//~^ ERROR: usage of a legacy numeric constant
8487
//~| HELP: use the associated constant instead
88+
i32::MAX;
89+
//~^ ERROR: usage of a legacy numeric method
90+
//~| HELP: use the associated constant instead
91+
assert_eq!(0, -i32::MAX);
92+
//~^ ERROR: usage of a legacy numeric method
93+
//~| HELP: use the associated constant instead
94+
i128::MAX;
95+
//~^ ERROR: usage of a legacy numeric constant
96+
//~| HELP: use the associated constant instead
97+
u32::MAX;
98+
//~^ ERROR: usage of a legacy numeric method
99+
//~| HELP: use the associated constant instead
85100
}
86101

87102
#[warn(clippy::legacy_numeric_constants)]

tests/ui/legacy_numeric_constants.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,24 @@ fn main() {
7979
f64::consts::E;
8080
b!();
8181

82+
<std::primitive::i32>::max_value();
83+
//~^ ERROR: usage of a legacy numeric method
84+
//~| HELP: use the associated constant instead
8285
[(0, "", std::i128::MAX)];
8386
//~^ ERROR: usage of a legacy numeric constant
8487
//~| HELP: use the associated constant instead
88+
(i32::max_value());
89+
//~^ ERROR: usage of a legacy numeric method
90+
//~| HELP: use the associated constant instead
91+
assert_eq!(0, -(i32::max_value()));
92+
//~^ ERROR: usage of a legacy numeric method
93+
//~| HELP: use the associated constant instead
94+
(std::i128::MAX);
95+
//~^ ERROR: usage of a legacy numeric constant
96+
//~| HELP: use the associated constant instead
97+
(<u32>::max_value());
98+
//~^ ERROR: usage of a legacy numeric method
99+
//~| HELP: use the associated constant instead
85100
}
86101

87102
#[warn(clippy::legacy_numeric_constants)]

tests/ui/legacy_numeric_constants.stderr

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ LL | u32::MAX;
7272
| +++++
7373

7474
error: usage of a legacy numeric method
75-
--> tests/ui/legacy_numeric_constants.rs:50:10
75+
--> tests/ui/legacy_numeric_constants.rs:50:5
7676
|
7777
LL | i32::max_value();
78-
| ^^^^^^^^^^^
78+
| ^^^^^^^^^^^^^^^^
7979
|
8080
help: use the associated constant instead
8181
|
@@ -84,10 +84,10 @@ LL + i32::MAX;
8484
|
8585

8686
error: usage of a legacy numeric method
87-
--> tests/ui/legacy_numeric_constants.rs:53:9
87+
--> tests/ui/legacy_numeric_constants.rs:53:5
8888
|
8989
LL | u8::max_value();
90-
| ^^^^^^^^^^^
90+
| ^^^^^^^^^^^^^^^
9191
|
9292
help: use the associated constant instead
9393
|
@@ -96,10 +96,10 @@ LL + u8::MAX;
9696
|
9797

9898
error: usage of a legacy numeric method
99-
--> tests/ui/legacy_numeric_constants.rs:56:9
99+
--> tests/ui/legacy_numeric_constants.rs:56:5
100100
|
101101
LL | u8::min_value();
102-
| ^^^^^^^^^^^
102+
| ^^^^^^^^^^^^^^^
103103
|
104104
help: use the associated constant instead
105105
|
@@ -120,10 +120,10 @@ LL + u8::MIN;
120120
|
121121

122122
error: usage of a legacy numeric method
123-
--> tests/ui/legacy_numeric_constants.rs:62:27
123+
--> tests/ui/legacy_numeric_constants.rs:62:5
124124
|
125125
LL | ::std::primitive::u8::min_value();
126-
| ^^^^^^^^^^^
126+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
127127
|
128128
help: use the associated constant instead
129129
|
@@ -132,10 +132,10 @@ LL + ::std::primitive::u8::MIN;
132132
|
133133

134134
error: usage of a legacy numeric method
135-
--> tests/ui/legacy_numeric_constants.rs:65:26
135+
--> tests/ui/legacy_numeric_constants.rs:65:5
136136
|
137137
LL | std::primitive::i32::max_value();
138-
| ^^^^^^^^^^^
138+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139139
|
140140
help: use the associated constant instead
141141
|
@@ -171,8 +171,20 @@ LL - let x = std::u64::MAX;
171171
LL + let x = u64::MAX;
172172
|
173173

174+
error: usage of a legacy numeric method
175+
--> tests/ui/legacy_numeric_constants.rs:82:5
176+
|
177+
LL | <std::primitive::i32>::max_value();
178+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
179+
|
180+
help: use the associated constant instead
181+
|
182+
LL - <std::primitive::i32>::max_value();
183+
LL + std::primitive::i32::MAX;
184+
|
185+
174186
error: usage of a legacy numeric constant
175-
--> tests/ui/legacy_numeric_constants.rs:82:14
187+
--> tests/ui/legacy_numeric_constants.rs:85:14
176188
|
177189
LL | [(0, "", std::i128::MAX)];
178190
| ^^^^^^^^^^^^^^
@@ -183,8 +195,56 @@ LL - [(0, "", std::i128::MAX)];
183195
LL + [(0, "", i128::MAX)];
184196
|
185197

198+
error: usage of a legacy numeric method
199+
--> tests/ui/legacy_numeric_constants.rs:88:5
200+
|
201+
LL | (i32::max_value());
202+
| ^^^^^^^^^^^^^^^^^^
203+
|
204+
help: use the associated constant instead
205+
|
206+
LL - (i32::max_value());
207+
LL + i32::MAX;
208+
|
209+
210+
error: usage of a legacy numeric method
211+
--> tests/ui/legacy_numeric_constants.rs:91:20
212+
|
213+
LL | assert_eq!(0, -(i32::max_value()));
214+
| ^^^^^^^^^^^^^^^^^^
215+
|
216+
help: use the associated constant instead
217+
|
218+
LL - assert_eq!(0, -(i32::max_value()));
219+
LL + assert_eq!(0, -i32::MAX);
220+
|
221+
222+
error: usage of a legacy numeric constant
223+
--> tests/ui/legacy_numeric_constants.rs:94:5
224+
|
225+
LL | (std::i128::MAX);
226+
| ^^^^^^^^^^^^^^^^
227+
|
228+
help: use the associated constant instead
229+
|
230+
LL - (std::i128::MAX);
231+
LL + i128::MAX;
232+
|
233+
234+
error: usage of a legacy numeric method
235+
--> tests/ui/legacy_numeric_constants.rs:97:5
236+
|
237+
LL | (<u32>::max_value());
238+
| ^^^^^^^^^^^^^^^^^^^^
239+
|
240+
help: use the associated constant instead
241+
|
242+
LL - (<u32>::max_value());
243+
LL + u32::MAX;
244+
|
245+
186246
error: usage of a legacy numeric constant
187-
--> tests/ui/legacy_numeric_constants.rs:116:5
247+
--> tests/ui/legacy_numeric_constants.rs:131:5
188248
|
189249
LL | std::u32::MAX;
190250
| ^^^^^^^^^^^^^
@@ -195,5 +255,5 @@ LL - std::u32::MAX;
195255
LL + u32::MAX;
196256
|
197257

198-
error: aborting due to 16 previous errors
258+
error: aborting due to 21 previous errors
199259

0 commit comments

Comments
 (0)