Skip to content

Lint for as _ casts #8847

Closed
Closed
@daprilik

Description

@daprilik

What it does

(Discussed in #8846)

A lint to warn against as _ conversions.

Lint Name

as_underscore

Category

style

Advantage

as _ is the Rust equivalent of C/C++'s implicit casting (albeit with a visual signal that some cast is indeed happening), and comes with many of the same problems.

e.g: consider a fn foo(n: usize) and a n: u16.
If foo is called as foo(n as _), and at some point, the function signature of foo changes to fn foo(n: u8), then the code would continue to compile just fine, albeit with a (likely unexpected) integer truncation.

Drawbacks

as _ is far more concise when the type being cast to is very wordy (but hence why this lint would live in the style category, and be disabled by default).

Example

fn foo(n: usize) {}
let n: u16 = 256;
foo(n as _);

Could be written as:

fn foo(n: usize) {}
let n: u16 = 256;
foo(n as u16);

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions