Skip to content

Commit f1d7286

Browse files
committed
from feedback - more tests, fixes
1 parent 437e09c commit f1d7286

9 files changed

Lines changed: 134 additions & 41 deletions

clippy_lints/src/format_args.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ declare_clippy_lint! {
251251
/// let s: &str = "hello";
252252
/// println!("{}", s);
253253
/// ```
254-
#[clippy::version = "1.89.0"]
254+
#[clippy::version = "1.95.0"]
255255
pub REDUNDANT_REF_IN_FORMAT_ARGS,
256-
nursery,
256+
perf,
257257
"redundant reference in format args causes double reference"
258258
}
259259

tests/ui/explicit_deref_methods.fixed

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
#![allow(unused_variables, unused_must_use)]
44
#![allow(
55
clippy::borrow_deref_ref,
6-
suspicious_double_ref_op,
7-
noop_method_call,
6+
clippy::deref_addrof,
87
clippy::explicit_auto_deref,
98
clippy::needless_borrow,
109
clippy::no_effect,
10+
clippy::redundant_ref_in_format_args,
1111
clippy::uninlined_format_args,
1212
clippy::unnecessary_literal_unwrap,
13-
clippy::deref_addrof
13+
noop_method_call,
14+
suspicious_double_ref_op
1415
)]
1516

1617
use std::ops::{Deref, DerefMut};

tests/ui/explicit_deref_methods.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
#![allow(unused_variables, unused_must_use)]
44
#![allow(
55
clippy::borrow_deref_ref,
6-
suspicious_double_ref_op,
7-
noop_method_call,
6+
clippy::deref_addrof,
87
clippy::explicit_auto_deref,
98
clippy::needless_borrow,
109
clippy::no_effect,
10+
clippy::redundant_ref_in_format_args,
1111
clippy::uninlined_format_args,
1212
clippy::unnecessary_literal_unwrap,
13-
clippy::deref_addrof
13+
noop_method_call,
14+
suspicious_double_ref_op
1415
)]
1516

1617
use std::ops::{Deref, DerefMut};

tests/ui/explicit_deref_methods.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: explicit `deref` method call
2-
--> tests/ui/explicit_deref_methods.rs:58:19
2+
--> tests/ui/explicit_deref_methods.rs:59:19
33
|
44
LL | let b: &str = a.deref();
55
| ^^^^^^^^^ help: try: `&*a`
@@ -8,73 +8,73 @@ LL | let b: &str = a.deref();
88
= help: to override `-D warnings` add `#[allow(clippy::explicit_deref_methods)]`
99

1010
error: explicit `deref_mut` method call
11-
--> tests/ui/explicit_deref_methods.rs:61:23
11+
--> tests/ui/explicit_deref_methods.rs:62:23
1212
|
1313
LL | let b: &mut str = a.deref_mut();
1414
| ^^^^^^^^^^^^^ help: try: `&mut **a`
1515

1616
error: explicit `deref` method call
17-
--> tests/ui/explicit_deref_methods.rs:65:39
17+
--> tests/ui/explicit_deref_methods.rs:66:39
1818
|
1919
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
2020
| ^^^^^^^^^ help: try: `&*a`
2121

2222
error: explicit `deref` method call
23-
--> tests/ui/explicit_deref_methods.rs:65:50
23+
--> tests/ui/explicit_deref_methods.rs:66:50
2424
|
2525
LL | let b: String = format!("{}, {}", a.deref(), a.deref());
2626
| ^^^^^^^^^ help: try: `&*a`
2727

2828
error: explicit `deref` method call
29-
--> tests/ui/explicit_deref_methods.rs:69:20
29+
--> tests/ui/explicit_deref_methods.rs:70:20
3030
|
3131
LL | println!("{}", a.deref());
3232
| ^^^^^^^^^ help: try: `&*a`
3333

3434
error: explicit `deref` method call
35-
--> tests/ui/explicit_deref_methods.rs:73:11
35+
--> tests/ui/explicit_deref_methods.rs:74:11
3636
|
3737
LL | match a.deref() {
3838
| ^^^^^^^^^ help: try: `&*a`
3939

4040
error: explicit `deref` method call
41-
--> tests/ui/explicit_deref_methods.rs:78:28
41+
--> tests/ui/explicit_deref_methods.rs:79:28
4242
|
4343
LL | let b: String = concat(a.deref());
4444
| ^^^^^^^^^ help: try: `&*a`
4545

4646
error: explicit `deref` method call
47-
--> tests/ui/explicit_deref_methods.rs:81:13
47+
--> tests/ui/explicit_deref_methods.rs:82:13
4848
|
4949
LL | let b = just_return(a).deref();
5050
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `just_return(a)`
5151

5252
error: explicit `deref` method call
53-
--> tests/ui/explicit_deref_methods.rs:84:28
53+
--> tests/ui/explicit_deref_methods.rs:85:28
5454
|
5555
LL | let b: String = concat(just_return(a).deref());
5656
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `just_return(a)`
5757

5858
error: explicit `deref` method call
59-
--> tests/ui/explicit_deref_methods.rs:124:31
59+
--> tests/ui/explicit_deref_methods.rs:125:31
6060
|
6161
LL | let b: &str = expr_deref!(a.deref());
6262
| ^^^^^^^^^ help: try: `&*a`
6363

6464
error: explicit `deref` method call
65-
--> tests/ui/explicit_deref_methods.rs:154:14
65+
--> tests/ui/explicit_deref_methods.rs:155:14
6666
|
6767
LL | let _ = &Deref::deref(&"foo");
6868
| ^^^^^^^^^^^^^^^^^^^^ help: try: `*&"foo"`
6969

7070
error: explicit `deref_mut` method call
71-
--> tests/ui/explicit_deref_methods.rs:156:14
71+
--> tests/ui/explicit_deref_methods.rs:157:14
7272
|
7373
LL | let _ = &DerefMut::deref_mut(&mut x);
7474
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut **&mut x`
7575

7676
error: explicit `deref_mut` method call
77-
--> tests/ui/explicit_deref_methods.rs:157:14
77+
--> tests/ui/explicit_deref_methods.rs:158:14
7878
|
7979
LL | let _ = &DerefMut::deref_mut((&mut &mut x).deref_mut());
8080
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut ***(&mut &mut x)`

tests/ui/recursive_format_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
clippy::borrow_deref_ref,
44
clippy::deref_addrof,
55
clippy::inherent_to_string_shadow_display,
6+
clippy::redundant_ref_in_format_args,
67
clippy::to_string_in_format_args,
78
clippy::uninlined_format_args
89
)]

tests/ui/recursive_format_impl.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: using `self.to_string` in `fmt::Display` implementation will cause infinite recursion
2-
--> tests/ui/recursive_format_impl.rs:31:25
2+
--> tests/ui/recursive_format_impl.rs:32:25
33
|
44
LL | write!(f, "{}", self.to_string())
55
| ^^^^^^^^^^^^^^^^
@@ -8,71 +8,71 @@ LL | write!(f, "{}", self.to_string())
88
= help: to override `-D warnings` add `#[allow(clippy::recursive_format_impl)]`
99

1010
error: using `self` as `Display` in `impl Display` will cause infinite recursion
11-
--> tests/ui/recursive_format_impl.rs:76:9
11+
--> tests/ui/recursive_format_impl.rs:77:9
1212
|
1313
LL | write!(f, "{}", self)
1414
| ^^^^^^^^^^^^^^^^^^^^^
1515
|
1616
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
1717

1818
error: using `self` as `Display` in `impl Display` will cause infinite recursion
19-
--> tests/ui/recursive_format_impl.rs:86:9
19+
--> tests/ui/recursive_format_impl.rs:87:9
2020
|
2121
LL | write!(f, "{}", &self)
2222
| ^^^^^^^^^^^^^^^^^^^^^^
2323
|
2424
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
2525

2626
error: using `self` as `Debug` in `impl Debug` will cause infinite recursion
27-
--> tests/ui/recursive_format_impl.rs:93:9
27+
--> tests/ui/recursive_format_impl.rs:94:9
2828
|
2929
LL | write!(f, "{:?}", &self)
3030
| ^^^^^^^^^^^^^^^^^^^^^^^^
3131
|
3232
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
3333

3434
error: using `self` as `Display` in `impl Display` will cause infinite recursion
35-
--> tests/ui/recursive_format_impl.rs:103:9
35+
--> tests/ui/recursive_format_impl.rs:104:9
3636
|
3737
LL | write!(f, "{}", &&&self)
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^
3939
|
4040
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
4141

4242
error: using `self` as `Display` in `impl Display` will cause infinite recursion
43-
--> tests/ui/recursive_format_impl.rs:178:9
43+
--> tests/ui/recursive_format_impl.rs:179:9
4444
|
4545
LL | write!(f, "{}", &*self)
4646
| ^^^^^^^^^^^^^^^^^^^^^^^
4747
|
4848
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
4949

5050
error: using `self` as `Debug` in `impl Debug` will cause infinite recursion
51-
--> tests/ui/recursive_format_impl.rs:185:9
51+
--> tests/ui/recursive_format_impl.rs:186:9
5252
|
5353
LL | write!(f, "{:?}", &*self)
5454
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5555
|
5656
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
5757

5858
error: using `self` as `Display` in `impl Display` will cause infinite recursion
59-
--> tests/ui/recursive_format_impl.rs:202:9
59+
--> tests/ui/recursive_format_impl.rs:203:9
6060
|
6161
LL | write!(f, "{}", *self)
6262
| ^^^^^^^^^^^^^^^^^^^^^^
6363
|
6464
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
6565

6666
error: using `self` as `Display` in `impl Display` will cause infinite recursion
67-
--> tests/ui/recursive_format_impl.rs:219:9
67+
--> tests/ui/recursive_format_impl.rs:220:9
6868
|
6969
LL | write!(f, "{}", **&&*self)
7070
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
7171
|
7272
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
7373

7474
error: using `self` as `Display` in `impl Display` will cause infinite recursion
75-
--> tests/ui/recursive_format_impl.rs:236:9
75+
--> tests/ui/recursive_format_impl.rs:237:9
7676
|
7777
LL | write!(f, "{}", &&**&&*self)
7878
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/redundant_ref_in_format_args.fixed

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ fn main() {
6969
println!("{}", *a); //~ redundant_ref_in_format_args
7070
println!("{:?}", *a); //~ redundant_ref_in_format_args
7171

72+
// Parenthesized expressions: &(expr)
73+
let n: i32 = 42;
74+
println!("{}", (n)); //~ redundant_ref_in_format_args
75+
println!("{:?}", (n + 1)); //~ redundant_ref_in_format_args
76+
println!("{}", (String::from("paren"))); //~ redundant_ref_in_format_args
77+
78+
// Block expressions: &{ expr }
79+
println!("{}", { n }); //~ redundant_ref_in_format_args
80+
println!("{:?}", { n + 1 }); //~ redundant_ref_in_format_args
81+
println!("{}", { String::from("block") }); //~ redundant_ref_in_format_args
82+
7283
let v1 = 42.12345;
7384
let v2 = 20;
7485
let v3 = 10;
@@ -83,4 +94,20 @@ fn main() {
8394
println!("{0:1$.2$}", v1, v2, v3); //~ redundant_ref_in_format_args
8495
println!("{0:1$.2$}", v1, v2, v3); //~ redundant_ref_in_format_args
8596
println!("{0:1$.2$}", v1, v2, v3); //~ redundant_ref_in_format_args
97+
98+
// Macro wrapping println! - should not lint (println! call is inside macro expansion)
99+
macro_rules! my_println {
100+
($($args:tt)*) => {
101+
println!($($args)*);
102+
};
103+
}
104+
my_println!("{}", &n);
105+
106+
// Arguments coming from a macro - should not lint (& comes from expansion)
107+
macro_rules! make_ref {
108+
($e:expr) => {
109+
&$e
110+
};
111+
}
112+
println!("{}", make_ref!(n));
86113
}

tests/ui/redundant_ref_in_format_args.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ fn main() {
6969
println!("{}", &*a); //~ redundant_ref_in_format_args
7070
println!("{:?}", &*a); //~ redundant_ref_in_format_args
7171

72+
// Parenthesized expressions: &(expr)
73+
let n: i32 = 42;
74+
println!("{}", &(n)); //~ redundant_ref_in_format_args
75+
println!("{:?}", &(n + 1)); //~ redundant_ref_in_format_args
76+
println!("{}", &(String::from("paren"))); //~ redundant_ref_in_format_args
77+
78+
// Block expressions: &{ expr }
79+
println!("{}", &{ n }); //~ redundant_ref_in_format_args
80+
println!("{:?}", &{ n + 1 }); //~ redundant_ref_in_format_args
81+
println!("{}", &{ String::from("block") }); //~ redundant_ref_in_format_args
82+
7283
let v1 = 42.12345;
7384
let v2 = 20;
7485
let v3 = 10;
@@ -83,4 +94,20 @@ fn main() {
8394
println!("{0:1$.2$}", &v1, v2, v3); //~ redundant_ref_in_format_args
8495
println!("{0:1$.2$}", v1, &v2, v3); //~ redundant_ref_in_format_args
8596
println!("{0:1$.2$}", v1, v2, &v3); //~ redundant_ref_in_format_args
97+
98+
// Macro wrapping println! - should not lint (println! call is inside macro expansion)
99+
macro_rules! my_println {
100+
($($args:tt)*) => {
101+
println!($($args)*);
102+
};
103+
}
104+
my_println!("{}", &n);
105+
106+
// Arguments coming from a macro - should not lint (& comes from expansion)
107+
macro_rules! make_ref {
108+
($e:expr) => {
109+
&$e
110+
};
111+
}
112+
println!("{}", make_ref!(n));
86113
}

0 commit comments

Comments
 (0)