Skip to content

Add lint for & -> &mut transmutes #13146

Closed

Description

@huonw

Doing a &T to &mut T transmute is now undefined behaviour (it needs to use the Unsafe type), and so there could be a lint for it.

Unfortunately, such a lint is likely to miss many such casts, but it will theoretically catch common cases like transmute::<&Foo, &mut Foo>(self).

Activity

  1. nikomatsakis commented on Mar 26, 2014

    @nikomatsakis
    Contributor

    The proposed changes to intrinsics help here, because they wlil effectively forbid transmutes between types unless we can be sure of the size, which implies that one cannot transmute arbitrary types T to U and so on. We could even make these sorts of const casts illegal -- though somehow we have to make the Unsafe type able to do it. :)

  2. nikomatsakis commented on Mar 26, 2014

    @nikomatsakis
    Contributor

    Oh, hmm, the RFC doesn't have the interesting parts. Nm.

  3. emberian commented on Mar 28, 2014

    @emberian
    Contributor
  4. nikomatsakis commented on Mar 28, 2014

    @nikomatsakis
    Contributor

    Ah, the proposed changes to transmute are in #12898, not the RFC.

  5. steveklabnik commented on Apr 20, 2015

    @steveklabnik
    Contributor

    Triage: nothing has changed, these transmutes still silently work:

    use std::mem;
    
    fn main() {
        let x = 5;
    
        let y = &x;
    
        let z = unsafe { mem::transmute::<&i32, &mut i32>(y) };
    }
    
  6. added a commit that references this issue on Apr 22, 2015
    ae84d4f
  7. added 3 commits that reference this issue on Apr 29, 2015
    fadc86c
    6c30c6b
    2fdb714
  8. added a commit that references this issue on May 5, 2015
    89fa21b
  9. added a commit that references this issue on May 6, 2015
    d131f33
  10. added a commit that references this issue on May 6, 2015
    8767e97
  11. added a commit that references this issue on May 7, 2015
    103d02f
  12. added a commit that references this issue on Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions