Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also consider mem::transmute with the invalid_reference_casting lint #114757

Merged

Conversation

Urgau
Copy link
Contributor

@Urgau Urgau commented Aug 12, 2023

This PR extend the invalid_reference_casting lint with regard to the std::mem::transmute function.

error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
  --> $DIR/reference_casting.rs:27:16
   |
LL |     let _num = &mut *std::mem::transmute::<_, *mut i32>(&num);
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I encourage anyone reviewing this PR to do so without whitespaces.

@rustbot
Copy link
Collaborator

rustbot commented Aug 12, 2023

r? @b-naber

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 12, 2023
@rust-log-analyzer

This comment has been minimized.

@Urgau Urgau force-pushed the transmute-with-invalid_reference_casting branch from 0be26d1 to b517dd5 Compare August 12, 2023 11:05
@est31
Copy link
Member

est31 commented Aug 12, 2023

Does this also detect if you cast to the mutable reference directly, like let _num = std::mem::transmute::<_, & mut i32>(&num)?

@Urgau
Copy link
Contributor Author

Urgau commented Aug 12, 2023

Does this also detect if you cast to the mutable reference directly, like let _num = std::mem::transmute::<_, & mut i32>(&num)?

No, but it's already covered by the mutable-transmutes lint.

@est31
Copy link
Member

est31 commented Aug 12, 2023

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Aug 12, 2023

📌 Commit b517dd5 has been approved by est31

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 12, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 12, 2023
…ence_casting, r=est31

Also consider `mem::transmute` with the `invalid_reference_casting` lint

This PR extend the `invalid_reference_casting` lint with regard to the `std::mem::transmute` function.

```
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
  --> $DIR/reference_casting.rs:27:16
   |
LL |     let _num = &mut *std::mem::transmute::<_, *mut i32>(&num);
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

*I encourage anyone reviewing this PR to do so [without whitespaces](https://github.blog/2011-10-21-github-secrets/#whitespace).*
@bors
Copy link
Contributor

bors commented Aug 13, 2023

⌛ Testing commit b517dd5 with merge 570601f...

@bors
Copy link
Contributor

bors commented Aug 13, 2023

☀️ Test successful - checks-actions
Approved by: est31
Pushing 570601f to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 13, 2023
@bors bors merged commit 570601f into rust-lang:master Aug 13, 2023
12 checks passed
@rustbot rustbot added this to the 1.73.0 milestone Aug 13, 2023
@Urgau Urgau deleted the transmute-with-invalid_reference_casting branch August 13, 2023 14:38
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (570601f): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.2% [3.0%, 3.4%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.6% [-2.6%, -2.6%] 1
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 634.77s -> 634.281s (-0.08%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants