Skip to content
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

New lint: unnecessary_option #11612

Open
jubnzv opened this issue Oct 5, 2023 · 0 comments
Open

New lint: unnecessary_option #11612

jubnzv opened this issue Oct 5, 2023 · 0 comments
Labels
A-lint Area: New lints

Comments

@jubnzv
Copy link
Contributor

jubnzv commented Oct 5, 2023

What it does

This lint would warn against using the Option type for local variables when the None value is unused.

Advantage

Remove the unnecessary Options makes the source code more readable and effective.

Drawbacks

  • To make the lint more effective and simple, the implementation of the lint should not be exhaustive. It makes sense to iterate over HIR and mark an Option variable as "used" if it is used as an argument of a function call, i.e. don't propagate these values across functions.
  • The lint will warn only at the definition point of the local variable. Therefore, the user has to fix all the cases when the Some value is used by himself.

Example

fn test() {
  let a = Some(5);
  // Any statements here, but the `None` value of `a` is unused, e.g.:
  // if a.is_some() { /* ... */ }
  // if let Some(_) = a { /* ... */ }
}

Could be written as:

fn test() {
  let a = 5;
}
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

1 participant