rust-lang / rust-lang/rust-clippy

Detect potentially mis-written out param functions

Open
#11,658 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What it does

(Feel free to adjust the title and description, I may have worded things wrong).

There is a significant difference between writing:

fn outparams(mut param: f64) {}

fn outparams(param: &mut f64) {}

but because both of these are valid code, it is easy to mistakenly write the wrong one.

It would be nice if clippy (or eventually even rustc) could detect cases where there's likely a mistake - for example when the function is pure and ends up doing nothing, something is likely to be amiss.

If this issue is accepted, I would love to learn how to contribute a fix for this 🧡

Advantage

This lint warning could detect mistakes that are easy to make and flag potentially-incorrect code for the users.

Drawbacks
  • Risk of false positives would probably depend on the implementation method - I don't know enough about this yet.
  • Perhaps there is a potential future lint (even if optional) that would discourage the use of out params entirely, and following the instructions of this lint would lead to another lint warn/error?
Example

I ran into this problem while directly translating some C code into Rust. I was confused about why I was always getting the same output regardless of the input, and it took me a surprisingly long time to find my error.

// bad code; this doesn't actually do anything!            vvv here
pub fn spectrum_apply_3x2(matrix: [f64; 6], src: [f64; 2], mut tgt: [f64; 2]) {
    tgt[0] = matrix[0] * src[0] + matrix[1] * src[1] + matrix[2];
    tgt[1] = matrix[3] * src[0] + matrix[4] * src[1] + matrix[5];
}
// the correct version                                          vvvv here
pub fn spectrum_apply_3x2(matrix: [f64; 6], src: [f64; 2], tgt: &mut [f64; 2]) {
    tgt[0] = matrix[0] * src[0] + matrix[1] * src[1] + matrix[2];
    tgt[1] = matrix[3] * src[0] + matrix[4] * src[1] + matrix[5];
}

(There were a lot of functions with out params like this in the larger context, so please don't nitpick the specific function body, or whether I should've used a library method instead, etc. This is just one of the smallest functions in the file, and serves as a good illustration of the issue)

Playground link - currently neither rustc or clippy produce any warnings for the bad code.

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 with the issue's Rust examples and run the linked Playground to reproduce the absence of warnings for the value parameter case. Then inspect Clippy's existing lint structure and tests to determine where this proposed detection belongs. Done means a documented lint behavior distinguishes likely mistaken out-parameter declarations while accounting for the false-positive concerns raised in the issue.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.