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

unnecessary_to_owned causes borrowing error after removing to_vec #12098

Closed
DervexDev opened this issue Jan 5, 2024 · 1 comment · Fixed by #12650
Closed

unnecessary_to_owned causes borrowing error after removing to_vec #12098

DervexDev opened this issue Jan 5, 2024 · 1 comment · Fixed by #12650
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@DervexDev
Copy link

DervexDev commented Jan 5, 2024

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

@DervexDev DervexDev added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 5, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jan 5, 2024
@DervexDev DervexDev changed the title unnecessary_to_owned causes borrowing error after removingto_vec unnecessary_to_owned causes borrowing error after removing to_vec Jan 5, 2024
@cocodery
Copy link
Contributor

@rustbot claim

bors added a commit that referenced this issue May 9, 2024
fix false positive in Issue/12098 because lack of consideration of mutable caller

fixes [Issue#12098](#12098)

In issue#12098, the former code doesn't consider the caller for clone is mutable, and suggests to delete clone function.

In this change, we first get the inner caller requests for clone,
and if it's immutable, the following code will suggest deleting clone.

If it's mutable, the loop will check whether a borrow check violation exists,
if exists, the lint should not execute, and the function will directly return;
otherwise, the following code will handle this.

changelog: [`clippy::unnecessary_to_owned`]: fix false positive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
3 participants