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 for as _ casts #8847

Closed
daprilik opened this issue May 19, 2022 · 5 comments · Fixed by #8934
Closed

Lint for as _ casts #8847

daprilik opened this issue May 19, 2022 · 5 comments · Fixed by #8934
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy

Comments

@daprilik
Copy link

daprilik commented May 19, 2022

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);
@daprilik daprilik added the A-lint Area: New lints label May 19, 2022
@xFrednet
Copy link
Member

Thank you for the ticket, I would put this lint in the restriction group like clippy::as_conversions that would make it allow by default. The group also sets the lint level, having it in the style group would make this allow by default, which we probably don't want 🙃

The implementation can be based on clippy::as_conversions and probably reuse a bunch of code.

@rustbot label +good-first-issue

@rustbot rustbot added the good-first-issue These issues are a good way to get started with Clippy label May 19, 2022
@ghost
Copy link

ghost commented May 20, 2022

It's not just as _ that's problematic but as as a whole. Even if you used as usize it's possible that the type of n could change to u128 and now you have that same situation. This is why you should use Into instead whenever possible because it guarantees that the conversion is not lossy. I would recommend you use cast_lossless instead.

@daprilik
Copy link
Author

daprilik commented May 20, 2022

@mikerite I generally agree with you that as casts should be avoided when possible, and I'm well aware of their pitfalls. That said, while your point is certainly valid, it's not an argument against adding a new lint here.

For one reason or another, I doubt I'd be able to remove all as lints from the codebase I'm interested in, hence why I'm interested in this kind of "middle ground" lint, where folks can still use as, but are forced to be a bit more mindful about the specific type being cast to.

@DevAccentor
Copy link
Contributor

@rustbot claim

@daprilik
Copy link
Author

daprilik commented Jun 6, 2022

Thanks @DevAccentor 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants