Skip to content

Warning for unused arithmetic expressions #50124

Closed
@jonhoo

Description

@jonhoo

After having spent a day tracking down this bug, and recalling all the past times the same thing has bitten me, I'd like to propose a lint unused results of arithmetic expressions. Consider the following code:

let mut delta = 0isize;
match foo {
    Foo::Add(x) => {
        delta += x;
    }
    Foo::Sub(x) => {
        delta - x;
    }
}

This code currently compiles with no warnings (given a suitable enum Foo). However, bugs like these can be extremely hard to track down. They can often be hard to spot, especially when going through methods like saturating_sub which do not modify self, but instead return the new result (e.g., delta = delta.saturating_sub(x) is necessary).

It would be really neat to have a warning about unused arithmetic expressions (ideally one that also handles saturating_sub). I don't think there are generally cases where not using the result of an arithmetic expression is what the developer intended to do. This also complements the unused_variables and unused_mut lints quite nicely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions