rust-lang / rust-lang/rust-clippy

Possible more DRY suggestion for "as" cast

Open
#13,815 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-documentation C-enhancement
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Description

I am not sure if this is a good idea or if it can be done. But I think it's worth reporting here.

I'm using v.1.85 Nightly from the Playground. This code:

#![allow(dead_code)]
#![warn(clippy::all)]
#![warn(clippy::nursery)]
#![warn(clippy::pedantic)]

fn test1(x: u32) -> u16 {
    x as u16
}
fn main() {}

Gives:

warning: this could be a `const fn`
 --> src/main.rs:6:1
  |
6 | / fn test1(x: u32) -> u16 {
7 | |     x as u16
8 | | }
  | |_^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn
note: the lint level is defined here
 --> src/main.rs:3:9
  |
3 | #![warn(clippy::nursery)]
  |         ^^^^^^^^^^^^^^^
  = note: `#[warn(clippy::missing_const_for_fn)]` implied by `#[warn(clippy::nursery)]`
help: make the function `const`
  |
6 | const fn test1(x: u32) -> u16 {
  | +++++

warning: casting `u32` to `u16` may truncate the value
 --> src/main.rs:7:5
  |
7 |     x as u16
  |     ^^^^^^^^
  |
  = help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation
note: the lint level is defined here
 --> src/main.rs:4:9
  |
4 | #![warn(clippy::pedantic)]
  |         ^^^^^^^^^^^^^^^^
  = note: `#[warn(clippy::cast_possible_truncation)]` implied by `#[warn(clippy::pedantic)]`
help: ... or use `try_from` and handle the error accordingly
  |
7 |     u16::try_from(x)
  |

So replacing the "as" cast with try_from is safer, on the other hand, if the programmer wishes to keep the "as" cast, then an alternative more DRY (don't repeat yourself) way to write that code is:

#![allow(dead_code)]
#![warn(clippy::all)]
#![warn(clippy::nursery)]
#![warn(clippy::pedantic)]

fn test2(x: u32) -> u16 {
    x as _
}
fn main() {}

Sometimes it's better to write DRY code. It's also good for Clippy to show this syntax because it's less known than the common way to cast with "as".

Version

Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the cast_possible_truncation warning with the Rust examples in the issue, then inspect the lint's implementation and diagnostic behavior. Determine whether the requested as _ syntax belongs in the suggestion, and consider the relevant Clippy test coverage; done means the intended behavior and diagnostic wording are settled and verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.