Skip to content

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

Merged
merged 2 commits into from
Jun 26, 2025

Conversation

AaronBallman
Copy link
Collaborator

This was caught by a static analysis tool and seemed like a reasonable code quality improvement.

This was caught by a static analysis tool and seemed like a reasonable
code quality improvement.
@AaronBallman AaronBallman added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Jun 25, 2025
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jun 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 25, 2025

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

This 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:

  • (modified) clang/include/clang/AST/Type.h (+5-3)
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;
   }
 

this->Kind = Other.Kind;
this->ResultType = Other.ResultType;
this->Value = Other.Value;
if (this != &Other) {
Copy link
Collaborator

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?

Copy link
Collaborator Author

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?

Copy link
Collaborator

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'.

Copy link
Collaborator Author

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.

Copy link
Contributor

@cor3ntin cor3ntin left a 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

@AaronBallman AaronBallman merged commit 9514901 into llvm:main Jun 26, 2025
7 checks passed
@AaronBallman AaronBallman deleted the aballman-sa-spirvoperand branch June 26, 2025 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants