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_to_owned false positive #8759

Closed
crumblingstatue opened this issue Apr 28, 2022 · 0 comments · Fixed by #8794
Closed

unnecessary_to_owned false positive #8759

crumblingstatue opened this issue Apr 28, 2022 · 0 comments · Fixed by #8794
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@crumblingstatue
Copy link

crumblingstatue commented Apr 28, 2022

Summary

#[derive(Default)]
struct View {
    
}

impl std::borrow::ToOwned for View {
	type Owned = View;
	fn to_owned(&self) -> Self::Owned {
		View {}
	}
}

#[derive(Default)]
struct RenderWindow {
    default_view: View,
}

impl RenderWindow {
    fn default_view(&self) -> &View {
        &self.default_view
    }
    fn set_view(&mut self, _view: &View) {
		
    }
}

fn main() {
	let mut rw = RenderWindow::default();
	rw.set_view(&rw.default_view().to_owned());
}

Clippy output:

warning: unnecessary use of `to_owned`
  --> src/main.rs:29:14
   |
29 |     rw.set_view(&rw.default_view().to_owned());
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `rw.default_view()`

If we apply the suggestion, rustc output:

error[E0502]: cannot borrow `rw` as mutable because it is also borrowed as immutable
  --> src/main.rs:29:2
   |
29 |     rw.set_view(rw.default_view());
   |     ^^^--------^-----------------^
   |     |  |        |
   |     |  |        immutable borrow occurs here
   |     |  immutable borrow later used by call
   |     mutable borrow occurs here

Version

rustc 1.62.0-nightly (69a5d2481 2022-04-27)
binary: rustc
commit-hash: 69a5d2481e856a5a18885390b8cf6950b9ff8dd3
commit-date: 2022-04-27
host: x86_64-unknown-linux-gnu
release: 1.62.0-nightly
LLVM version: 14.0.1
@crumblingstatue crumblingstatue added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Apr 28, 2022
@bors bors closed this as completed in 9c78883 May 7, 2022
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-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant