Skip to content

Commit f7d9fe5

Browse files
committed
Don't lint on auto-deref field access
1 parent 75cc92b commit f7d9fe5

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

clippy_lints/src/only_used_in_recursion.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,15 @@ impl<'tcx> LateLintPass<'tcx> for OnlyUsedInRecursion {
339339
e = parent;
340340
continue;
341341
},
342-
ExprKind::Field(..) | ExprKind::AddrOf(..) | ExprKind::Cast(..) => {
342+
ExprKind::AddrOf(..) | ExprKind::Cast(..) => {
343343
e = parent;
344344
continue;
345345
},
346+
// Only allow field accesses without auto-deref
347+
ExprKind::Field(..) if typeck.adjustments().get(child_id).is_none() => {
348+
e = parent;
349+
continue
350+
}
346351
_ => (),
347352
},
348353
_ => (),

tests/ui/only_used_in_recursion.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,25 @@ fn overwritten_param(flag: u32, mut a: usize) -> usize {
177177
} else {
178178
a = 5;
179179
}
180-
overwritten_param(flag, a)
180+
overwritten_param(flag - 1, a)
181+
}
182+
183+
fn field_direct(flag: u32, mut a: (usize,)) -> usize {
184+
if flag == 0 {
185+
0
186+
} else {
187+
a.0 += 5;
188+
field_direct(flag - 1, a)
189+
}
190+
}
191+
192+
fn field_deref(flag: u32, a: &mut Box<(usize,)>) -> usize {
193+
if flag == 0 {
194+
0
195+
} else {
196+
a.0 += 5;
197+
field_deref(flag - 1, a)
198+
}
181199
}
182200

183201
fn main() {}

tests/ui/only_used_in_recursion.stderr

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,22 @@ LL | fn overwritten_param(flag: u32, mut a: usize) -> usize {
234234
| ^ help: if this is intentional, prefix it with an underscore: `_a`
235235
|
236236
note: parameter used here
237-
--> $DIR/only_used_in_recursion.rs:180:29
237+
--> $DIR/only_used_in_recursion.rs:180:33
238238
|
239-
LL | overwritten_param(flag, a)
240-
| ^
239+
LL | overwritten_param(flag - 1, a)
240+
| ^
241241

242-
error: aborting due to 20 previous errors
242+
error: parameter is only used in recursion
243+
--> $DIR/only_used_in_recursion.rs:183:32
244+
|
245+
LL | fn field_direct(flag: u32, mut a: (usize,)) -> usize {
246+
| ^ help: if this is intentional, prefix it with an underscore: `_a`
247+
|
248+
note: parameter used here
249+
--> $DIR/only_used_in_recursion.rs:188:32
250+
|
251+
LL | field_direct(flag - 1, a)
252+
| ^
253+
254+
error: aborting due to 21 previous errors
243255

0 commit comments

Comments
 (0)