Skip to content

Commit cedfb21

Browse files
committed
fix ip_constant when call wrapped in extra parens
1 parent d4dea4b commit cedfb21

File tree

4 files changed

+108
-23
lines changed

4 files changed

+108
-23
lines changed

clippy_lints/src/methods/ip_constant.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ use smallvec::SmallVec;
99
use super::IP_CONSTANT;
1010

1111
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, func: &Expr<'_>, args: &[Expr<'_>]) {
12-
if let ExprKind::Path(QPath::TypeRelative(
13-
Ty {
14-
kind: TyKind::Path(QPath::Resolved(_, func_path)),
15-
..
16-
},
17-
p,
18-
)) = func.kind
12+
if let ExprKind::Path(QPath::TypeRelative(ty, p)) = func.kind
13+
&& let TyKind::Path(QPath::Resolved(_, func_path)) = ty.kind
1914
&& p.ident.name == sym::new
2015
&& let Some(func_def_id) = func_path.res.opt_def_id()
2116
&& matches!(
@@ -40,13 +35,15 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, func: &Expr<'_>, args
4035
_ => return,
4136
};
4237

38+
let mut sugg = vec![(expr.span.with_lo(p.ident.span.lo()), constant_name.to_owned())];
39+
let before_span = expr.span.shrink_to_lo().until(ty.span);
40+
if !before_span.is_empty() {
41+
// Remove everything before the type name
42+
sugg.push((before_span, String::new()));
43+
};
44+
4345
span_lint_and_then(cx, IP_CONSTANT, expr.span, "hand-coded well-known IP address", |diag| {
44-
diag.span_suggestion_verbose(
45-
expr.span.with_lo(p.ident.span.lo()),
46-
"use",
47-
constant_name,
48-
Applicability::MachineApplicable,
49-
);
46+
diag.multipart_suggestion_verbose("use", sugg, Applicability::MachineApplicable);
5047
});
5148
}
5249
}

tests/ui/ip_constant.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ fn literal_test3() {
4848
//~^ ip_constant
4949
}
5050

51+
fn wrapped_in_parens() {
52+
let _ = std::net::Ipv4Addr::LOCALHOST;
53+
//~^ ip_constant
54+
let _ = std::net::Ipv4Addr::BROADCAST;
55+
//~^ ip_constant
56+
let _ = std::net::Ipv4Addr::UNSPECIFIED;
57+
//~^ ip_constant
58+
59+
let _ = std::net::Ipv6Addr::LOCALHOST;
60+
//~^ ip_constant
61+
let _ = std::net::Ipv6Addr::UNSPECIFIED;
62+
//~^ ip_constant
63+
}
64+
5165
const CONST_U8_0: u8 = 0;
5266
const CONST_U8_1: u8 = 1;
5367
const CONST_U8_127: u8 = 127;

tests/ui/ip_constant.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ fn literal_test3() {
4848
//~^ ip_constant
4949
}
5050

51+
fn wrapped_in_parens() {
52+
let _ = (std::net::Ipv4Addr::new(127, 0, 0, 1));
53+
//~^ ip_constant
54+
let _ = (std::net::Ipv4Addr::new(255, 255, 255, 255));
55+
//~^ ip_constant
56+
let _ = (std::net::Ipv4Addr::new(0, 0, 0, 0));
57+
//~^ ip_constant
58+
59+
let _ = (std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
60+
//~^ ip_constant
61+
let _ = (std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
62+
//~^ ip_constant
63+
}
64+
5165
const CONST_U8_0: u8 = 0;
5266
const CONST_U8_1: u8 = 1;
5367
const CONST_U8_127: u8 = 127;

tests/ui/ip_constant.stderr

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,69 @@ LL - let _ = std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
180180
LL + let _ = std::net::Ipv6Addr::UNSPECIFIED;
181181
|
182182

183+
error: hand-coded well-known IP address
184+
--> tests/ui/ip_constant.rs:52:13
185+
|
186+
LL | let _ = (std::net::Ipv4Addr::new(127, 0, 0, 1));
187+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
188+
|
189+
help: use
190+
|
191+
LL - let _ = (std::net::Ipv4Addr::new(127, 0, 0, 1));
192+
LL + let _ = std::net::Ipv4Addr::LOCALHOST;
193+
|
194+
195+
error: hand-coded well-known IP address
196+
--> tests/ui/ip_constant.rs:54:13
197+
|
198+
LL | let _ = (std::net::Ipv4Addr::new(255, 255, 255, 255));
199+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
200+
|
201+
help: use
202+
|
203+
LL - let _ = (std::net::Ipv4Addr::new(255, 255, 255, 255));
204+
LL + let _ = std::net::Ipv4Addr::BROADCAST;
205+
|
206+
207+
error: hand-coded well-known IP address
208+
--> tests/ui/ip_constant.rs:56:13
209+
|
210+
LL | let _ = (std::net::Ipv4Addr::new(0, 0, 0, 0));
211+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
212+
|
213+
help: use
214+
|
215+
LL - let _ = (std::net::Ipv4Addr::new(0, 0, 0, 0));
216+
LL + let _ = std::net::Ipv4Addr::UNSPECIFIED;
217+
|
218+
219+
error: hand-coded well-known IP address
220+
--> tests/ui/ip_constant.rs:59:13
221+
|
222+
LL | let _ = (std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
223+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
224+
|
225+
help: use
226+
|
227+
LL - let _ = (std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
228+
LL + let _ = std::net::Ipv6Addr::LOCALHOST;
229+
|
230+
183231
error: hand-coded well-known IP address
184232
--> tests/ui/ip_constant.rs:61:13
185233
|
234+
LL | let _ = (std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
235+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
236+
|
237+
help: use
238+
|
239+
LL - let _ = (std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
240+
LL + let _ = std::net::Ipv6Addr::UNSPECIFIED;
241+
|
242+
243+
error: hand-coded well-known IP address
244+
--> tests/ui/ip_constant.rs:75:13
245+
|
186246
LL | let _ = Ipv4Addr::new(CONST_U8_127, CONST_U8_0, CONST_U8_0, CONST_U8_1);
187247
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
188248
|
@@ -193,7 +253,7 @@ LL + let _ = Ipv4Addr::LOCALHOST;
193253
|
194254

195255
error: hand-coded well-known IP address
196-
--> tests/ui/ip_constant.rs:63:13
256+
--> tests/ui/ip_constant.rs:77:13
197257
|
198258
LL | let _ = Ipv4Addr::new(CONST_U8_255, CONST_U8_255, CONST_U8_255, CONST_U8_255);
199259
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -205,7 +265,7 @@ LL + let _ = Ipv4Addr::BROADCAST;
205265
|
206266

207267
error: hand-coded well-known IP address
208-
--> tests/ui/ip_constant.rs:65:13
268+
--> tests/ui/ip_constant.rs:79:13
209269
|
210270
LL | let _ = Ipv4Addr::new(CONST_U8_0, CONST_U8_0, CONST_U8_0, CONST_U8_0);
211271
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -217,7 +277,7 @@ LL + let _ = Ipv4Addr::UNSPECIFIED;
217277
|
218278

219279
error: hand-coded well-known IP address
220-
--> tests/ui/ip_constant.rs:69:13
280+
--> tests/ui/ip_constant.rs:83:13
221281
|
222282
LL | let _ = Ipv6Addr::new(
223283
| _____________^
@@ -246,7 +306,7 @@ LL + let _ = Ipv6Addr::LOCALHOST;
246306
|
247307

248308
error: hand-coded well-known IP address
249-
--> tests/ui/ip_constant.rs:81:13
309+
--> tests/ui/ip_constant.rs:95:13
250310
|
251311
LL | let _ = Ipv6Addr::new(
252312
| _____________^
@@ -275,7 +335,7 @@ LL + let _ = Ipv6Addr::UNSPECIFIED;
275335
|
276336

277337
error: hand-coded well-known IP address
278-
--> tests/ui/ip_constant.rs:96:13
338+
--> tests/ui/ip_constant.rs:110:13
279339
|
280340
LL | let _ = Ipv4Addr::new(126 + 1, 0, 0, 1);
281341
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -287,7 +347,7 @@ LL + let _ = Ipv4Addr::LOCALHOST;
287347
|
288348

289349
error: hand-coded well-known IP address
290-
--> tests/ui/ip_constant.rs:98:13
350+
--> tests/ui/ip_constant.rs:112:13
291351
|
292352
LL | let _ = Ipv4Addr::new(254 + CONST_U8_1, 255, { 255 - CONST_U8_0 }, CONST_U8_255);
293353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -299,7 +359,7 @@ LL + let _ = Ipv4Addr::BROADCAST;
299359
|
300360

301361
error: hand-coded well-known IP address
302-
--> tests/ui/ip_constant.rs:100:13
362+
--> tests/ui/ip_constant.rs:114:13
303363
|
304364
LL | let _ = Ipv4Addr::new(0, CONST_U8_255 - 255, 0, { 1 + 0 - 1 });
305365
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -311,7 +371,7 @@ LL + let _ = Ipv4Addr::UNSPECIFIED;
311371
|
312372

313373
error: hand-coded well-known IP address
314-
--> tests/ui/ip_constant.rs:104:13
374+
--> tests/ui/ip_constant.rs:118:13
315375
|
316376
LL | let _ = Ipv6Addr::new(0 + CONST_U16_0, 0, 0, 0, 0, 0, 0, 1);
317377
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -323,7 +383,7 @@ LL + let _ = Ipv6Addr::LOCALHOST;
323383
|
324384

325385
error: hand-coded well-known IP address
326-
--> tests/ui/ip_constant.rs:106:13
386+
--> tests/ui/ip_constant.rs:120:13
327387
|
328388
LL | let _ = Ipv6Addr::new(0 + 0, 0, 0, 0, 0, { 2 - 1 - CONST_U16_1 }, 0, 1);
329389
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -334,5 +394,5 @@ LL - let _ = Ipv6Addr::new(0 + 0, 0, 0, 0, 0, { 2 - 1 - CONST_U16_1 }, 0, 1)
334394
LL + let _ = Ipv6Addr::LOCALHOST;
335395
|
336396

337-
error: aborting due to 25 previous errors
397+
error: aborting due to 30 previous errors
338398

0 commit comments

Comments
 (0)