rust-lang / rust-lang/rust-clippy

new lint: find unused method results (take '&mut self' into account)

Open
#3,061 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint S-needs-discussion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Currently, the rustc unused value warning seems to just look for LHS value that is not referenced anywhere else, like

let x = 0; // unused !
let y = 1;
println!("{}", y);

However there is no warning if we call an object method that modifies the object inplace, but we don't use the new object state afterwards:

let mut x = vec![1, 3, 2];
x.sort(); // here "x" is "used"
// here "x" is no longer used so technically we can omit the entire "x.sort()"
// but there is no warning for this :(

Maybe there could be a more advanced unused variable lint that checks if we call an object method taking &mut self without side effects and not returning anything without the object being ever referenced afterwards?

I'd love to have some sort of warning for the vec.sort() example.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing how rustc's unused value warning handles the shown let example, then compare it with the vec.sort() example involving &mut self. Define the lint's intended side-effect boundary and verify that it warns only when the mutated object is not referenced afterward, while avoiding methods with observable side effects.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.