Skip to content

Commit 969e514

Browse files
committed
needless_borrow: do not contradict dangerous_implicit_autorefs
Rust 1.88 introduces the `dangerous_implicit_autorefs` lint which warns about using implicit autorefs on a place obtained from a raw pointer, as this may create aliasing issues. Prevent `clippy::needless_borrow` from triggering in this case, by disabling the lint when taking a reference on a raw pointer dereference. There might be a better way for doing this in the long run with a finer way of distinguish the problematic cases, but this will prevent Clippy from contradicting the compiler in the meantime.
1 parent 40bead0 commit 969e514

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed

clippy_lints/src/dereference.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,15 @@ fn report<'tcx>(
997997
);
998998
},
999999
State::DerefedBorrow(state) => {
1000+
// Do not suggest removing a non-mandatory `&` in `&*rawptr` in an `unside` context,
1001+
// as this may make rustc trigger its `dangerous_implicit_autorefs` lint.
1002+
if let ExprKind::AddrOf(BorrowKind::Ref, _, subexpr) = data.first_expr.kind
1003+
&& let ExprKind::Unary(UnOp::Deref, subsubexpr) = subexpr.kind
1004+
&& cx.typeck_results().expr_ty_adjusted(subsubexpr).is_raw_ptr()
1005+
{
1006+
return;
1007+
}
1008+
10001009
let mut app = Applicability::MachineApplicable;
10011010
let (snip, snip_is_macro) =
10021011
snippet_with_context(cx, expr.span, data.first_expr.span.ctxt(), "..", &mut app);

tests/ui/needless_borrow.fixed

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ fn main() {
107107
let x = (1, 2);
108108
let _ = x.0;
109109
//~^ needless_borrow
110-
let x = &x as *const (i32, i32);
111-
let _ = unsafe { (*x).0 };
112-
//~^ needless_borrow
113110

114111
// Issue #8367
115112
trait Foo {
@@ -289,3 +286,15 @@ fn issue_12268() {
289286

290287
// compiler
291288
}
289+
290+
fn issue_14743<T>(slice: &[T]) {
291+
let _ = slice.len();
292+
//~^ needless_borrow
293+
294+
let slice = slice as *const [T];
295+
let _ = unsafe { (&*slice).len() };
296+
297+
// Check that rustc would actually warn if Clippy had suggested removing the reference
298+
#[expect(dangerous_implicit_autorefs)]
299+
let _ = unsafe { (*slice).len() };
300+
}

tests/ui/needless_borrow.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ fn main() {
107107
let x = (1, 2);
108108
let _ = (&x).0;
109109
//~^ needless_borrow
110-
let x = &x as *const (i32, i32);
111-
let _ = unsafe { (&*x).0 };
112-
//~^ needless_borrow
113110

114111
// Issue #8367
115112
trait Foo {
@@ -289,3 +286,15 @@ fn issue_12268() {
289286

290287
// compiler
291288
}
289+
290+
fn issue_14743<T>(slice: &[T]) {
291+
let _ = (&slice).len();
292+
//~^ needless_borrow
293+
294+
let slice = slice as *const [T];
295+
let _ = unsafe { (&*slice).len() };
296+
297+
// Check that rustc would actually warn if Clippy had suggested removing the reference
298+
#[expect(dangerous_implicit_autorefs)]
299+
let _ = unsafe { (*slice).len() };
300+
}

tests/ui/needless_borrow.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,71 +103,71 @@ error: this expression borrows a value the compiler would automatically borrow
103103
LL | let _ = (&x).0;
104104
| ^^^^ help: change this to: `x`
105105

106-
error: this expression borrows a value the compiler would automatically borrow
107-
--> tests/ui/needless_borrow.rs:111:22
108-
|
109-
LL | let _ = unsafe { (&*x).0 };
110-
| ^^^^^ help: change this to: `(*x)`
111-
112106
error: this expression creates a reference which is immediately dereferenced by the compiler
113-
--> tests/ui/needless_borrow.rs:122:5
107+
--> tests/ui/needless_borrow.rs:119:5
114108
|
115109
LL | (&&()).foo();
116110
| ^^^^^^ help: change this to: `(&())`
117111

118112
error: this expression creates a reference which is immediately dereferenced by the compiler
119-
--> tests/ui/needless_borrow.rs:132:5
113+
--> tests/ui/needless_borrow.rs:129:5
120114
|
121115
LL | (&&5).foo();
122116
| ^^^^^ help: change this to: `(&5)`
123117

124118
error: this expression creates a reference which is immediately dereferenced by the compiler
125-
--> tests/ui/needless_borrow.rs:159:23
119+
--> tests/ui/needless_borrow.rs:156:23
126120
|
127121
LL | let x: (&str,) = (&"",);
128122
| ^^^ help: change this to: `""`
129123

130124
error: this expression borrows a value the compiler would automatically borrow
131-
--> tests/ui/needless_borrow.rs:202:13
125+
--> tests/ui/needless_borrow.rs:199:13
132126
|
133127
LL | (&self.f)()
134128
| ^^^^^^^^^ help: change this to: `(self.f)`
135129

136130
error: this expression borrows a value the compiler would automatically borrow
137-
--> tests/ui/needless_borrow.rs:212:13
131+
--> tests/ui/needless_borrow.rs:209:13
138132
|
139133
LL | (&mut self.f)()
140134
| ^^^^^^^^^^^^^ help: change this to: `(self.f)`
141135

142136
error: this expression borrows a value the compiler would automatically borrow
143-
--> tests/ui/needless_borrow.rs:250:22
137+
--> tests/ui/needless_borrow.rs:247:22
144138
|
145139
LL | let _ = &mut (&mut { x.u }).x;
146140
| ^^^^^^^^^^^^^^ help: change this to: `{ x.u }`
147141

148142
error: this expression borrows a value the compiler would automatically borrow
149-
--> tests/ui/needless_borrow.rs:258:22
143+
--> tests/ui/needless_borrow.rs:255:22
150144
|
151145
LL | let _ = &mut (&mut { x.u }).x;
152146
| ^^^^^^^^^^^^^^ help: change this to: `{ x.u }`
153147

154148
error: this expression borrows a value the compiler would automatically borrow
155-
--> tests/ui/needless_borrow.rs:263:22
149+
--> tests/ui/needless_borrow.rs:260:22
156150
|
157151
LL | let _ = &mut (&mut x.u).x;
158152
| ^^^^^^^^^^ help: change this to: `x.u`
159153

160154
error: this expression borrows a value the compiler would automatically borrow
161-
--> tests/ui/needless_borrow.rs:265:22
155+
--> tests/ui/needless_borrow.rs:262:22
162156
|
163157
LL | let _ = &mut (&mut { x.u }).x;
164158
| ^^^^^^^^^^^^^^ help: change this to: `{ x.u }`
165159

166160
error: this expression creates a reference which is immediately dereferenced by the compiler
167-
--> tests/ui/needless_borrow.rs:287:23
161+
--> tests/ui/needless_borrow.rs:284:23
168162
|
169163
LL | option.unwrap_or((&x.0,));
170164
| ^^^^ help: change this to: `x.0`
171165

166+
error: this expression creates a reference which is immediately dereferenced by the compiler
167+
--> tests/ui/needless_borrow.rs:291:13
168+
|
169+
LL | let _ = (&slice).len();
170+
| ^^^^^^^^ help: change this to: `slice`
171+
172172
error: aborting due to 28 previous errors
173173

0 commit comments

Comments
 (0)