Skip to content

warning about unneeded unsafe functions is wrong #5954

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

Closed
thestinger opened this issue Apr 19, 2013 · 3 comments
Closed

warning about unneeded unsafe functions is wrong #5954

thestinger opened this issue Apr 19, 2013 · 3 comments
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@thestinger
Copy link
Contributor

It can't know that the function is safe because it doesn't require unsafe code. A function can break invariants in private data that are relied upon by unsafe blocks elsewhere in the module.

The unneeded unsafe block warning is great, but I think the function equivalent should be removed.

@alexcrichton

@alexcrichton
Copy link
Member

It's certainly easy enough to implement, does this come up very often? I'm having trouble thinking of concrete examples.

The connotation of unsafe to me means that it should be used as little as possible, and if you have some sort of invariant it would rather be wrapped in assert! instead of unsafe.

@thestinger
Copy link
Contributor Author

I don't think it comes up often, but poking a hole through the API seems like a valid use case for unsafe functions. For example an object might use a raw pointer internally assumed to be valid and allow setting the field to another value through an unsafe function. Obtaining a raw pointer and assigning it to a field is considered totally safe.

@pcwalton
Copy link
Contributor

The other use case for this is if you're implementing a function on a trait that is marked unsafe because an implementation for some other type needs to be unsafe, but this type implementation doesn't need to be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

3 participants