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

Detect useless underscored variables with no side effects #7545

Closed
wooster0 opened this issue Aug 7, 2021 · 2 comments · Fixed by #7775
Closed

Detect useless underscored variables with no side effects #7545

wooster0 opened this issue Aug 7, 2021 · 2 comments · Fixed by #7775
Assignees
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy

Comments

@wooster0
Copy link

wooster0 commented Aug 7, 2021

What it does

It detects variables like let _unused = 1; and suggests deleting that variable because it serves 0% purpose and has no side effects whatsoever. This is different from let _unused_result = function(); which likely has a side effect (unless the function is empty/the function contents have no side effect, maybe that should be detected too? Probably not but I'm not sure).

See also: #7527 (comment)

Categories (optional)

  • Kind: clippy::suspicious.

It removes unused variables that serve no purpose.

One could argue that there are use cases for this like tests (for rustc I suppose) or examples merely showing off the ability to declare such variables, but one can of course always turn this lint off and in the most common case the user will actually want to remove this underscored variable with 0% side effects, I believe.

Drawbacks

  • The requirement to mute the lint, in very rare cases where the user wants this.

Example

fn main() {
    let _i_serve_no_purpose = 1;
}

Could be written as:

fn main() { }

If the variable is used afterwards then of course there should be no lint regardless of whether there is a side effect in the variable declaration itself or not.

@camsteffen
Copy link
Contributor

This can be an enhancement of no_effect.

@camsteffen camsteffen added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy and removed A-lint Area: New lints labels Aug 11, 2021
@F3real
Copy link
Contributor

F3real commented Oct 4, 2021

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants