Skip to content

suggest using if let Some(x) = arr.first() over if !arr.is_empty() then arr[0] #11858

Open
@J-ZhengLi

Description

@J-ZhengLi

What it does

Well, exactly as the title described, and could apply to whiles as well.

The idea is kinda like a combination of [unnecessary_unwrap] and [get_first].

Such lint might already exist, idk.

Advantage

Readability?

Drawbacks

maybe? maybe not?

Example

if !seq.is_empty() {
    let x = seq[0];
    // ...
}

Could be written as:

if let Some(x) = seq.first() {
    // ...
}

Metadata

Metadata

Assignees

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions