Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
J-ZhengLi opened this issue Nov 23, 2023 · 1 comment · May be fixed by #14058
Open

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

J-ZhengLi opened this issue Nov 23, 2023 · 1 comment · May be fixed by #14058
Assignees
Labels
A-lint Area: New lints

Comments

@J-ZhengLi
Copy link
Member

J-ZhengLi commented Nov 23, 2023

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() {
    // ...
}
@J-ZhengLi J-ZhengLi added the A-lint Area: New lints label Nov 23, 2023
@Jacherr
Copy link
Contributor

Jacherr commented Mar 10, 2024

@rustbot claim

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-lint Area: New lints
Projects
None yet
2 participants