Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find inadvertent object pointer comparisons?

Quick question - Is there a good way to find uses of == with objects instead of isEqual:?

Full story:

I had a bunch of code kind of like this:

typedef long DataKey;

DataKey x;
DataKey y;

if (x == y) {
    // do stuff
}

I now have a need to replace using a long for my DataKey with an object. After creating the class and doing a bunch of global search and replace, my code now is kind of like this:

@interface DataKey : NSObject

DataKey *x;
DataKey *y;

if (x == y) { // uh-oh - this is now bad
    // do stuff
}

Is there a warning I can enable in the compiler that warns about using the scalar operators in pointers? I'm using Xcode 4.5.2 with the LLVM 4.1 compiler. I haven't been able to find one.

Any other suggestions to help fix all of this code? This is not a trivial code base. There are hundreds of source files to deal with. This is a major refactoring effort.

Edit:

It would be great if there was a warning much like when you use assignment in an if condition instead of comparison. You can get a warning for this and use parenthesis to stop the warning.

Update:

Based on a suggestion in the comments, I added the -Weverything compiler option to a test project. This did not produce any desired result when comparing two object pointers with ==.

Perhaps a combination of C++, overloading the operator== method, and getting some compiler warning/error when trying to use that operator can be achieved. I may post another question focused on this option.

like image 648
rmaddy Avatar asked Nov 03 '22 09:11

rmaddy


1 Answers

I'm pretty sure there is no such warning, as comparing pointers for equality is pretty common and not all that commonly done in error. Your best bet will unfortunately be to go through any place DataKeys might be used this way and search for ==. Not very fun, I know.

like image 199
Chuck Avatar answered Nov 14 '22 09:11

Chuck