Skip to content

remove redundant clones via mir opt? #83193

Open
@matthiaskrgr

Description

@matthiaskrgr

I wonder if it is possible to have an optimization pass that removes redundant clones if we know that .clone() does not do anything funky and that the original object would have been untouched and dropped in the same block where it was cloned.

There's a clippy lint which tries to warn about useless clones https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone

The pass could optimize something like

#[derive(Clone)]
pub struct S {
    inner: String,
}

pub fn a(a: S) -> S {
    a.clone()
}

to

#[derive(Clone)]
pub struct S {
    inner: String,
}

pub fn a(a: S) -> S {
    a // just return the object
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-mir-optArea: MIR optimizationsC-enhancementCategory: An issue proposing an enhancement or a PR with one.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions