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

False positive in needless_pass_by_value on referenced fns #1939

Closed
llogiq opened this issue Aug 8, 2017 · 2 comments
Closed

False positive in needless_pass_by_value on referenced fns #1939

llogiq opened this issue Aug 8, 2017 · 2 comments
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-correctness Lint: Belongs in the correctness lint group

Comments

@llogiq
Copy link
Contributor

llogiq commented Aug 8, 2017

The lint does not take into account that the interface may be restricted to a
value type when the function is used as rvalues or in arguments.

Example:

fn validate<F: FnMut(String) -> bool>(_: F) { unimplemented!(); }

fn take_string_by_value(x: String) -> bool { x.is_empty() }

fn main() {
    validate(take_string_by_value);
}

lints with

warning: this argument is passed by value, but not consumed in the function body
 --> src/main.rs:3:28
  |
3 | fn take_string_by_value(x: String) -> bool { x.is_empty() }
  |                            ^^^^^^ help: consider changing the type to: `&str`
  |
  = note: #[warn(needless_pass_by_value)] on by default
  = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_pass_by_value

However, adding a & to the String leads to a compilation error. The solution would be to check for references (we likely need this functionality elsewhere, so we may have it already)

@llogiq llogiq added L-correctness Lint: Belongs in the correctness lint group E-medium Call for participation: Medium difficulty level problem and requires some initial experience. C-bug Category: Clippy is not doing the correct thing labels Aug 8, 2017
@oli-obk
Copy link
Contributor

oli-obk commented Aug 9, 2017

ugh... We need to check all use sites and technically the moment the function is public we need to bail out, too, because external users' code will be broken by it.

I think this will make the lint much less useful, but I also don't see how to improve it.

Note: the lint is already a breaking change, because you need to pass &foo instead of foo to the function.

Dear Rust,
please consider automatically inserting an intermediate closure for argument derefs
Yours
Oli

@dtolnay
Copy link
Member

dtolnay commented Nov 21, 2018

Closing as a duplicate of #2434 (I know, that one is newer but it has more 👍 so it is the one that searchers are landing on).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-correctness Lint: Belongs in the correctness lint group
Projects
None yet
Development

No branches or pull requests

3 participants