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

Suggest {Option,Result}::as_ref() instead of cloned() in some cases #114052

Merged
merged 1 commit into from
Jul 25, 2023

Conversation

clubby789
Copy link
Contributor

Fixes #114050

When we have an expr available that produces the type expectation, we can suggest appending .as_ref() to the span, instead of cloning the expr producing the mismatch

@rustbot
Copy link
Collaborator

rustbot commented Jul 25, 2023

r? @jackh726

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 25, 2023
Copy link
Member

@WaffleLapkin WaffleLapkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, what happens in a &mut case?

compiler/rustc_hir_typeck/src/demand.rs Outdated Show resolved Hide resolved
@clubby789
Copy link
Contributor Author

The original code explicitly checks for &T (not &mut T) so there's no effect on comparing &mut T to T.

Copy link
Member

@WaffleLapkin WaffleLapkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me with nits applied (namely method/closure rename and an added // run-rustfix)

@clubby789
Copy link
Contributor Author

@bors r=WaffleLapkin

@bors
Copy link
Contributor

bors commented Jul 25, 2023

📌 Commit c83dfe9 has been approved by WaffleLapkin

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 25, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 25, 2023
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#114008 (coverage: Obtain the `__llvm_covfun` section name outside a per-function loop)
 - rust-lang#114014 (builtin_macros: expect raw strings too)
 - rust-lang#114043 (docs(LazyLock): add example pass local LazyLock variable to struct)
 - rust-lang#114051 (Add regression test for invalid "unused const" in method)
 - rust-lang#114052 (Suggest `{Option,Result}::as_ref()` instead of `cloned()` in some cases)
 - rust-lang#114058 (Add help for crate arg when crate name is invalid)
 - rust-lang#114060 (abi: unsized field in union - assert to delay bug )

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit b7a1ff2 into rust-lang:master Jul 25, 2023
11 checks passed
@rustbot rustbot added this to the 1.73.0 milestone Jul 25, 2023
@clubby789
Copy link
Contributor Author

Looking into handling &mut as well it could be rather convoluted.

fn main() {
    let mut s = String::new();
    let x = Some(s.clone());
    let y = Some(&mut s);
    println!("{}", x == y);
}

must become

fn main() {
    let mut s = String::new();
    let x = Some(s.clone());
    let y = Some(&mut s);
    println!("{}", x.as_ref() == y.as_deref());
}

@WaffleLapkin
Copy link
Member

@clubby789 it can be simple for Copy types where you can just use .copied(). For types where we want to use refs... yeah it could be convoluted

bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 29, 2023
…ion, r=WaffleLapkin

Refactor + improve diagnostics for `&mut T`/`T` mismatch inside Option/Result

Follow up to rust-lang#114052. This also makes the diagnostics structured + translatable.

r? `@WaffleLapkin`
flip1995 pushed a commit to flip1995/rust that referenced this pull request Jul 31, 2023
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#114008 (coverage: Obtain the `__llvm_covfun` section name outside a per-function loop)
 - rust-lang#114014 (builtin_macros: expect raw strings too)
 - rust-lang#114043 (docs(LazyLock): add example pass local LazyLock variable to struct)
 - rust-lang#114051 (Add regression test for invalid "unused const" in method)
 - rust-lang#114052 (Suggest `{Option,Result}::as_ref()` instead of `cloned()` in some cases)
 - rust-lang#114058 (Add help for crate arg when crate name is invalid)
 - rust-lang#114060 (abi: unsized field in union - assert to delay bug )

r? `@ghost`
`@rustbot` modify labels: rollup
Urgau added a commit to Urgau/rust that referenced this pull request Aug 17, 2023
cuviper added a commit to cuviper/rust that referenced this pull request Aug 17, 2023
…rors

Revert PR rust-lang#114052 to fix invalid suggestion

This PR reverts rust-lang#114052 to fix the invalid suggestion produced by the PR.

Unfortunately the invalid suggestion cannot be improved from the current position where it's emitted since we lack enough information (is an assignment?, left or right?, ...) to be able to fix it here. Furthermore the previous wasn't wrong, just suboptimal, contrary to the current one which is just wrong.

Added a regression test and commented out some code instead of removing it so we can use it later.

Reopens rust-lang#114050
Fixes rust-lang#114925
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 18, 2023
Rollup of 5 pull requests

Successful merges:

 - rust-lang#113715 (Unstable Book: update `lang_items` page and split it)
 - rust-lang#114897 (Partially revert rust-lang#107200)
 - rust-lang#114913 (Fix suggestion for attempting to define a string with single quotes)
 - rust-lang#114931 (Revert PR rust-lang#114052 to fix invalid suggestion)
 - rust-lang#114944 (update `thiserror` to version >= 1.0.46)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

E0308 suggestion can lead to unnecessary clones
5 participants