rust-lang / rust-lang/rust-clippy
`needless_pass_by_value` FP that errors once actioned because of trait bounds (Bevy)
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
When using the Bevy ECS game engine, "systems" are defined as a function with parameters. Bevy also has wrapper types around & references: Res<T> (at least, that's how I understand it), that are used as parameters to systems.
The needless_pass_by_value lint is triggered by the Res<T> type; however, actioning the lint by making it &Res<T> will result in compilation errors because of the trait bounds on the functions that consume the function definition (in order to register the system), e.g. App::add_startup_system.
Lint Name
needless_pass_by_value
Reproducer
I tried this code:
Cargo.toml:
[package]
name = "untitled"
version = "0.1.0"
edition = "2021"
[dependencies]
bevy = "0.7.0"
main.rs:
#![warn(clippy::needless_pass_by_value)]
use bevy::prelude::*;
fn main() {
App::new().add_startup_system(spawn);
}
fn spawn(assets: Res<AssetServer>) {
let _: Handle<Mesh> = assets.load("");
}
I saw this happen:
Checking untitled v0.1.0 (D:\dev\git\untitled)
warning: this argument is passed by value, but not consumed in the function body
--> src\main.rs:9:18
|
9 | fn spawn(assets: Res<AssetServer>) {
| ^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&Res<AssetServer>`
|
note: the lint level is defined here
--> src\main.rs:1:9
|
1 | #![warn(clippy::needless_pass_by_value)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
warning: `untitled` (bin "untitled") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.53s
I expected to see this happen:
Checking untitled v0.1.0 (D:\dev\git\untitled)
Finished dev [unoptimized + debuginfo] target(s) in 0.54s
If I change Res<AssetServer> to &Res<AssetServer>:
error[E0277]: the trait bound `for<'r, 's> fn(&'r bevy::prelude::Res<'s, bevy::prelude::AssetServer>) {spawn}: bevy::prelude::IntoSystem<(), (), _>` is not satisfied
--> src\main.rs:6:35
|
6 | App::new().add_startup_system(spawn);
| ------------------ ^^^^^ the trait `bevy::prelude::IntoSystem<(), (), _>` is not implemented for `for<'r, 's> fn(&'r bevy::prelude::Res<'s, bevy::prelude::AssetServer>) {spawn}`
| |
| required by a bound introduced by this call
|
= note: required because of the requirements on the impl of `bevy::ecs::schedule::IntoSystemDescriptor<_>` for `for<'r, 's> fn(&'r bevy::prelude::Res<'s, bevy::prelude::AssetServer>) {spawn}`
note: required by a bound in `bevy::prelude::App::add_startup_system`
--> C:\Users\jdsmi\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_app-0.7.0\src\app.rs:436:22
|
436 | system: impl IntoSystemDescriptor<Params>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `bevy::prelude::App::add_startup_system`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `untitled` due to previous error
Version
rustc 1.61.0 (fe5b13d68 2022-05-18)
binary: rustc
commit-hash: fe5b13d681f25ee6474be29d748c65adcd91f69e
commit-date: 2022-05-18
host: x86_64-pc-windows-msvc
release: 1.61.0
LLVM version: 14.0.0
Additional Labels
@rustbot label +I-suggestion-causes-error
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the needless_pass_by_value lint implementation and its tests, then reproduce the report using the Cargo.toml and main.rs example with Bevy 0.7.0. Trace the suggested &Res<AssetServer> signature against App::add_startup_system and its IntoSystem bound; done means the lint no longer produces an action that fails to compile.
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
- 42/100