Skip to content

Commit ee532c0

Browse files
committed
Don't lint explicit_auto_deref on reborrows
1 parent 8a74d33 commit ee532c0

File tree

5 files changed

+76
-32
lines changed

5 files changed

+76
-32
lines changed

clippy_lints/src/dereference.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ enum State {
183183
deref_span: Span,
184184
deref_hir_id: HirId,
185185
},
186+
Reborrow {
187+
deref_span: Span,
188+
deref_hir_id: HirId,
189+
},
186190
Borrow,
187191
}
188192

@@ -395,10 +399,38 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
395399
));
396400
},
397401
(Some((State::Borrow, data)), RefOp::Deref) => {
402+
if typeck.expr_ty(sub_expr).is_ref() {
403+
self.state = Some((
404+
State::Reborrow {
405+
deref_span: expr.span,
406+
deref_hir_id: expr.hir_id,
407+
},
408+
data,
409+
));
410+
} else {
411+
self.state = Some((
412+
State::ExplicitDeref {
413+
deref_span: expr.span,
414+
deref_hir_id: expr.hir_id,
415+
},
416+
data,
417+
));
418+
}
419+
},
420+
(
421+
Some((
422+
State::Reborrow {
423+
deref_span,
424+
deref_hir_id,
425+
},
426+
data,
427+
)),
428+
RefOp::Deref,
429+
) => {
398430
self.state = Some((
399431
State::ExplicitDeref {
400-
deref_span: expr.span,
401-
deref_hir_id: expr.hir_id,
432+
deref_span,
433+
deref_hir_id,
402434
},
403435
data,
404436
));
@@ -959,7 +991,7 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, state: State, data
959991
},
960992
);
961993
},
962-
State::Borrow => (),
994+
State::Borrow | State::Reborrow { .. } => (),
963995
}
964996
}
965997

tests/ui/borrow_deref_ref_unfixable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(dead_code, unused_variables, clippy::explicit_auto_deref)]
1+
#![allow(dead_code, unused_variables)]
22

33
fn main() {}
44

tests/ui/explicit_auto_deref.fixed

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
clippy::needless_borrow,
99
clippy::ptr_arg,
1010
clippy::redundant_field_names,
11-
clippy::too_many_arguments
11+
clippy::too_many_arguments,
12+
clippy::borrow_deref_ref
1213
)]
1314

1415
trait CallableStr {
@@ -48,6 +49,7 @@ impl<U: ?Sized> CallableT<U> for i32 {
4849
}
4950

5051
fn f_str(_: &str) {}
52+
fn f_string(_: &String) {}
5153
fn f_t<T>(_: T) {}
5254
fn f_ref_t<T: ?Sized>(_: &T) {}
5355

@@ -158,4 +160,8 @@ fn main() {
158160
}
159161
let _ = E2::S1(&*s); // Don't lint. Inferred type would change.
160162
let _ = E2::S2 { s: &*s }; // Don't lint. Inferred type would change.
163+
164+
let ref_s = &s;
165+
let _: &String = &*ref_s; // Don't lint reborrow.
166+
f_string(&*ref_s); // Don't lint reborrow.
161167
}

tests/ui/explicit_auto_deref.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
clippy::needless_borrow,
99
clippy::ptr_arg,
1010
clippy::redundant_field_names,
11-
clippy::too_many_arguments
11+
clippy::too_many_arguments,
12+
clippy::borrow_deref_ref
1213
)]
1314

1415
trait CallableStr {
@@ -48,6 +49,7 @@ impl<U: ?Sized> CallableT<U> for i32 {
4849
}
4950

5051
fn f_str(_: &str) {}
52+
fn f_string(_: &String) {}
5153
fn f_t<T>(_: T) {}
5254
fn f_ref_t<T: ?Sized>(_: &T) {}
5355

@@ -158,4 +160,8 @@ fn main() {
158160
}
159161
let _ = E2::S1(&*s); // Don't lint. Inferred type would change.
160162
let _ = E2::S2 { s: &*s }; // Don't lint. Inferred type would change.
163+
164+
let ref_s = &s;
165+
let _: &String = &*ref_s; // Don't lint reborrow.
166+
f_string(&*ref_s); // Don't lint reborrow.
161167
}

tests/ui/explicit_auto_deref.stderr

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,157 @@
11
error: deref which would be done by auto-deref
2-
--> $DIR/explicit_auto_deref.rs:61:20
2+
--> $DIR/explicit_auto_deref.rs:63:20
33
|
44
LL | let _: &str = &*s;
55
| ^^ help: try this: `s`
66
|
77
= note: `-D clippy::explicit-auto-deref` implied by `-D warnings`
88

99
error: deref which would be done by auto-deref
10-
--> $DIR/explicit_auto_deref.rs:65:12
10+
--> $DIR/explicit_auto_deref.rs:67:12
1111
|
1212
LL | f_str(&*s);
1313
| ^^ help: try this: `s`
1414

1515
error: deref which would be done by auto-deref
16-
--> $DIR/explicit_auto_deref.rs:69:14
16+
--> $DIR/explicit_auto_deref.rs:71:14
1717
|
1818
LL | f_str_t(&*s, &*s); // Don't lint second param.
1919
| ^^ help: try this: `s`
2020

2121
error: deref which would be done by auto-deref
22-
--> $DIR/explicit_auto_deref.rs:72:25
22+
--> $DIR/explicit_auto_deref.rs:74:25
2323
|
2424
LL | let _: &Box<i32> = &**b;
2525
| ^^^ help: try this: `b`
2626

2727
error: deref which would be done by auto-deref
28-
--> $DIR/explicit_auto_deref.rs:78:8
28+
--> $DIR/explicit_auto_deref.rs:80:8
2929
|
3030
LL | c(&*s);
3131
| ^^ help: try this: `s`
3232

3333
error: deref which would be done by auto-deref
34-
--> $DIR/explicit_auto_deref.rs:84:9
34+
--> $DIR/explicit_auto_deref.rs:86:9
3535
|
3636
LL | &**x
3737
| ^^^^ help: try this: `x`
3838

3939
error: deref which would be done by auto-deref
40-
--> $DIR/explicit_auto_deref.rs:88:11
40+
--> $DIR/explicit_auto_deref.rs:90:11
4141
|
4242
LL | { &**x }
4343
| ^^^^ help: try this: `x`
4444

4545
error: deref which would be done by auto-deref
46-
--> $DIR/explicit_auto_deref.rs:92:9
46+
--> $DIR/explicit_auto_deref.rs:94:9
4747
|
4848
LL | &**{ x }
4949
| ^^^^^^^^ help: try this: `{ x }`
5050

5151
error: deref which would be done by auto-deref
52-
--> $DIR/explicit_auto_deref.rs:96:9
52+
--> $DIR/explicit_auto_deref.rs:98:9
5353
|
5454
LL | &***x
5555
| ^^^^^ help: try this: `x`
5656

5757
error: deref which would be done by auto-deref
58-
--> $DIR/explicit_auto_deref.rs:113:13
58+
--> $DIR/explicit_auto_deref.rs:115:13
5959
|
6060
LL | f1(&*x);
6161
| ^^ help: try this: `x`
6262

6363
error: deref which would be done by auto-deref
64-
--> $DIR/explicit_auto_deref.rs:114:13
64+
--> $DIR/explicit_auto_deref.rs:116:13
6565
|
6666
LL | f2(&*x);
6767
| ^^ help: try this: `x`
6868

6969
error: deref which would be done by auto-deref
70-
--> $DIR/explicit_auto_deref.rs:115:13
70+
--> $DIR/explicit_auto_deref.rs:117:13
7171
|
7272
LL | f3(&*x);
7373
| ^^ help: try this: `x`
7474

7575
error: deref which would be done by auto-deref
76-
--> $DIR/explicit_auto_deref.rs:116:28
76+
--> $DIR/explicit_auto_deref.rs:118:28
7777
|
7878
LL | f4.callable_str()(&*x);
7979
| ^^ help: try this: `x`
8080

8181
error: deref which would be done by auto-deref
82-
--> $DIR/explicit_auto_deref.rs:117:13
82+
--> $DIR/explicit_auto_deref.rs:119:13
8383
|
8484
LL | f5(&*x);
8585
| ^^ help: try this: `x`
8686

8787
error: deref which would be done by auto-deref
88-
--> $DIR/explicit_auto_deref.rs:118:13
88+
--> $DIR/explicit_auto_deref.rs:120:13
8989
|
9090
LL | f6(&*x);
9191
| ^^ help: try this: `x`
9292

9393
error: deref which would be done by auto-deref
94-
--> $DIR/explicit_auto_deref.rs:119:28
94+
--> $DIR/explicit_auto_deref.rs:121:28
9595
|
9696
LL | f7.callable_str()(&*x);
9797
| ^^ help: try this: `x`
9898

9999
error: deref which would be done by auto-deref
100-
--> $DIR/explicit_auto_deref.rs:120:26
100+
--> $DIR/explicit_auto_deref.rs:122:26
101101
|
102102
LL | f8.callable_t()(&*x);
103103
| ^^ help: try this: `x`
104104

105105
error: deref which would be done by auto-deref
106-
--> $DIR/explicit_auto_deref.rs:121:13
106+
--> $DIR/explicit_auto_deref.rs:123:13
107107
|
108108
LL | f9(&*x);
109109
| ^^ help: try this: `x`
110110

111111
error: deref which would be done by auto-deref
112-
--> $DIR/explicit_auto_deref.rs:122:14
112+
--> $DIR/explicit_auto_deref.rs:124:14
113113
|
114114
LL | f10(&*x);
115115
| ^^ help: try this: `x`
116116

117117
error: deref which would be done by auto-deref
118-
--> $DIR/explicit_auto_deref.rs:123:27
118+
--> $DIR/explicit_auto_deref.rs:125:27
119119
|
120120
LL | f11.callable_t()(&*x);
121121
| ^^ help: try this: `x`
122122

123123
error: deref which would be done by auto-deref
124-
--> $DIR/explicit_auto_deref.rs:127:17
124+
--> $DIR/explicit_auto_deref.rs:129:17
125125
|
126126
LL | let _ = S1(&*s);
127127
| ^^ help: try this: `s`
128128

129129
error: deref which would be done by auto-deref
130-
--> $DIR/explicit_auto_deref.rs:132:22
130+
--> $DIR/explicit_auto_deref.rs:134:22
131131
|
132132
LL | let _ = S2 { s: &*s };
133133
| ^^ help: try this: `s`
134134

135135
error: deref which would be done by auto-deref
136-
--> $DIR/explicit_auto_deref.rs:148:30
136+
--> $DIR/explicit_auto_deref.rs:150:30
137137
|
138138
LL | let _ = Self::S1(&**s);
139139
| ^^^^ help: try this: `s`
140140

141141
error: deref which would be done by auto-deref
142-
--> $DIR/explicit_auto_deref.rs:149:35
142+
--> $DIR/explicit_auto_deref.rs:151:35
143143
|
144144
LL | let _ = Self::S2 { s: &**s };
145145
| ^^^^ help: try this: `s`
146146

147147
error: deref which would be done by auto-deref
148-
--> $DIR/explicit_auto_deref.rs:152:21
148+
--> $DIR/explicit_auto_deref.rs:154:21
149149
|
150150
LL | let _ = E1::S1(&*s);
151151
| ^^ help: try this: `s`
152152

153153
error: deref which would be done by auto-deref
154-
--> $DIR/explicit_auto_deref.rs:153:26
154+
--> $DIR/explicit_auto_deref.rs:155:26
155155
|
156156
LL | let _ = E1::S2 { s: &*s };
157157
| ^^ help: try this: `s`

0 commit comments

Comments
 (0)