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

Lint to disallow using as for pointer casts in favor of .cast() #5890

Closed
retep998 opened this issue Aug 11, 2020 · 4 comments · Fixed by #6542
Closed

Lint to disallow using as for pointer casts in favor of .cast() #5890

retep998 opened this issue Aug 11, 2020 · 4 comments · Fixed by #6542
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@retep998
Copy link
Member

retep998 commented Aug 11, 2020

What it does

Any raw pointer cast from *const T to *const U or *mut T to *mut U that uses as should be caught by this lint. Instead you should use .cast().

Should probably be a pedantic lint.

Drawbacks

None.

Example

foo as *const c_void

Could be written as:

foo.cast()
@retep998 retep998 added the A-lint Area: New lints label Aug 11, 2020
@matthiaskrgr
Copy link
Member

There's a lint that seems to lint on as in general: https://rust-lang.github.io/rust-clippy/master/index.html#as_conversions

@retep998
Copy link
Member Author

Yes, and there are also lints for specific types of as casts. There is no lint for specifically pointer casts while still allowing other kinds of as casts.

@ErichDonGubler
Copy link
Contributor

This would seem to affect the transmutes_expressible_as_ptr_casts lint -- that link suggests using as, e.g., p as *const [u16];.

@flip1995 flip1995 added L-suggestion Lint: Improving, adding or fixing lint suggestions E-medium Call for participation: Medium difficulty level problem and requires some initial experience. labels Aug 16, 2020
@rail-rain
Copy link
Contributor

It seems to me this lint has already suggested twice (#4065 and #4708 (comment)). Maybe 4065 can be closed since 5890 has a bit more details.

@bors bors closed this as completed in a6b72d3 Jan 5, 2021
bors added a commit that referenced this issue Jan 19, 2021
Add a note to `as_conversions`

I have seen a couple of examples where there are some misunderstandings of `as_conversions` ([1](#5890 (comment)), [2](#6116 (comment)) and [3](#6428)). This PR adds the note that explains its purpose and relationship with other `as` related casts. Open question: should I list every related lints for discoverbility, or just suggest how to find these? I chose the former because there's no way to list only and all `as` related lints (e.g. on All the Clippt Lints, 'cast' includes some noises, but `cast_` excludes some) even though I cannot guarantee the list will be updated to include future changes.

---

changelog: Add a note to the document of `as_conversions`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants