-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Guard against self-assignment; NFC #145743
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
Guard against self-assignment; NFC #145743
Conversation
This was caught by a static analysis tool and seemed like a reasonable code quality improvement.
@llvm/pr-subscribers-clang Author: Aaron Ballman (AaronBallman) ChangesThis was caught by a static analysis tool and seemed like a reasonable code quality improvement. Full diff: https://github.com/llvm/llvm-project/pull/145743.diff 1 Files Affected:
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 24f3ae78e857b..f4d39afded322 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -6410,9 +6410,11 @@ class SpirvOperand {
~SpirvOperand() {}
SpirvOperand &operator=(const SpirvOperand &Other) {
- this->Kind = Other.Kind;
- this->ResultType = Other.ResultType;
- this->Value = Other.Value;
+ if (this != &Other) {
+ this->Kind = Other.Kind;
+ this->ResultType = Other.ResultType;
+ this->Value = Other.Value;
+ }
return *this;
}
|
clang/include/clang/AST/Type.h
Outdated
this->Kind = Other.Kind; | ||
this->ResultType = Other.ResultType; | ||
this->Value = Other.Value; | ||
if (this != &Other) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean... I guess? The copy below is unbelievably cheap except for maybe ap-int. I DO find myself wondering why this (and operator==) isnt just = default
though. Could we try that instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would require C++20, wouldn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For comparison yes. Sorry, my suggestion was for THIS, not for operator==
(though TBH, I expected you to try both and tell my why it wouldn't work, and I hadn't recalled that we only added it for compare then :) ).
But operator=
should work with just = default
.
My problem with the guard is how inexpensive the copy is (though I see llvm::APInt
doesn't actually support self-move-assign, but DOES have the this = &RHS
for copy) compared to a branch.
So =default
would 'get it right'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, that's worth a shot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
approving this because it's a nice code cleaning, however defaulting operator= does not protect against self assignment afaik
This was caught by a static analysis tool and seemed like a reasonable code quality improvement.