How to find inadvertent object pointer comparisons?

被刻印的时光 ゝ 提交于 2019-12-12 11:04:06

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/13731097/how-to-find-inadvertent-object-pointer-comparisons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!