Solve a error .clone() suggestion when moving a mutable reference#127579
Solve a error .clone() suggestion when moving a mutable reference#127579bors merged 1 commit intorust-lang:masterfrom
.clone() suggestion when moving a mutable reference#127579Conversation
This comment has been minimized.
This comment has been minimized.
.clone() suggestion when moving a mutable reference.clone() suggestion when moving a mutable reference
.clone() suggestion when moving a mutable reference.clone() suggestion when moving a mutable reference
.clone() suggestion when moving a mutable reference.clone() suggestion when moving a mutable reference
| } else { | ||
| (None, &[][..], 0) | ||
| }; | ||
| let mut reborrow = false; |
There was a problem hiding this comment.
| let mut reborrow = false; | |
| let mut suggest_reborrow = false; |
| && let node = self.infcx.tcx.hir_node_by_def_id(def_id) | ||
| && let Some(fn_sig) = node.fn_sig() | ||
| && let Some(ident) = node.ident() | ||
| && let Some(pos) = args.iter().position(|arg| arg.hir_id == expr.hir_id) | ||
| && let Some(arg) = fn_sig.decl.inputs.get(pos + offset) |
There was a problem hiding this comment.
this only works for functions from the current crate, greatly lowering the usefulness of this new diagnostics.
You should instead be able to use tcx.fn_sig(def_id).skip_binder().skip_binder().inputs()[pos + offset].is_param()
There was a problem hiding this comment.
this only works for functions from the current crate, greatly lowering the usefulness of this new diagnostics.
You should instead be able to use
tcx.fn_sig(def_id).skip_binder().skip_binder().inputs()[pos + offset].is_param()
Done. Thank you.
Because is_param in
rust/compiler/rustc_middle/src/ty/sty.rs
Line 1048 in c92a8e4
index which I'm not sure what means, I don't use this function.
| reborrow = is_sugg_reborrow(); | ||
|
|
||
| let mut span: MultiSpan = arg.span.into(); | ||
| span.push_span_label( |
There was a problem hiding this comment.
also I feel like we should not emit the "consider changing this parameter type in function generic to borrow instead if owning the value isn't necessary" note if a reborrow would work. so either suggest a reborrow or suggest changing the fn_sig, but not both
There was a problem hiding this comment.
also I feel like we should not emit the "consider changing this parameter type in function
genericto borrow instead if owning the value isn't necessary" note if a reborrow would work. so either suggest a reborrow or suggest changing the fn_sig, but not both
Done. Thank you.
2b60569 to
1f78e60
Compare
This comment has been minimized.
This comment has been minimized.
.clone() suggestion when moving a mutable reference.clone() suggestion when moving a mutable reference
|
☔ The latest upstream changes (presumably #127614) made this pull request unmergeable. Please resolve the merge conflicts. |
.clone() suggestion when moving a mutable reference.clone() suggestion when moving a mutable reference
|
☔ The latest upstream changes (presumably #127796) made this pull request unmergeable. Please resolve the merge conflicts. |
|
Hello, I have made modifications, please review for me again. I received a conflict notification and I would like to resolve the conflict after approved. |
| } | ||
| false | ||
| }; | ||
| *has_suggest_reborrow = is_sugg_reborrow(); |
There was a problem hiding this comment.
why use a closure here instead of inlining the code below into return true?
There was a problem hiding this comment.
why use a closure here instead of inlining the code below into
return true?
Thank you. I just think that because it's in the function suggest_ref_or_clone function, this judgment code checks a special situation and returns early, so maybe this check code could be organized into a closure. Should I abandon the closure here?
There was a problem hiding this comment.
yes, abandon the closure
Done. Thank you.
…on and it's type is a generic param, it can be reborrowed to avoid moving.
for example:
```rust
struct Y(u32);
// x's type is '& mut Y' and it is used in `fn generic<T>(x: T) {}`.
fn generic<T>(x: T) {}
```
fixes rust-lang#127285
|
@bors r+ rollup |
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#125042 (Use ordinal number in argument error) - rust-lang#127229 (rustdoc: click target for sidebar items flush left) - rust-lang#127337 (Move a few intrinsics to Rust abi) - rust-lang#127472 (MIR building: Stop using `unpack!` for `BlockAnd<()>`) - rust-lang#127579 (Solve a error `.clone()` suggestion when moving a mutable reference) - rust-lang#127769 (Don't use implicit features in `Cargo.toml` in `compiler/`) - rust-lang#127844 (Remove invalid further restricting suggestion for type bound) - rust-lang#127855 (Add myself to review rotation) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#125042 (Use ordinal number in argument error) - rust-lang#127229 (rustdoc: click target for sidebar items flush left) - rust-lang#127337 (Move a few intrinsics to Rust abi) - rust-lang#127472 (MIR building: Stop using `unpack!` for `BlockAnd<()>`) - rust-lang#127579 (Solve a error `.clone()` suggestion when moving a mutable reference) - rust-lang#127769 (Don't use implicit features in `Cargo.toml` in `compiler/`) - rust-lang#127844 (Remove invalid further restricting suggestion for type bound) - rust-lang#127855 (Add myself to review rotation) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#125042 (Use ordinal number in argument error) - rust-lang#127229 (rustdoc: click target for sidebar items flush left) - rust-lang#127337 (Move a few intrinsics to Rust abi) - rust-lang#127472 (MIR building: Stop using `unpack!` for `BlockAnd<()>`) - rust-lang#127579 (Solve a error `.clone()` suggestion when moving a mutable reference) - rust-lang#127769 (Don't use implicit features in `Cargo.toml` in `compiler/`) - rust-lang#127844 (Remove invalid further restricting suggestion for type bound) - rust-lang#127855 (Add myself to review rotation) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#127579 - surechen:fix_127285, r=lcnr Solve a error `.clone()` suggestion when moving a mutable reference If the moved value is a mut reference, it is used in a generic function and it's type is a generic param, suggest it can be reborrowed to avoid moving. for example: ```rust struct Y(u32); // x's type is '& mut Y' and it is used in `fn generic<T>(x: T) {}`. fn generic<T>(x: T) {} ``` fixes rust-lang#127285
Dedup `&mut *` reborrow suggestion in loops rust-lang#73534 added a reborrow suggestion in loops; rust-lang#127579 generalized this to generic parameters, making the suggestion triggers twice: ```rs use std::io::Read; fn decode_scalar(_reader: impl Read) {} fn decode_array(reader: &mut impl Read) { for _ in 0.. { decode_scalar(reader); } } ``` ``` error[E0382]: use of moved value: `reader` --> src/lib.rs:6:23 | 4 | fn decode_array(reader: &mut impl Read) { | ------ move occurs because `reader` has type `&mut impl Read`, which does not implement the `Copy` trait 5 | for _ in 0.. { | ------------ inside of this loop 6 | decode_scalar(reader); | ^^^^^^ value moved here, in previous iteration of loop | help: consider creating a fresh reborrow of `reader` here | 6 | decode_scalar(&mut *reader); | ++++++ help: consider creating a fresh reborrow of `reader` here | 6 | decode_scalar(&mut *reader); | ++++++ ``` This PR removes the suggestion in loops, as it requires generic parameters anyway (i.e., the reborrow is automatic if there is no generic params). `@rustbot` label +A-borrow-checker +A-diagnostics +A-suggestion-diagnostics +D-papercut
Rollup merge of rust-lang#138462 - ShE3py:mut-borrow-in-loop, r=oli-obk Dedup `&mut *` reborrow suggestion in loops rust-lang#73534 added a reborrow suggestion in loops; rust-lang#127579 generalized this to generic parameters, making the suggestion triggers twice: ```rs use std::io::Read; fn decode_scalar(_reader: impl Read) {} fn decode_array(reader: &mut impl Read) { for _ in 0.. { decode_scalar(reader); } } ``` ``` error[E0382]: use of moved value: `reader` --> src/lib.rs:6:23 | 4 | fn decode_array(reader: &mut impl Read) { | ------ move occurs because `reader` has type `&mut impl Read`, which does not implement the `Copy` trait 5 | for _ in 0.. { | ------------ inside of this loop 6 | decode_scalar(reader); | ^^^^^^ value moved here, in previous iteration of loop | help: consider creating a fresh reborrow of `reader` here | 6 | decode_scalar(&mut *reader); | ++++++ help: consider creating a fresh reborrow of `reader` here | 6 | decode_scalar(&mut *reader); | ++++++ ``` This PR removes the suggestion in loops, as it requires generic parameters anyway (i.e., the reborrow is automatic if there is no generic params). `@rustbot` label +A-borrow-checker +A-diagnostics +A-suggestion-diagnostics +D-papercut
If the moved value is a mut reference, it is used in a generic function and it's type is a generic param, suggest it can be reborrowed to avoid moving.
for example:
fixes #127285