rust-lang / rust-lang/rust-clippy

Clippy suggestion about borrowed_box when trait object is boxed is incomplete

Open
#11,940 0 comments 15 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

This follows from https://github.com/rust-lang/rust-clippy/issues/3971
and https://github.com/rust-lang/rust-clippy/issues/8161.

I think the message should suggest the .as_ref() method at the call site.

Reproducer

Clippy reported this:

warning: you seem to be trying to use `&Box<T>`. Consider using just `&T`
  --> advent2023-bin/src/main.rs:46:54
   |
46 |     let get_result_pair = move |day_num: usize, day: &Box<dyn DayTrait>| -> (String, String) {
   |                                                      ^^^^^^^^^^^^^^^^^^ help: try: `&dyn DayTrait`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrowed_box
   = note: `#[warn(clippy::borrowed_box)]` on by default

When fixed as indicated:

error[E0277]: the trait bound `Box<dyn DayTrait>: DayTrait` is not satisfied
  --> advent2023-bin/src/main.rs:63:76
   |
63 |             print_day(day_num, day.get_display(), get_result_pair(day_num, &day));
   |                                                                            ^^^^ the trait `DayTrait` is not implemented for `Box<dyn DayTrait>`
   |
   = help: the trait `DayTrait` is implemented for `Day<D, O, C, F, S>`
   = note: required for the cast from `&Box<dyn DayTrait>` to `&dyn DayTrait`

See minimum reproduction:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=88aa6d38369c9c8f48eabf5899ed65b0

Version
rustc 1.76.0-nightly (87e1447aa 2023-11-30)
binary: rustc
commit-hash: 87e1447aadaa2899ff6ccabe1fa669eb50fb60a1
commit-date: 2023-11-30
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.5
Additional Labels

No response

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 linked Rust Playground minimum reproduction and locate the borrowed_box lint in rust-clippy. Confirm the diagnostic for the boxed trait object, then update the lint’s test coverage so the call-site suggestion includes .as_ref().

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.