Skip to content

New Lint: function argument only used in recursion #8390

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

Closed
buttercrab opened this issue Feb 3, 2022 · 2 comments · Fixed by #8422
Closed

New Lint: function argument only used in recursion #8390

buttercrab opened this issue Feb 3, 2022 · 2 comments · Fixed by #8422
Assignees
Labels
A-lint Area: New lints

Comments

@buttercrab
Copy link
Contributor

buttercrab commented Feb 3, 2022

What it does

Arguments that are used only in recursion to itself.
Since it is used, the warning unused_variables does not come out, but it is useless.

Lint Name

only_used_in_recursion

Category

perf

Advantage

  • Lowers the complexity of the function.
  • Better performance when the useless argument have some operation (like below example)

Drawbacks

No response

Example

fn f(a: usize, b: usize) -> usize {
    if a == 0 {
        1
    } else {
        f(a - 1, b + 1)
    }
}

fn main() {
    print!("{}", f(1, 1));
}

Could be written as:

fn f(a: usize) -> usize {
    if a == 0 {
        1
    } else {
        f(a - 1)
    }
}

fn main() {
    print!("{}", f(1));
}
@buttercrab buttercrab added the A-lint Area: New lints label Feb 3, 2022
@buttercrab
Copy link
Contributor Author

Can I claim this if it looks good?

@buttercrab
Copy link
Contributor Author

@rustbot claim

bors added a commit that referenced this issue Mar 12, 2022
new lint: `only_used_in_recursion`

changelog:
- added `only_used_in_recursion`.
- fixed code that variables are only used in recursion.
- this would not lint when `unused_variable`

Issue: #8390
bors added a commit that referenced this issue Mar 13, 2022
new lint: `only_used_in_recursion`

changed:
- added `only_used_in_recursion`.
- fixed code that variables are only used in recursion.
- this would not lint when `unused_variable`

This fixes: #8390

-----

changelog: add lint [`only_used_in_recursion`]
@bors bors closed this as completed in e2e492c Mar 13, 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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant