rust-lang / rust-lang/rust-clippy

False positive on clippy::borrowed_box forced by Mockall

Open
#5,770 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I tried this code:

#[mockall::automock]
trait Bar {
    fn foo(&self, value: Box<String>);
}

fn main() {
    let mut mock_bar = MockBar::default();
    mock_bar.expect_foo().with(mockall::predicate::function(|value: &Box<String>| **value == ""));
}

I expected clippy to give no warnings

Instead, this happened:

warning: you seem to be trying to use `&Box<T>`. Consider using just `&T`
 --> src\main.rs:8:69
  |
8 |     mock_bar.expect_foo().with(mockall::predicate::function(|value: &Box<String>| **value == ""));
  |                                                                     ^^^^^^^^^^^^ help: try: `&String`
  |
  = note: `#[warn(clippy::borrowed_box)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrowed_box

The Mockall library predicate uses &T where T is the type of a function's parameter. In this case T is Box so the type in the predicate's closure is forced to be &Box and can't be change to &String.

Meta
  • cargo clippy -V: clippy 0.0.212 (bb37a0f9 2020-06-16)
  • rustc -Vv:
rustc 1.44.1 (c7087fe00 2020-06-17)
binary: rustc
commit-hash: c7087fe00d2ba919df1d813c040a5d47e43b0fe7
commit-date: 2020-06-17
host: x86_64-pc-windows-msvc
release: 1.44.1
LLVM version: 9.0

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 provided Mockall reproducer and the borrowed_box lint, then run cargo clippy with the reported Rust version context to confirm the warning. Trace the lint's handling of closure parameter types and add a regression test if the relevant test location is found; done means this forced &Box<String> case no longer produces a false positive while ordinary borrowed-box cases remain covered.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.