rust-lang / rust-lang/rust-clippy

Lint on use of `.into()` from `T` to `Option<T>`

Open
#11,950 1 comment 0 reactions 1 assignee View on GitHub

@TennyZhuang is already working on this.

Since Dec 18, 2023.

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

Description

What it does

Flags all usage of .into() to wrap a value in Some and turn it into an Option.

Advantage

I've found myself wanting to avoid the use of .into() to convert from T to Option<T>. It feels like it decreases readability, because it's a blanket impl, and .into() could just as easily be converting into a completely different type that's specific to T. Given that, I'd like to avoid such usage in favor of writing Some explicitly instead. I'd love to have a lint that helps me do that.

I'd expect this to be a style or restriction lint, allow-by-default, since this is a matter of taste and style rather than something universal that everyone would want.

Drawbacks

This is a lint enforcing developer style, which would only be used if explicitly allowed, so I wouldn't expect any drawbacks for those using it; it should produce what developers expect.

Example
struct S(Option<u64>);
let x = 42;
let y = S(x.into());

Could be written as:

struct S(Option<u64>);
let x = 42;
let y = S(Some(x));

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.