Skip to content

unnecessary_to_owned causes borrowing error after removing to_vec #12098

Closed
@DervexDev

Description

@DervexDev

Summary

Clippy suggests to remove to_vec which results in compile error: "cannot assign to test.mut_this because it is borrowed". In this case to_vec is required as it copies self so that it's no longer borrowed.

Lint Name

clippy::unnecessary_to_owned

Reproducer

I tried this code:

struct Test {
    list: Vec<String>,
    mut_this: bool,
}

impl Test {
    fn list(&self) -> &[String] {
        &self.list
    }
}

fn main() {
    let mut test = Test {
        list: vec![String::from("foo"), String::from("bar")],
        mut_this: false,
    };

    for string in test.list().to_vec() {
        println!("{}", string);
        test.mut_this = true;
    }
}

I saw this happen:

unnecessary use of `to_vec`
main.rs(18, 19): use: `test.list()`

I expected to see this happen:
No warning

Version

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: aarch64-apple-darwin
release: 1.73.0
LLVM version: 17.0.2

Additional Labels

@rustbot label I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions