Skip to content

New Lint: pointer_as_const_underscore #10587

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
blyxyas opened this issue Apr 2, 2023 · 4 comments
Closed

New Lint: pointer_as_const_underscore #10587

blyxyas opened this issue Apr 2, 2023 · 4 comments
Labels
A-lint Area: New lints

Comments

@blyxyas
Copy link
Member

blyxyas commented Apr 2, 2023

What it does

As far as I know, every usage of .as_ptr() as *const _ is overcomplicated, as it has a pointer (.as_ptr()) being casted to a pointer (as *const _). So as_ptr() as *const _ would be equal to .as_ptr() (Example of them being equal).

Even when they have the same result, ~19.1k files use this pattern.


Notes

  • Similar to add new lint as_underscore_ptr to check for as *{const,mut} _ #10567, this could cause some issues (as one targets as *const _ and the other one .as_ptr() as *const _)
  • I used the common .as_ptr() here as an example, but the lint would include any function returning a pointer.
  • While .as_ptr() as *const _ = .as_ptr(), it isn't equal to just as *const _.

Lint Name

pointer_as_const_underscore or as_ptr_as_const_underscore

Category

No response

Advantage

Cleaner code

Drawbacks

None that I can think of.

Example

let a = "a";
let a_ptr = a.as_ptr() as *const _;

Could be written as:

let a = "a";
let a_ptr = a.as_ptr();
@blyxyas blyxyas added the A-lint Area: New lints label Apr 2, 2023
@Jarcho
Copy link
Contributor

Jarcho commented Apr 2, 2023

Those are only the same if type inference doesn't pick a new type. e.g.

let a = "a";
let a: *const u8 = a.as_ptr() as *const _;

@Centri3
Copy link
Member

Centri3 commented May 14, 2023

Those are only the same if type inference doesn't pick a new type. e.g.

let a = "a";
let a: *const u8 = a.as_ptr() as *const _;

That could be written as let a = a.as_ptr().cast::<u8>(), though, which is much easier to understand (at least IMO).

in the case of changing mutability, something like let a = a.as_ptr().cast_mut() would be used instead (there is as_mut_ptr for this purpose, but let's ignore that for the purpose of this example)

The only place as *const (or its mutable sibling) makes sense really anywhere is when as_ptr isn't available or addr_of doesn't work, since it's temporary, so maybe this could be extended to as *const overall?

@rustbot claim

@Centri3
Copy link
Member

Centri3 commented May 14, 2023

so maybe this could be extended to as *const overall?

oh snap, just noticed this is already covered by #10567

@blyxyas
Copy link
Member Author

blyxyas commented Jul 26, 2023

Just noticed that as *const _ in #10567, could also catch a pointer.

@blyxyas blyxyas closed this as completed Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants