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

redundant_clone false-positive with arrays #5405

Closed
arnauorriols opened this issue Apr 2, 2020 · 1 comment
Closed

redundant_clone false-positive with arrays #5405

arnauorriols opened this issue Apr 2, 2020 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@arnauorriols
Copy link

When you need to own a value out of an array, you need to clone it. However clippy complains that the clone is redundant:

fn main() {
    let a: [String; 1] = [String::from("foo")];
    let b: String = a[0].clone();
}
// warning: redundant clone
//  --> src/main.rs:3:25
//   |
// 3 |     let b: String = a[0].clone();
//   |                         ^^^^^^^^ help: remove this
//   |
//   = note: `#[warn(clippy::redundant_clone)]` on by default
// note: this value is dropped without further use
//  --> src/main.rs:3:21
//   |
// 3 |     let b: String = a[0].clone();
//   |                     ^^^^
//   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone

If I remove the clone, I get this compiler error:

fn main() {
    let a: [String; 1] = [String::from("foo")];
    let b: String = a[0];
}
// error[E0508]: cannot move out of type `[std::string::String; 1]`, a non-copy array
//  --> src/lib.rs:3:21
//   |
// 3 |     let b: String = a[0];
//   |                     ^^^^
//   |                     |
//   |                     cannot move out of here
//   |                     move occurs because `a[_]` has type `std::string::String`, which does not implement the `Copy` trait
//   |                     help: consider borrowing here: `&a[0]`
//
// error: aborting due to previous error
//
// For more information about this error, try `rustc --explain E0508`.
$ cargo +nightly clippy -V
clippy 0.0.212 (23549a8 2020-03-16)
@flip1995 flip1995 added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Apr 2, 2020
@flip1995
Copy link
Member

flip1995 commented Apr 2, 2020

cc @sinkuu

rabisg0 added a commit to rabisg0/rust-clippy that referenced this issue Apr 12, 2020
Check whether slice elements implement Copy before suggesting to drop
the clone method
bors added a commit that referenced this issue Apr 14, 2020
Fixes #5405: redundant clone false positive with arrays

Check whether slice elements implement Copy before suggesting to drop
the clone method

changelog: add a check for slice indexing on redundant_clone lint
@bors bors closed this as completed in ab3946d Apr 14, 2020
flip1995 pushed a commit to flip1995/rust-clippy that referenced this issue May 5, 2020
Changes:
````
Rename dummy_hir_id -> parent_hir_id
rustup rust-lang/rust#71116
Change default many single char names threshold
Better precedence case management + more tests
Use only check_expr with parent expr and precedence
Check for Deref trait impl + add fixed version
Report using stmts and expr + tests
Global rework + fix imports
Working basic dereference clip
Add test for zero single char names
Make the single char threshold strict inequality
large_enum_variant: Report sizes of variants
Refactor: Use rustc's `match_def_path`
deps: bump compiletest-rs from 0.4 to 0.5
rustup rust-lang/rust#70643
Explain panic on `E0463` in integration tests
Temporarily disable rustfmt integration test
result_map_unit_fn: Fix incorrect UI tests
Cleanup: Use rustc's is_proc_macro_attr
Cleanup: Use our `sym!` macro more
Fixes rust-lang#5405: redundant clone false positive with arrays
Disallow bit-shifting in `integer_arithmetic` lint
update lints
cargo dev fmt
Make use of Option/Result diagnostic items
Make use of some existing diagnostic items
Say that diagnostic items are preferred over paths
verbose_bit_mask: fix bit mask used in docs
Update documentation for new_ret_no_self
Update doc generation script
Add lint on large const arrays
Make the epsilon note spanless
Split check_fn function
Indicate when arrays are compared in error message
Make epsilon note spanless when comparing arrays
Add float cmp const tests for arrays
Add float cmp tests for arrays
Handle constant arrays with single value
Don't show comparison suggestion for arrays
Allow for const arrays of zeros
Handle evaluating constant index expression
Add handling of float arrays to miri_to_const
Update stderr of float_cmp test
Update field names in is_float
Add tests for float in array comparison
Add lint when comparing floats in an array
````

Fixes #71114
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-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

2 participants