Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clippy needless_pass_by_value false positive #283

Open
DaAlbrecht opened this issue Feb 24, 2025 · 2 comments
Open

Clippy needless_pass_by_value false positive #283

DaAlbrecht opened this issue Feb 24, 2025 · 2 comments
Labels
A-Linter Related to the linter and custom lints C-Feature Make something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples X-Contentious There are nontrivial implications that should be thought through

Comments

@DaAlbrecht
Copy link
Collaborator

The getting-started example from bevy looks like this

use bevy::prelude::*;

#[derive(Component)]
struct Person;

#[derive(Component)]
struct Name(String);

fn main() {
    App::new().add_systems(Update, greet_people).run();
}

fn greet_people(query: Query<&Name, With<Person>>) {
    for name in &query {
        println!("hello {}!", name.0);
    }
}

resulting in the following false positive of the pedantic needless-pass-by-value clippy lint.

warning: this argument is passed by value, but not consumed in the function body
  --> src/main.rs:13:24
   |
13 | fn greet_people(query: Query<&Name, With<Person>>) {
   |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&Query<&Name, With<Person>>`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
   = note: `-W clippy::needless-pass-by-value` implied by `-W clippy::pedantic`
   = help: to override `-W clippy::pedantic` add `#[allow(clippy::needless_pass_by_value)]`

If you follow the advice you get the following

use bevy::prelude::*;

#[derive(Component)]
struct Person;

#[derive(Component)]
struct Name(String);

fn main() {
    App::new().add_systems(Update, greet_people).run();
}

fn greet_people(query: &Query<&Name, With<Person>>) {
    for name in &query {
        println!("hello {}!", name.0);
    }
}
error[E0277]: `fn(&Query<'b, 'c, &Name, With<Person>>) {greet_people}` does not describe a valid system configuration
   --> src/main.rs:10:36
    |
10  |     App::new().add_systems(Update, greet_people).run();
    |                -----------         ^^^^^^^^^^^^ invalid system configuration
    |                |
    |                required by a bound introduced by this call
    |
    = note: the full name for the type has been written to '/private/tmp/demo_lint/target/debug/deps/demo_lint-3b418e1522fada65.long-type-10272769760713719372.txt'
    = note: consider using `--verbose` to print the full type name to the console
    = help: the trait `IntoSystem<(), (), _>` is not implemented for fn item `fn(&Query<'b, 'c, &Name, With<Person>>) {greet_people}`
    = help: the following other types implement trait `IntoSystem<In, Out, Marker>`:
              `IntoAdapterSystem<Func, S>` implements `IntoSystem<<Func as Adapt<<S as IntoSystem<I, O, M>>::System>>::In, <Func as Adapt<<S as IntoSystem<I, O, M>>::System>>::Out, (IsAdapterSystemMarker, I, O, M)>`
              `IntoPipeSystem<A, B>` implements `IntoSystem<IA, OB, (IsPipeSystemMarker, OA, IB, MA, MB)>`
    = note: required for `fn(&Query<'b, 'c, &Name, With<Person>>) {greet_people}` to implement `IntoSystemConfigs<_>`
note: required by a bound in `bevy::prelude::App::add_systems`

While this is only a pedantic lint I still think it's a great one and I have already seen multiple posts about bevy and this lint as shown here: rust-lang/rust-clippy#8940

@DaAlbrecht DaAlbrecht added the A-Linter Related to the linter and custom lints label Feb 24, 2025
@BD103
Copy link
Member

BD103 commented Feb 24, 2025

For context, please see this comment: #276 (comment)

We can likely copy Clippy's needless_pass_by_value code to implement this lint. To make it work for Bevy-specific types, we should modify it to not emit a lint if the parameter implements SystemParam.

I'm not sure I like the idea of completely ripping the lint and being forced to maintain it ourselves, but I also can't see rust-lang/rust-clippy#8940 reasonably being being fixed, as it would have to check every single place each function is referenced.

@DaAlbrecht
Copy link
Collaborator Author

For context, please see this comment: #276 (comment)

We can likely copy Clippy's needless_pass_by_value code to implement this lint. To make it work for Bevy-specific types, we should modify it to not emit a lint if the parameter implements SystemParam.

I'm not sure I like the idea of completely ripping the lint and being forced to maintain it ourselves, but I also can't see rust-lang/rust-clippy#8940 reasonably being being fixed, as it would have to check every single place each function is referenced.

Being forced to maintain the lint ourselves is kinda unfortunate but it would at least be a pretty easy fix. But since its only of the category pedantic its completely fine for me if we don't do it.

@BD103 BD103 added C-Feature Make something new possible X-Contentious There are nontrivial implications that should be thought through D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Related to the linter and custom lints C-Feature Make something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples X-Contentious There are nontrivial implications that should be thought through
Projects
Status: Todo
Development

No branches or pull requests

2 participants