-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Suggest a borrow when using dbg!() #120327
Comments
I thought the intent with |
Not sure about the majority of users but for me |
Probably a common thing to do, sure, but if we're talking about adding a lint, why not nudge the user in the direction of the intended and documented usage? You could at least do both: "insert |
The fix in #120990 will only report a hint when there is already a For example this code: fn in_expr() -> String {
let b: String = "".to_string();
let res = dbg!(b);
let _l = b.len();
return res;
} before this fix the error is: error[E0382]: borrow of moved value: `b`
--> ./t/de.rs:10:14
|
8 | let b: String = "".to_string();
| - move occurs because `b` has type `String`, which does not implement the `Copy` trait
9 | let res = dbg!(b);
| ------- value moved here
10 | let _l = b.len();
| ^ value borrowed here after move
|
help: borrow this binding in the pattern to avoid moving the value
--> /Users/yukang/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/macros.rs:364:13
|
364| ref tmp => {
| +++
error: aborting due to 1 previous error with the fix, since there is already a error[E0382]: borrow of moved value: `b`
--> ./t/de.rs:10:14
|
8 | let b: String = "".to_string();
| - move occurs because `b` has type `String`, which does not implement the `Copy` trait
9 | let res = dbg!(b);
| ------- value moved here
10 | let _l = b.len();
| ^ value borrowed here after move
|
help: consider borrowing instead of transferring ownership
|
9 | let res = dbg!(&b);
| + The suggestion is not 100% right, since if we change it to If the original code is: fn in_expr() -> String {
let b: String = "".to_string();
let res = dbg!(b);
let _l = b.len();
return res.to_string();
} Then suggest a borrow is a great and correct hint. |
Similar in the scenario of function argument: fn get_expr(_s: String) {}
fn test() {
let a: String = "".to_string();
let _res = get_expr(dbg!(a));
let _l = a.len();
} original error is: error[E0382]: borrow of moved value: `a`
--> ./t/de.rs:12:14
|
10 | let a: String = "".to_string();
| - move occurs because `a` has type `String`, which does not implement the `Copy` trait
11 | let _res = get_expr(dbg!(a));
| ------- value moved here
12 | let _l = a.len();
| ^ value borrowed here after move
|
help: borrow this binding in the pattern to avoid moving the value
--> /Users/yukang/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/macros.rs:364:13
|
364| ref tmp => {
| +++ new errors from the fix: error[E0382]: borrow of moved value: `a`
--> ./t/de.rs:12:14
|
10 | let a: String = "".to_string();
| - move occurs because `a` has type `String`, which does not implement the `Copy` trait
11 | let _res = get_expr(dbg!(a));
| ------- value moved here
12 | let _l = a.len();
| ^ value borrowed here after move
|
help: consider borrowing instead of transferring ownership
|
11 | let _res = get_expr(dbg!(&a));
| + |
Would
The suggestion wouldn't compile anyway right? Because
Once that's addressed (whichever way it's done), However, if you make the (I agree that the |
Yes, it's a good hint, but from the implementation view of a hint, it's much harder to analyze the following code from the error point and then construct a new expression. Even it seems an easy thing for humans. You are right, the suggestion is not 100% right, we may need an extra follow-up fix after applying the hint, which is also a common thing in rustc diagnostics. |
…r=oli-obk Suggest a borrow when using dbg Fixes rust-lang#120327 r? `@estebank`
…r=oli-obk Suggest a borrow when using dbg Fixes rust-lang#120327 r? `@estebank`
…r=oli-obk Suggest a borrow when using dbg Fixes rust-lang#120327 r? ``@estebank``
…r=oli-obk Suggest a borrow when using dbg Fixes rust-lang#120327 r? ```@estebank```
Rollup merge of rust-lang#120990 - chenyukang:yukang-fix-120327-dbg, r=oli-obk Suggest a borrow when using dbg Fixes rust-lang#120327 r? ````@estebank````
Code
Current output
Desired output
Rationale and extra context
No response
Other cases
No response
Rust Version
Anything else?
No response
The text was updated successfully, but these errors were encountered: