[cfe-dev] a patch for static analyzer to check : std::list::empty()

Richard legalize at xmission.com
Sun Feb 1 14:53:59 PST 2015


In article <23afe066.8253.14b47490dc7.Coremail.wuming_81 at 163.com>,
    ²ÊÔÆ×·Ô <wuming_81 at 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.
-- 
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
     The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
         The Terminals Wiki <http://terminals.classiccmp.org>
  Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>



More information about the cfe-dev mailing list