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 unnecessary literal casts #6116

Closed
euclio opened this issue Oct 5, 2020 · 8 comments
Closed

lint unnecessary literal casts #6116

euclio opened this issue Oct 5, 2020 · 8 comments
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@euclio
Copy link
Contributor

euclio commented Oct 5, 2020

What it does

Lints unnecessary as-casts of literals when they could be written using literal syntax.

Categories (optional)

  • Kind: clippy::style

What is the advantage of the recommended code over the original code

  • Avoids accidental truncation
  • Uses syntax dedicated to this purpose

Drawbacks

None.

Example

1 as u32

Could be written as:

1u32
@leonardo-m
Copy link

Better to suggest: 1_u32

@ebroto
Copy link
Member

ebroto commented Oct 6, 2020

This would be currently linted by as_conversions (playground). The docs imply it's allow-by-default because it has false positives.

The lint could be improved though to generate suggestions as the one you gave.

@ebroto ebroto added L-suggestion Lint: Improving, adding or fixing lint suggestions C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages and removed A-lint Area: New lints labels Oct 6, 2020
@euclio
Copy link
Contributor Author

euclio commented Oct 7, 2020

I think the lints serve different purposes. I 1 as u32 is almost certainly a mistake, while there are legitimate uses of as casts in general (which is why it's allow-by-default).

@flip1995
Copy link
Member

flip1995 commented Oct 9, 2020

Yes as_conversions is a restriction lint, with the design that it lints all as conversions, regardless what it converts.

@flip1995 flip1995 added good-first-issue These issues are a good way to get started with Clippy A-lint Area: New lints hacktoberfest and removed C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels Oct 9, 2020
@geoffreycopin
Copy link
Contributor

Hi, I'd be glad to give this a shot !

@geoffreycopin
Copy link
Contributor

geoffreycopin commented Oct 13, 2020

When testing my implementation, I noticed that there is some overlap with a previously existing lint that flags int literals that are casted to a float type.

Would it be suitable to just extend the aforementioned lint so that it become "casting number literal to <cast_to> is unnecessary" ? If not, what would be a good way to handle the overlap ?

@ebroto
Copy link
Member

ebroto commented Oct 13, 2020

@geoffreycopin I think what you suggest makes sense as that lint was already extended in #3842 to add the float cases.

Even if later there's a decision to not enhance that lint, your code should go in that module to be able to handle the overlap.

bors added a commit that referenced this issue Oct 24, 2020
Lint unnecessary int-to-int and float-to-float casts

This is an implementation of a lint that detects unnecessary casts of number literals, as discussed here:
#6116

---

changelog: lint unnecessary as-casts of literals when they could be written using literal syntax.
bors added a commit that referenced this issue Oct 24, 2020
Lint unnecessary int-to-int and float-to-float casts

This is an implementation of a lint that detects unnecessary casts of number literals, as discussed here:
#6116

---

changelog: lint unnecessary as-casts of literals when they could be written using literal syntax.
@ebroto
Copy link
Member

ebroto commented Oct 24, 2020

Fixed by #6187

@ebroto ebroto closed this as completed Oct 24, 2020
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 good-first-issue These issues are a good way to get started with Clippy L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

No branches or pull requests

5 participants