rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

improve [`almost_swapped`] to look for let statement

Open DevAccentor opened this issue 2 years ago • 3 comments

fix #8151

improve [almost_swapped] to also detects code using let statements like

let b = 1;
let a = b;
let b = a;

let mut b = 1;
let a = b;
b = a;

changelog: improve [almost_swapped] to look for code using let statement

DevAccentor avatar Jun 04 '22 14:06 DevAccentor

r? @flip1995

(rust-highfive has picked a reviewer for you, use r? to override)

rust-highfive avatar Jun 04 '22 14:06 rust-highfive

It would be better to rework this to parse the two statement into (Symbol|Expr, Expr) pairs and then check if it's almost swapped. Something like:

fn parse(...) -> Option<(ExprOrLocal, Expr)> { ... }

if let Some((lhs0, rhs0)) = parse(stmt[0])
    && let Some((lhs1, rhs1)) = parse(stmt[1])
    && is_same(lhs0, rhs1)
    && is_same(lhs1, rhs0)
{ ... }

Jarcho avatar Jul 22 '22 14:07 Jarcho

I like the suggestion by @Jarcho. This makes the code more DRY. Can you refactor the code please?

flip1995 avatar Jul 22 '22 16:07 flip1995

Because I took too long for my review, I think this PR is stale, so I close it for now. @DevAccentor if you want to continue working on it, let me know and I'll reopen the PR. Sorry for not responding for so long!

flip1995 avatar Aug 19 '22 14:08 flip1995

I would like to take over this PR.

chansuke avatar Jan 08 '23 10:01 chansuke