From 40e125053bc2ddaf20d6bce14fa026cdb3131ca1 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 18 Sep 2026 09:38:38 +0200 Subject: [PATCH] Improved linter to recognize copies as identical values --- src/linter/isIdentical.go | 30 ++++++++++++++++++++++++++++++ src/linter/lintBinaryOp.go | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/linter/isIdentical.go diff --git a/src/linter/isIdentical.go b/src/linter/isIdentical.go new file mode 100644 index 0000000..7fd7790 --- /dev/null +++ b/src/linter/isIdentical.go @@ -0,0 +1,30 @@ +package linter + +import ( + "git.urbach.dev/cli/q/src/ssa" +) + +// isIdentical returns true if the values are identical or copies of identical values. +func isIdentical(left ssa.Value, right ssa.Value) bool { + if left == right { + return true + } + + leftCopy, leftIsCopy := left.(*ssa.Copy) + + if leftIsCopy && leftCopy.Value == right { + return true + } + + rightCopy, rightIsCopy := right.(*ssa.Copy) + + if rightIsCopy && rightCopy.Value == left { + return true + } + + if leftIsCopy && rightIsCopy && leftCopy.Value == rightCopy.Value { + return true + } + + return false +} \ No newline at end of file diff --git a/src/linter/lintBinaryOp.go b/src/linter/lintBinaryOp.go index 0b45dad..49acc7b 100644 --- a/src/linter/lintBinaryOp.go +++ b/src/linter/lintBinaryOp.go @@ -16,7 +16,7 @@ func lintBinaryOp(binOp *ssa.BinaryOp, file *fs.File) error { return nil } - if binOp.Left == binOp.Right { + if isIdentical(binOp.Left, binOp.Right) { switch binOp.Op { case token.Sub, token.Div, token.Mod, token.And, token.Or, token.Xor, token.Equal, token.NotEqual, token.Less, token.LessEqual, token.Greater, token.GreaterEqual: return errors.New(&IdenticalExpressions{Operator: binOp.Op.String()}, file, binOp.Source) -- 2.51.2