Skip to content

unnecessary_to_owned doesn't take type changes into account #9351

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

Closed
ghost opened this issue Aug 19, 2022 · 2 comments · Fixed by #9424
Closed

unnecessary_to_owned doesn't take type changes into account #9351

ghost opened this issue Aug 19, 2022 · 2 comments · Fixed by #9424
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

@ghost
Copy link

ghost commented Aug 19, 2022

Summary

unnecessary_to_owned suggests changing the type of an input but doesn't check if this type is used in other places. This can lead to suggestions that won't compile due to mismatched type errors.

Lint Name

unnecessary_to_owned

Reproducer

I tried this code:

#![allow(unused_variables)]
use std::ops::Deref;
use std::path::PathBuf;

fn main() {
    let path = std::path::Path::new("x");
    let x: PathBuf = require_deref_path(path.to_owned());
    // let x: PathBuf = require_deref_path(path);
}

fn require_deref_path<T: Deref<Target = std::path::Path>>(x: T) -> T {
    x
}

I saw this happen:

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

Applying the suggestion results in this error

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
  --> src/main.rs:8:41
   |
8  |     let x: PathBuf = require_deref_path(path);
   |                      ------------------ ^^^^- help: try using a conversion method: `.to_path_buf()`
   |                      |                  |
   |                      |                  expected struct `PathBuf`, found `&Path`
   |                      arguments to this function are incorrect
   |
note: function defined here
  --> src/main.rs:11:4
   |
11 | fn require_deref_path<T: Deref<Target = std::path::Path>>(x: T) -> T {

Playground

I expected to see this happen:
No warning.

Version

Clippy 0.1.65 (2022-08-16 86c6ebe)

Additional Labels

@rustbot +label I-suggestion-causes-error

@ghost ghost 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 Aug 19, 2022
@rustbot

This comment was marked as off-topic.

@ghost
Copy link
Author

ghost commented Aug 19, 2022

@rustbot claim

@rustbot rustbot assigned ghost Aug 19, 2022
@xFrednet xFrednet added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Aug 19, 2022
bors added a commit that referenced this issue Sep 4, 2022
Fix `unnecessary_to_owned` false positive

Fixes #9351.

Note that this commit reworks that fix for #9317. The change
is to check that the type implements `AsRef<str>` before regarding
`to_string` as an equivalent of `to_owned`. This was suggested
by Jarcho in the #9317 issue comments.

The benefit of this is that it moves some complexity out of
`check_other_call_arg` and simplifies the module as a whole.

changelog: FP: [`unnecessary_to_owned`]: No longer lints, if type change would cause errors in the caller function
@bors bors closed this as completed in 750a2d5 Sep 4, 2022
kraktus pushed a commit to kraktus/rust-clippy that referenced this issue Sep 5, 2022
Fixes rust-lang#9351.

Note that this commit reworks that fix for rust-lang#9317. The change
is to check that the type implements `AsRef<str>` before regarding
`to_string` as an equivalent of `to_owned`. This was suggested
by Jarcho in the rust-lang#9317 issue comments.

The benefit of this is that it moves some complexity out of
`check_other_call_arg` and simplifies the module as a whole.
kartva pushed a commit to kartva/rust-clippy that referenced this issue Sep 6, 2022
Fixes rust-lang#9351.

Note that this commit reworks that fix for rust-lang#9317. The change
is to check that the type implements `AsRef<str>` before regarding
`to_string` as an equivalent of `to_owned`. This was suggested
by Jarcho in the rust-lang#9317 issue comments.

The benefit of this is that it moves some complexity out of
`check_other_call_arg` and simplifies the module as a whole.
kartva pushed a commit to kartva/rust-clippy that referenced this issue Sep 7, 2022
Fixes rust-lang#9351.

Note that this commit reworks that fix for rust-lang#9317. The change
is to check that the type implements `AsRef<str>` before regarding
`to_string` as an equivalent of `to_owned`. This was suggested
by Jarcho in the rust-lang#9317 issue comments.

The benefit of this is that it moves some complexity out of
`check_other_call_arg` and simplifies the module as a whole.
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
Development

Successfully merging a pull request may close this issue.

2 participants