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

unnecessary_sort_by suggests broken code #5813

Closed
matthiaskrgr opened this issue Jul 17, 2020 · 1 comment
Closed

unnecessary_sort_by suggests broken code #5813

matthiaskrgr opened this issue Jul 17, 2020 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

I tried this code:

#[derive(Ord, PartialOrd, Eq, PartialEq)]
struct A<'a> {
    name: &'a str,
}

impl A<'_> {
    fn name(&self) -> &str {
        &self.name
    }
}

fn main() {
    let a = A { name: "" };
    let b = A { name: "" };
    let mut v = vec![a, b];
    v.sort_by(|a, b| a.name().cmp(b.name()))
}

Clippy suggested

warning: use Vec::sort_by_key here instead
  --> src/main.rs:16:5
   |
16 |     v.sort_by(|a, b| a.name().cmp(b.name()))
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `v.sort_by_key(|&a| a.name())`
   |
   = note: `#[warn(clippy::unnecessary_sort_by)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by

warning: 1 warning emitted

but the suggestion does not compile:

error[E0507]: cannot move out of a shared reference
  --> src/main.rs:16:20
   |
16 |     v.sort_by_key(|&a| a.name())
   |                    ^-
   |                    ||
   |                    |data moved here
   |                    |move occurs because `a` has type `A<'_>`, which does not implement the `Copy` trait
   |                    help: consider removing the `&`: `a`

error[E0515]: cannot return value referencing local variable `a`
  --> src/main.rs:16:24
   |
16 |     v.sort_by_key(|&a| a.name())
   |                        -^^^^^^^
   |                        |
   |                        returns a value referencing data owned by the current function
   |                        `a` is borrowed here

error: aborting due to 2 previous errors

Meta

  • cargo clippy -V: e.g. clippy 0.0.212 (346aec9 2020-07-11)
  • rustc -Vv:
    rustc 1.46.0-nightly (346aec9b0 2020-07-11)
    

binary: rustc
commit-hash: 346aec9
commit-date: 2020-07-11
host: x86_64-unknown-linux-gnu
release: 1.46.0-nightly
LLVM version: 10.0


@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Jul 17, 2020
@matthiaskrgr
Copy link
Member Author

Looks like #5754 :/

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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

1 participant