rust-lang / rust-lang/rust-clippy

Should the `mut_from_ref` lint apply to unsafe functions?

Open
#4,281 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

pub struct MutUnsafeSlice<'s>(&'s mut [u32]);
struct Foo {}
impl Foo {
    pub unsafe fn foo(&self) -> &mut [u32] {
        panic!()
    }
    pub unsafe fn foo1(&self) -> MutUnsafeSlice {
        panic!()
    }
}
fn main() {}

playground

Should mut_from_ref apply to unsafe functions as well?

It is trivial to work around that error by wrapping the mutable reference in a new struct.

Here is the refcell implementation that does something very similar.

I personally don't have any strong opinions in either direction. I just needed to implement something similar to RefCell (runtime borrowing) and I ran into this lint.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the provided Rust snippet in the linked Playground and observe how the mut_from_ref lint treats the unsafe functions. Read the linked RefCell implementation for the analogous pattern, then decide whether unsafe functions should be diagnosed and capture the decision in an appropriate regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.