Skip to content

Suggest as_ref() in E0507 if applicable #61109

Closed
@estebank

Description

@estebank

Given:

pub struct LipogramCorpora {
    selections: Vec<(char, Option<String>)>,
}

impl LipogramCorpora {
    pub fn validate_all(&mut self) -> Result<(), char> {
        for selection in &self.selections {
            if selection.1.is_some() {
                if selection.1.unwrap().contains(selection.0) {
                    return Err(selection.0);
                }
            }
        }
        Ok(())
    }
}

rustc should suggest using selection.1..as_ref():

error[E0507]: cannot move out of borrowed content
 --> src/lib.rs:9:20
  |
9 |                 if selection.1.unwrap().contains(selection.0) {
  |                    ^^^^^^^^^^^
  |                    |
  |                    cannot move out of borrowed content
  |                    help: consider borrowing instead of moving: `selection.1.as_ref()`

cc #51100, #57158

From https://thenewwazoo.github.io/clone.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.P-lowLow priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions