Skip to content

New Lint: pointer_as_const_underscore #10587

Closed
@blyxyas

Description

@blyxyas

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();

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions