Skip to content

Commit 9ef4479

Browse files
committed
Auto merge of #5949 - rail-rain:fix_fp_borrow_interior_mutable_const, r=oli-obk
Fix fp in `borrow_interior_mutable_const` fixes #5796 changelog: fix false positive in `borrow_interior_mutable_const` when referencing a field behind a pointer.
2 parents 23db542 + ce8915c commit 9ef4479

File tree

3 files changed

+64
-18
lines changed

3 files changed

+64
-18
lines changed

clippy_lints/src/non_copy_const.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,21 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
211211
needs_check_adjustment = false;
212212
},
213213
ExprKind::Field(..) => {
214-
dereferenced_expr = parent_expr;
215214
needs_check_adjustment = true;
215+
216+
// Check whether implicit dereferences happened;
217+
// if so, no need to go further up
218+
// because of the same reason as the `ExprKind::Unary` case.
219+
if cx
220+
.typeck_results()
221+
.expr_adjustments(dereferenced_expr)
222+
.iter()
223+
.any(|adj| matches!(adj.kind, Adjust::Deref(_)))
224+
{
225+
break;
226+
}
227+
228+
dereferenced_expr = parent_expr;
216229
},
217230
ExprKind::Index(e, _) if ptr::eq(&**e, cur_expr) => {
218231
// `e[i]` => desugared to `*Index::index(&e, i)`,

tests/ui/borrow_interior_mutable_const.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![allow(clippy::declare_interior_mutable_const, clippy::ref_in_deref)]
33

44
use std::borrow::Cow;
5-
use std::cell::Cell;
5+
use std::cell::{Cell, UnsafeCell};
66
use std::fmt::Display;
77
use std::sync::atomic::{AtomicUsize, Ordering};
88
use std::sync::Once;
@@ -30,6 +30,37 @@ impl Trait<u32> for u64 {
3030
const ATOMIC: AtomicUsize = AtomicUsize::new(9);
3131
}
3232

33+
// This is just a pointer that can be safely dereferended,
34+
// it's semantically the same as `&'static T`;
35+
// but it isn't allowed to make a static reference from an arbitrary integer value at the moment.
36+
// For more information, please see the issue #5918.
37+
pub struct StaticRef<T> {
38+
ptr: *const T,
39+
}
40+
41+
impl<T> StaticRef<T> {
42+
/// Create a new `StaticRef` from a raw pointer
43+
///
44+
/// ## Safety
45+
///
46+
/// Callers must pass in a reference to statically allocated memory which
47+
/// does not overlap with other values.
48+
pub const unsafe fn new(ptr: *const T) -> StaticRef<T> {
49+
StaticRef { ptr }
50+
}
51+
}
52+
53+
impl<T> std::ops::Deref for StaticRef<T> {
54+
type Target = T;
55+
56+
fn deref(&self) -> &'static T {
57+
unsafe { &*self.ptr }
58+
}
59+
}
60+
61+
// use a tuple to make sure referencing a field behind a pointer isn't linted.
62+
const CELL_REF: StaticRef<(UnsafeCell<u32>,)> = unsafe { StaticRef::new(std::ptr::null()) };
63+
3364
fn main() {
3465
ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
3566
assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability
@@ -82,4 +113,6 @@ fn main() {
82113
assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
83114

84115
assert_eq!(NO_ANN.to_string(), "70"); // should never lint this.
116+
117+
let _ = &CELL_REF.0;
85118
}

tests/ui/borrow_interior_mutable_const.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: a `const` item with interior mutability should not be borrowed
2-
--> $DIR/borrow_interior_mutable_const.rs:34:5
2+
--> $DIR/borrow_interior_mutable_const.rs:65:5
33
|
44
LL | ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
55
| ^^^^^^
@@ -8,119 +8,119 @@ LL | ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
88
= help: assign this const to a local or static variable, and use the variable here
99

1010
error: a `const` item with interior mutability should not be borrowed
11-
--> $DIR/borrow_interior_mutable_const.rs:35:16
11+
--> $DIR/borrow_interior_mutable_const.rs:66:16
1212
|
1313
LL | assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability
1414
| ^^^^^^
1515
|
1616
= help: assign this const to a local or static variable, and use the variable here
1717

1818
error: a `const` item with interior mutability should not be borrowed
19-
--> $DIR/borrow_interior_mutable_const.rs:38:22
19+
--> $DIR/borrow_interior_mutable_const.rs:69:22
2020
|
2121
LL | let _once_ref = &ONCE_INIT; //~ ERROR interior mutability
2222
| ^^^^^^^^^
2323
|
2424
= help: assign this const to a local or static variable, and use the variable here
2525

2626
error: a `const` item with interior mutability should not be borrowed
27-
--> $DIR/borrow_interior_mutable_const.rs:39:25
27+
--> $DIR/borrow_interior_mutable_const.rs:70:25
2828
|
2929
LL | let _once_ref_2 = &&ONCE_INIT; //~ ERROR interior mutability
3030
| ^^^^^^^^^
3131
|
3232
= help: assign this const to a local or static variable, and use the variable here
3333

3434
error: a `const` item with interior mutability should not be borrowed
35-
--> $DIR/borrow_interior_mutable_const.rs:40:27
35+
--> $DIR/borrow_interior_mutable_const.rs:71:27
3636
|
3737
LL | let _once_ref_4 = &&&&ONCE_INIT; //~ ERROR interior mutability
3838
| ^^^^^^^^^
3939
|
4040
= help: assign this const to a local or static variable, and use the variable here
4141

4242
error: a `const` item with interior mutability should not be borrowed
43-
--> $DIR/borrow_interior_mutable_const.rs:41:26
43+
--> $DIR/borrow_interior_mutable_const.rs:72:26
4444
|
4545
LL | let _once_mut = &mut ONCE_INIT; //~ ERROR interior mutability
4646
| ^^^^^^^^^
4747
|
4848
= help: assign this const to a local or static variable, and use the variable here
4949

5050
error: a `const` item with interior mutability should not be borrowed
51-
--> $DIR/borrow_interior_mutable_const.rs:52:14
51+
--> $DIR/borrow_interior_mutable_const.rs:83:14
5252
|
5353
LL | let _ = &ATOMIC_TUPLE; //~ ERROR interior mutability
5454
| ^^^^^^^^^^^^
5555
|
5656
= help: assign this const to a local or static variable, and use the variable here
5757

5858
error: a `const` item with interior mutability should not be borrowed
59-
--> $DIR/borrow_interior_mutable_const.rs:53:14
59+
--> $DIR/borrow_interior_mutable_const.rs:84:14
6060
|
6161
LL | let _ = &ATOMIC_TUPLE.0; //~ ERROR interior mutability
6262
| ^^^^^^^^^^^^
6363
|
6464
= help: assign this const to a local or static variable, and use the variable here
6565

6666
error: a `const` item with interior mutability should not be borrowed
67-
--> $DIR/borrow_interior_mutable_const.rs:54:19
67+
--> $DIR/borrow_interior_mutable_const.rs:85:19
6868
|
6969
LL | let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR interior mutability
7070
| ^^^^^^^^^^^^
7171
|
7272
= help: assign this const to a local or static variable, and use the variable here
7373

7474
error: a `const` item with interior mutability should not be borrowed
75-
--> $DIR/borrow_interior_mutable_const.rs:55:14
75+
--> $DIR/borrow_interior_mutable_const.rs:86:14
7676
|
7777
LL | let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
7878
| ^^^^^^^^^^^^
7979
|
8080
= help: assign this const to a local or static variable, and use the variable here
8181

8282
error: a `const` item with interior mutability should not be borrowed
83-
--> $DIR/borrow_interior_mutable_const.rs:56:13
83+
--> $DIR/borrow_interior_mutable_const.rs:87:13
8484
|
8585
LL | let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR interior mutability
8686
| ^^^^^^^^^^^^
8787
|
8888
= help: assign this const to a local or static variable, and use the variable here
8989

9090
error: a `const` item with interior mutability should not be borrowed
91-
--> $DIR/borrow_interior_mutable_const.rs:62:13
91+
--> $DIR/borrow_interior_mutable_const.rs:93:13
9292
|
9393
LL | let _ = ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
9494
| ^^^^^^^^^^^^
9595
|
9696
= help: assign this const to a local or static variable, and use the variable here
9797

9898
error: a `const` item with interior mutability should not be borrowed
99-
--> $DIR/borrow_interior_mutable_const.rs:67:5
99+
--> $DIR/borrow_interior_mutable_const.rs:98:5
100100
|
101101
LL | CELL.set(2); //~ ERROR interior mutability
102102
| ^^^^
103103
|
104104
= help: assign this const to a local or static variable, and use the variable here
105105

106106
error: a `const` item with interior mutability should not be borrowed
107-
--> $DIR/borrow_interior_mutable_const.rs:68:16
107+
--> $DIR/borrow_interior_mutable_const.rs:99:16
108108
|
109109
LL | assert_eq!(CELL.get(), 6); //~ ERROR interior mutability
110110
| ^^^^
111111
|
112112
= help: assign this const to a local or static variable, and use the variable here
113113

114114
error: a `const` item with interior mutability should not be borrowed
115-
--> $DIR/borrow_interior_mutable_const.rs:81:5
115+
--> $DIR/borrow_interior_mutable_const.rs:112:5
116116
|
117117
LL | u64::ATOMIC.store(5, Ordering::SeqCst); //~ ERROR interior mutability
118118
| ^^^^^^^^^^^
119119
|
120120
= help: assign this const to a local or static variable, and use the variable here
121121

122122
error: a `const` item with interior mutability should not be borrowed
123-
--> $DIR/borrow_interior_mutable_const.rs:82:16
123+
--> $DIR/borrow_interior_mutable_const.rs:113:16
124124
|
125125
LL | assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
126126
| ^^^^^^^^^^^

0 commit comments

Comments
 (0)