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

clippy warning fix for "unnecessary use" causes compile error "cannot move out of borrowed" #11763

Open
cbanu opened this issue Nov 6, 2023 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@cbanu
Copy link

cbanu commented Nov 6, 2023

Summary

Output of cargo clippy --fix --bin "debug-viewer-rs" command:

Checking debug-viewer-rs v0.1.0 (/home/cosmin/Work/debug-viewer-rs)
warning: failed to automatically apply fixes suggested by rustc to crate `debug_viewer_rs`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0505]: cannot move out of `entry` because it is borrowed
  --> src/main.rs:26:58
   |
23 |         let entry = entry?;
   |             ----- binding `entry` declared here
24 |         match entry.path()?.to_str() {
   |               ----- borrow of `entry` occurs here
25 |             Some(entry_path) => {
26 |                 process_entry(entry_path, BufReader::new(entry))?;
   |                 ------------- borrow later used by call  ^^^^^ move out of `entry` occurs here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0505`.
Original diagnostics will follow.

warning: unnecessary use of `to_string`
  --> src/main.rs:26:31
   |
26 |                 process_entry(&entry_path.to_string(), BufReader::new(entry))?;
   |                               ^^^^^^^^^^^^^^^^^^^^^^^ help: use: `entry_path`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned
   = note: `#[warn(clippy::unnecessary_to_owned)]` on by default

warning: `debug-viewer-rs` (bin "debug-viewer-rs") generated 1 warning (run `cargo clippy --fix --bin "debug-viewer-rs"` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.22s

Reproducer

This is the code in src/main.rs:

use std::{
    fs::File,
    io::{BufReader, Read},
};

use anyhow::Result;
use flate2::bufread::GzDecoder;
use tar::Archive;

const DIAGS_FILE: &str = "../diags.tar.gz";

fn process_entry<R>(entry_path: &str, _contents: BufReader<R>) -> Result<()>
where
    R: Read,
{
    println!("Processing {}", entry_path);
    Ok(())
}

fn main() -> Result<()> {
    let mut archive = Archive::new(GzDecoder::new(BufReader::new(File::open(DIAGS_FILE)?)));
    for entry in archive.entries()? {
        let entry = entry?;
        match entry.path()?.to_str() {
            Some(entry_path) => {
                process_entry(&entry_path.to_string(), BufReader::new(entry))?;
            }
            None => continue,
        }
    }
    Ok(())
}

My Cargo.toml file is:

[package]
name = "debug-viewer-rs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.75"
flate2 = { version = "1.0.28", features = ["zlib-ng"], default-features = false }
tar = "0.4.40"

Version

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2

Additional Labels

No response

@cbanu cbanu added the C-bug Category: Clippy is not doing the correct thing label Nov 6, 2023
@phy1729
Copy link

phy1729 commented Nov 26, 2023

This seems to be the same bug I'm running into with

use url::Url;

fn main() {
    let mut url = Url::parse("https://example.com/foo.bar").unwrap();

    if let Some((raw_path, _)) = url.path().rsplit_once('.') {
        url.set_path(&raw_path.to_owned());
    }
    dbg!(url);
}

which results in a similar false positive lint

warning: unnecessary use of `to_owned`
 --> src/main.rs:7:22
  |
7 |         url.set_path(&raw_path.to_owned());
  |                      ^^^^^^^^^^^^^^^^^^^^ help: use: `raw_path`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned
  = note: `#[warn(clippy::unnecessary_to_owned)]` on by default

warning: `playground` (bin "playground") generated 1 warning (run `cargo clippy --fix --bin "playground"` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.81s

Also seems related to #8759.

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
Projects
None yet
Development

No branches or pull requests

2 participants