rust-lang / rust-lang/rust-clippy

`manual_clamp` misses when the upper bound is a function result

Open
#16,670 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-negative
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

manual_clamp doesn't trigger if one of the bounds being used is the return value from a function.

Applying this lint might change the meaning of the code if a function returns different results successive times it's called, but I think for a MaybeIncorrect lint it's reasonable to suggest this at least in cases where the function takes no mutable references and is given the same arguments.

Lint Name

manual_clamp

Reproducer

I tried this code:

fn upper_bound() -> f32 {
    5.0
}

pub fn manual_clamp(f: f32) -> f32 {
    if f < 0. { 0. } else if f > upper_bound() { upper_bound() } else { f }
}

I expected to see the manual_clamp lint trigger on the manual_clamp function.

Instead, this happened:

No lints

Version
rustc 1.94.0 (4a4ef493e 2026-03-02)
binary: rustc
commit-hash: 4a4ef493e3a1488c6e321570238084b38948f6db
commit-date: 2026-03-02
host: x86_64-unknown-linux-gnu
release: 1.94.0
LLVM version: 21.1.8

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 manual_clamp lint entry point and run the linked Rust Playground reproducer. Compare the lint's handling of a function result used as the upper bound, then add coverage for the reported case; done means the lint triggers without changing behavior for unsupported side-effecting calls.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.