Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add a lint for Option::take on a temporary #8618

Closed
scottmcm opened this issue Apr 1, 2022 · 1 comment · Fixed by #8665
Closed

Add a lint for Option::take on a temporary #8618

scottmcm opened this issue Apr 1, 2022 · 1 comment · Fixed by #8665
Assignees
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-complexity Lint: Belongs in the complexity lint group

Comments

@scottmcm
Copy link
Member

scottmcm commented Apr 1, 2022

What it does

(Inspired by Take() vs as_ref().take() on URLO.)

Lint when calling Option::take on a temporary, as when you have an owned temporary there's no need to take it -- you already have ownership, and you won't be able to see the None left behind, so the take can just be omitted.

Lint Name

option_take_on_temporary

Category

suspicious, complexity

Advantage

  • Shorter code
  • Less confusing to to reader

Drawbacks

None, so long as the temporary detection is accurate.

Example

fn main() {
    let x = Some(2);
    let y = x.as_ref().take();
}

Could be written as:

fn main() {
    let x = Some(2);
    let y = x.as_ref();
}
@scottmcm scottmcm added the A-lint Area: New lints label Apr 1, 2022
@b-ncMN
Copy link
Contributor

b-ncMN commented Apr 4, 2022

@rustbot claim

@flip1995 flip1995 added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-complexity Lint: Belongs in the complexity lint group labels Apr 6, 2022
@bors bors closed this as completed in e5ebece Apr 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-complexity Lint: Belongs in the complexity lint group
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants