-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide borrow-instead-of-move suggestions for calls of fn-like items…
… from other crates This also downgrades its applicability to MaybeIncorrect. Its suggestion can result in ill-typed code when the type parameter it suggests providing a different generic argument for appears elsewhere in the callee's signature or predicates.
- Loading branch information
Showing
5 changed files
with
170 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tests/ui/moves/auxiliary/suggest-borrow-for-generic-arg-aux.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//! auxiliary definitons for suggest-borrow-for-generic-arg.rs, to ensure the suggestion works on | ||
//! functions defined in other crates. | ||
use std::borrow::{Borrow, BorrowMut}; | ||
use std::convert::{AsMut, AsRef}; | ||
pub struct Bar; | ||
|
||
impl AsRef<Bar> for Bar { | ||
fn as_ref(&self) -> &Bar { | ||
self | ||
} | ||
} | ||
|
||
impl AsMut<Bar> for Bar { | ||
fn as_mut(&mut self) -> &mut Bar { | ||
self | ||
} | ||
} | ||
|
||
pub fn foo<T: AsRef<Bar>>(_: T) {} | ||
pub fn qux<T: AsMut<Bar>>(_: T) {} | ||
pub fn bat<T: Borrow<T>>(_: T) {} | ||
pub fn baz<T: BorrowMut<T>>(_: T) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//! Test suggetions to borrow generic arguments instead of moving when all predicates hold after | ||
//! substituting in the reference type. | ||
//@ run-rustfix | ||
//@ aux-build:suggest-borrow-for-generic-arg-aux.rs | ||
|
||
#![allow(unused_mut)] | ||
extern crate suggest_borrow_for_generic_arg_aux as aux; | ||
|
||
pub fn main() { | ||
let bar = aux::Bar; | ||
aux::foo(&bar); //~ HELP borrow the value | ||
let _baa = bar; //~ ERROR use of moved value | ||
let mut bar = aux::Bar; | ||
aux::qux(&mut bar); //~ HELP borrow the value | ||
let _baa = bar; //~ ERROR use of moved value | ||
let bar = aux::Bar; | ||
aux::bat(&bar); //~ HELP borrow the value | ||
let _baa = bar; //~ ERROR use of moved value | ||
let mut bar = aux::Bar; | ||
aux::baz(&mut bar); //~ HELP borrow the value | ||
let _baa = bar; //~ ERROR use of moved value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//! Test suggetions to borrow generic arguments instead of moving when all predicates hold after | ||
//! substituting in the reference type. | ||
//@ run-rustfix | ||
//@ aux-build:suggest-borrow-for-generic-arg-aux.rs | ||
|
||
#![allow(unused_mut)] | ||
extern crate suggest_borrow_for_generic_arg_aux as aux; | ||
|
||
pub fn main() { | ||
let bar = aux::Bar; | ||
aux::foo(bar); //~ HELP borrow the value | ||
let _baa = bar; //~ ERROR use of moved value | ||
let mut bar = aux::Bar; | ||
aux::qux(bar); //~ HELP borrow the value | ||
let _baa = bar; //~ ERROR use of moved value | ||
let bar = aux::Bar; | ||
aux::bat(bar); //~ HELP borrow the value | ||
let _baa = bar; //~ ERROR use of moved value | ||
let mut bar = aux::Bar; | ||
aux::baz(bar); //~ HELP borrow the value | ||
let _baa = bar; //~ ERROR use of moved value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
error[E0382]: use of moved value: `bar` | ||
--> $DIR/suggest-borrow-for-generic-arg.rs:12:16 | ||
| | ||
LL | let bar = aux::Bar; | ||
| --- move occurs because `bar` has type `Bar`, which does not implement the `Copy` trait | ||
LL | aux::foo(bar); | ||
| --- value moved here | ||
LL | let _baa = bar; | ||
| ^^^ value used here after move | ||
| | ||
help: borrow the value to avoid moving it | ||
| | ||
LL | aux::foo(&bar); | ||
| + | ||
|
||
error[E0382]: use of moved value: `bar` | ||
--> $DIR/suggest-borrow-for-generic-arg.rs:15:16 | ||
| | ||
LL | let mut bar = aux::Bar; | ||
| ------- move occurs because `bar` has type `Bar`, which does not implement the `Copy` trait | ||
LL | aux::qux(bar); | ||
| --- value moved here | ||
LL | let _baa = bar; | ||
| ^^^ value used here after move | ||
| | ||
help: borrow the value to avoid moving it | ||
| | ||
LL | aux::qux(&mut bar); | ||
| ++++ | ||
|
||
error[E0382]: use of moved value: `bar` | ||
--> $DIR/suggest-borrow-for-generic-arg.rs:18:16 | ||
| | ||
LL | let bar = aux::Bar; | ||
| --- move occurs because `bar` has type `Bar`, which does not implement the `Copy` trait | ||
LL | aux::bat(bar); | ||
| --- value moved here | ||
LL | let _baa = bar; | ||
| ^^^ value used here after move | ||
| | ||
help: borrow the value to avoid moving it | ||
| | ||
LL | aux::bat(&bar); | ||
| + | ||
|
||
error[E0382]: use of moved value: `bar` | ||
--> $DIR/suggest-borrow-for-generic-arg.rs:21:16 | ||
| | ||
LL | let mut bar = aux::Bar; | ||
| ------- move occurs because `bar` has type `Bar`, which does not implement the `Copy` trait | ||
LL | aux::baz(bar); | ||
| --- value moved here | ||
LL | let _baa = bar; | ||
| ^^^ value used here after move | ||
| | ||
help: borrow the value to avoid moving it | ||
| | ||
LL | aux::baz(&mut bar); | ||
| ++++ | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0382`. |