Skip to content

Add a lint for catching clone() auto(de)ref confusion. #11818

Closed
@Kimundi

Description

@Kimundi

Due to auto(de)ref, there are situations where calling clone() on a &T either clones the T, or the &T, depending on whether T has a Clone implementation or not.

This leads to confusing error messages in case of a missing Clone impls, especially in combination with #[deriving(Clone)].

Example

struct Foo; 
let x: Foo = (&Foo).clone();

Fails with

error: mismatched types: expected `main::Foo` but found `&main::Foo` (expected struct main::Foo but found &-ptr)

While this works:

struct Foo; 
impl Clone for Foo { fn clone(&self) -> Foo { Foo } }; 
let x: Foo = (&Foo).clone();

Lint

The proposed lint should catch situations where there are type errors arising from a clone() or deep_clone() call returning &T where T is expected, and recommend implementing Clone or DeepClone for T.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions