views:

70

answers:

3

I have read numerous times that Build and Analyze is not perfect. When I run it, it does turn out some useful information and has definitely improved my code, but it also gives results that are not necessary problems. At the minute I'm changing my code to try and get rid of all the Analyzer results, whether they are a problem or not. Is this best practise, because like warnings it is then easy to check that you have a problem, or should I recognise that the analyzer is not perfect and not change the code unless it has found an genuine problem?

+6  A: 

First of all, all compiler warnings are just errors that will only show up in the runtime. They don't prevent compilation but they will eventually cause a serious problem somewhere down the road. You should go into the build settings and check "Treat Warnings as Errors" and fix the resulting errors as you go. Otherwise, you will eventually hit hard to debug problems way down the road.

The analyzer is not perfect but you should closely inspect any problems it finds. Any problems it finds with your code should be fixed. Sometimes it reports errors from the API and there may not be anything to be done about those.

In most cases, ignoring errors from any compiler stage is a "penny wise, pound foolish" time savings. Any time you save upfront will be lost debugging later.

TechZen
Yep, I like to think of the static analyzer as an uber-compiler that gives me warnings/errors the compiler doesn't see. That being said, it's not perfect and I have seen a false positive or two, but 99% of the time it's spot on.
Alex
+3  A: 

Yes - ideally you want to fix all the issues raised from the Analyse process.

However, while clang is great, it is not perfect. Sometimes it may raise a false positive. If you are certain this is the case, and the diagnostic is in error, then you should try to recreate the smallest possible test case that exhibits the error, and submit it to http://clang-analyzer.llvm.org/filing_bugs.html so they can improve the analysis engine.

gavinb
+1 for suggesting reporting false positives.
Martinho Fernandes
A: 

An exception to the above answers, I assume, is when you are using Garbage Collection. In this case, the Analyzer, alas, still shows you potential leaks, but naturally they are not applicable when using Garbage Collection.

Enchilada
It will only warn about potential leaks if you're compiling a framework in the GC-supported (not required) or retain-release modes or an app in the retain-release mode. For an app compiled with GC though, it will evaluate the code in that context only.
Ashley Clark