a patch for static analyzer to check : std::list::empty()

Hello,
recently, as an exercise, I have implemented a checker for clang static analyzer.It checks the call of std::list::size( ) .

It is more efficient to use containers empty() method to identify an empty container.Codes like bellow :

int f() {
list l;
if (l.empty() == 0) {
}
}
will be warned.

btw, I did my dev on the release 3.5.

EmptyContainerChecker.cpp (5.78 KB)

empty_container_test.cpp (454 Bytes)

Looks like it might be a little like a recently contributed checker: http://llvm.org/viewvc/llvm-project?view=revision&revision=226172

In article <23afe066.8253.14b47490dc7.Coremail.wuming_81@163.com>,
    ²ÊÔÆ×·Ô <wuming_81@163.com> writes:

    recently, as an exercise, I have implemented a checker for clang static
analyzer.It checks the call of std::list::size( ) .

    It is more efficient to use containers empty() method to identify an
empty container.Codes like bellow :

    int f() {
        list<int> l;
        if (l.empty() == 0) {
        }
     }
    will be warned.

I think trunk clang-tidy contains a check/fix for this:

llvm/tools/clang/tools/extra/clang-tidy/readability/ContainerSizeEmpty.h:

/// \brief Checks whether a call to the \c size() method can be replaced with a
/// call to \c empty().
///
/// The emptiness of a container should be checked using the \c empty() method
/// instead of the \c size() method. It is not guaranteed that \c size() is a
/// constant-time function, and it is generally more efficient and also shows
/// clearer intent to use \c empty(). Furthermore some containers may implement
/// the \c empty() method but not implement the \c size() method. Using \c
/// empty() whenever possible makes it easier to switch to another container in
/// the future.

I'm not sure how clang-tidy interacts with the analyzer code. When I
tell clang-tidy to list all checks, it seems to list a bunch from the
static analyzer, but I don't know if clang-tidy provides fixes for them.

Yes, it does the same thing as my checker does.

I found many postential checkers here :
http://clang-analyzer.llvm.org/potential_checkers.html

which checkers are those no body is implementing ?

In article <43d6140f.83ac.14b47779201.Coremail.wuming_81@163.com>,
    ²ÊÔÆ×·Ô <wuming_81@163.com> writes:

Yes, it does the same thing as my checker does.

I found many postential checkers here :
http://clang-analyzer.llvm.org/potential_checkers.html

which checkers are those no body is implementing ?

It looks like the web page wasn't updated. IMO, clang is a fast
moving target and before attempting any contribution, always checkout
trunk and check for something similar to what you had in mind. On
more than one occasion I thought "A ha! Here's something small I can
contribute!" and then I go look in trunk to find out someone has
already done it :-).