From 705151e9f93a4896f17b3891a7f3880659ba2337 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= <klinkovsky@mmg.fjfi.cvut.cz>
Date: Thu, 20 Jan 2022 21:20:32 +0100
Subject: [PATCH] clang-tidy: use .empty() to check for emptiness of a
 container [readability-container-size-empty]

See https://clang.llvm.org/extra/clang-tidy/checks/readability-container-size-empty.html
---
 src/TNL/Benchmarks/Utils.h                 |  2 +-
 src/TNL/Config/ConfigEntry.h               |  4 +---
 src/TNL/Config/parseCommandLine.h          | 14 +++++++-------
 src/TNL/Meshes/Readers/MeshReader.h        |  4 ++--
 src/TNL/Meshes/Readers/PVTIReader.h        |  6 +++---
 src/TNL/Meshes/Readers/PVTUReader.h        |  6 +++---
 src/TNL/Meshes/Readers/XMLVTK.h            |  4 ++--
 src/TNL/Object.hpp                         |  2 +-
 src/TNL/Solvers/IterativeSolverMonitor.hpp |  2 +-
 src/TNL/String.hpp                         |  6 +++---
 src/TNL/SystemInfo.hpp                     |  2 +-
 11 files changed, 25 insertions(+), 27 deletions(-)

diff --git a/src/TNL/Benchmarks/Utils.h b/src/TNL/Benchmarks/Utils.h
index fe57c891ee..b6b3b4263a 100644
--- a/src/TNL/Benchmarks/Utils.h
+++ b/src/TNL/Benchmarks/Utils.h
@@ -158,7 +158,7 @@ writeMapAsJson( const std::map< std::string, std::string >& data, std::string fi
 {
    namespace fs = std::experimental::filesystem;
 
-   if( newExtension != "" ) {
+   if( ! newExtension.empty() ) {
       const fs::path oldPath = filename;
       const fs::path newPath = oldPath.parent_path() / ( oldPath.stem().string() + newExtension );
       filename = newPath;
diff --git a/src/TNL/Config/ConfigEntry.h b/src/TNL/Config/ConfigEntry.h
index b589e61892..f7f777b228 100644
--- a/src/TNL/Config/ConfigEntry.h
+++ b/src/TNL/Config/ConfigEntry.h
@@ -59,9 +59,7 @@ public:
    bool
    hasEnumValues() const override
    {
-      if( enumValues.size() > 0 )
-         return true;
-      return false;
+      return ! enumValues.empty();
    }
 
    void
diff --git a/src/TNL/Config/parseCommandLine.h b/src/TNL/Config/parseCommandLine.h
index db48c08fb3..cc41fddc7e 100644
--- a/src/TNL/Config/parseCommandLine.h
+++ b/src/TNL/Config/parseCommandLine.h
@@ -153,7 +153,7 @@ checkMissingEntries( const ConfigDescription& config,
       if( entryBase->isRequired() && ! parameters.checkParameter( entry_name ) )
          missingParameters.push_back( entry_name );
    }
-   if( missingParameters.size() > 0 ) {
+   if( ! missingParameters.empty() ) {
       std::cerr << "Some mandatory parameters are misssing. They are listed at the end." << std::endl;
       if( printUsage )
          Config::printUsage( config, programName );
@@ -256,7 +256,7 @@ parseCommandLine( int argc,
          if( value.empty() )
             throw Exceptions::ConfigError( "Missing value for the parameter " + option + "." );
          const std::vector< String > parsedEntryType = entryType.split();
-         if( parsedEntryType.size() == 0 )
+         if( parsedEntryType.empty() )
             throw Exceptions::ConfigError( "Internal error: Unknown config entry type " + entryType + "." );
          if( parsedEntryType[ 0 ] == "list" ) {
             std::vector< bool > bool_list;
@@ -298,15 +298,15 @@ parseCommandLine( int argc,
                   // this will not happen if all entry types are handled above
                   throw std::runtime_error( "Function parseCommandLine encountered unsupported entry type: " + entryType );
             }
-            if( bool_list.size() )
+            if( ! bool_list.empty() )
                parameters.addParameter< std::vector< bool > >( option, bool_list );
-            if( integer_list.size() )
+            if( ! integer_list.empty() )
                parameters.addParameter< std::vector< Integer > >( option, integer_list );
-            if( unsigned_integer_list.size() )
+            if( ! unsigned_integer_list.empty() )
                parameters.addParameter< std::vector< UnsignedInteger > >( option, unsigned_integer_list );
-            if( real_list.size() )
+            if( ! real_list.empty() )
                parameters.addParameter< std::vector< double > >( option, real_list );
-            if( string_list.size() )
+            if( ! string_list.empty() )
                parameters.addParameter< std::vector< std::string > >( option, string_list );
          }
          else {
diff --git a/src/TNL/Meshes/Readers/MeshReader.h b/src/TNL/Meshes/Readers/MeshReader.h
index b07168c216..556a363da1 100644
--- a/src/TNL/Meshes/Readers/MeshReader.h
+++ b/src/TNL/Meshes/Readers/MeshReader.h
@@ -92,7 +92,7 @@ public:
    loadMesh( MeshType& mesh )
    {
       // check that detectMesh has been called
-      if( meshType == "" )
+      if( meshType.empty() )
          detectMesh();
 
       // check if we have a grid
@@ -151,7 +151,7 @@ public:
    loadMesh( MeshType& mesh )
    {
       // check that detectMesh has been called
-      if( meshType == "" )
+      if( meshType.empty() )
          detectMesh();
 
       // check if we have an unstructured mesh
diff --git a/src/TNL/Meshes/Readers/PVTIReader.h b/src/TNL/Meshes/Readers/PVTIReader.h
index 47178e227b..e8c0c7eaf7 100644
--- a/src/TNL/Meshes/Readers/PVTIReader.h
+++ b/src/TNL/Meshes/Readers/PVTIReader.h
@@ -125,7 +125,7 @@ class PVTIReader : public XMLVTK
       const XMLElement* piece = getChildSafe( datasetElement, "Piece" );
       while( piece ) {
          const std::string source = getAttributeString( piece, "Source" );
-         if( source != "" ) {
+         if( ! source.empty() ) {
             pieceSources.push_back( getSourcePath( source ) );
          }
          else
@@ -133,7 +133,7 @@ class PVTIReader : public XMLVTK
          // find next
          piece = piece->NextSiblingElement( "Piece" );
       }
-      if( pieceSources.size() == 0 )
+      if( pieceSources.empty() )
          throw MeshReaderError( "PVTIReader", "the file does not contain any <Piece> element." );
 
       // check that the number of pieces matches the number of MPI ranks
@@ -210,7 +210,7 @@ public:
    loadMesh( MeshType& mesh )
    {
       // check that detectMesh has been called
-      if( meshType == "" )
+      if( meshType.empty() )
          detectMesh();
 
       // check if we have a distributed grid
diff --git a/src/TNL/Meshes/Readers/PVTUReader.h b/src/TNL/Meshes/Readers/PVTUReader.h
index 330045d929..293d62810e 100644
--- a/src/TNL/Meshes/Readers/PVTUReader.h
+++ b/src/TNL/Meshes/Readers/PVTUReader.h
@@ -53,7 +53,7 @@ class PVTUReader : public XMLVTK
       const XMLElement* piece = getChildSafe( datasetElement, "Piece" );
       while( piece ) {
          const std::string source = getAttributeString( piece, "Source" );
-         if( source != "" ) {
+         if( ! source.empty() ) {
             pieceSources.push_back( getSourcePath( source ) );
          }
          else
@@ -61,7 +61,7 @@ class PVTUReader : public XMLVTK
          // find next
          piece = piece->NextSiblingElement( "Piece" );
       }
-      if( pieceSources.size() == 0 )
+      if( pieceSources.empty() )
          throw MeshReaderError( "PVTUReader", "the file does not contain any <Piece> element." );
 
       // check that the number of pieces matches the number of MPI ranks
@@ -144,7 +144,7 @@ public:
    loadMesh( MeshType& mesh )
    {
       // check that detectMesh has been called
-      if( meshType == "" )
+      if( meshType.empty() )
          detectMesh();
 
       // check if we have a distributed unstructured mesh
diff --git a/src/TNL/Meshes/Readers/XMLVTK.h b/src/TNL/Meshes/Readers/XMLVTK.h
index 10e9be0a4c..b7cd96e8af 100644
--- a/src/TNL/Meshes/Readers/XMLVTK.h
+++ b/src/TNL/Meshes/Readers/XMLVTK.h
@@ -198,7 +198,7 @@ protected:
       while( *block != '\0' && std::isspace( *block ) )
          ++block;
 
-      if( compressor == "" ) {
+      if( compressor.empty() ) {
          std::size_t data_size = 0;
          const T* data_ptr = nullptr;
          std::pair< std::size_t, std::unique_ptr< std::uint8_t[] > > decoded_data =
@@ -393,7 +393,7 @@ public:
       compressor = getAttributeString( elem, "compressor", "<none>" );
       if( compressor == "<none>" )
          compressor = "";
-      if( compressor != "" && compressor != "vtkZLibDataCompressor" )
+      if( ! compressor.empty() && compressor != "vtkZLibDataCompressor" )
          throw MeshReaderError( "XMLVTK",
                                 "unsupported compressor type: " + compressor + " (only vtkZLibDataCompressor is supported)" );
 
diff --git a/src/TNL/Object.hpp b/src/TNL/Object.hpp
index 7e0d2f318f..c9f7f4e96d 100644
--- a/src/TNL/Object.hpp
+++ b/src/TNL/Object.hpp
@@ -113,7 +113,7 @@ parseObjectType( const String& objectType )
          templateBrackets++;
       if( ! templateBrackets ) {
          if( objectType[ i ] == ',' || objectType[ i ] == '>' ) {
-            if( buffer != "" ) {
+            if( ! buffer.empty() ) {
                parsedObjectType.push_back( buffer.strip( ' ' ) );
                buffer.clear();
             }
diff --git a/src/TNL/Solvers/IterativeSolverMonitor.hpp b/src/TNL/Solvers/IterativeSolverMonitor.hpp
index d0ca7e49dc..dff01e472b 100644
--- a/src/TNL/Solvers/IterativeSolverMonitor.hpp
+++ b/src/TNL/Solvers/IterativeSolverMonitor.hpp
@@ -149,7 +149,7 @@ IterativeSolverMonitor< Real, Index >::refresh()
       }
 
       const std::string displayed_stage = ( saved ) ? saved_stage : stage;
-      if( displayed_stage.length() && free > 5 ) {
+      if( ! displayed_stage.empty() && free > 5 ) {
          if( (int) displayed_stage.length() <= free - 2 ) {
             std::cout << "  " << displayed_stage;
             free -= ( 2 + displayed_stage.length() );
diff --git a/src/TNL/String.hpp b/src/TNL/String.hpp
index 8da511f14b..5c46b55394 100644
--- a/src/TNL/String.hpp
+++ b/src/TNL/String.hpp
@@ -186,7 +186,7 @@ String::operator!=( const String& str ) const
 
 inline String::operator bool() const
 {
-   return getLength();
+   return ! empty();
 }
 
 inline bool
@@ -239,14 +239,14 @@ String::split( char separator, SplitSkip skip ) const
    String s;
    for( int i = 0; i < this->getLength(); i++ ) {
       if( ( *this )[ i ] == separator ) {
-         if( skip != SplitSkip::SkipEmpty || s != "" )
+         if( skip != SplitSkip::SkipEmpty || ! s.empty() )
             parts.push_back( s );
          s = "";
       }
       else
          s += ( *this )[ i ];
    }
-   if( skip != SplitSkip::SkipEmpty || s != "" )
+   if( skip != SplitSkip::SkipEmpty || ! s.empty() )
       parts.push_back( s );
    return parts;
 }
diff --git a/src/TNL/SystemInfo.hpp b/src/TNL/SystemInfo.hpp
index c95e61d3f1..52a029cf81 100644
--- a/src/TNL/SystemInfo.hpp
+++ b/src/TNL/SystemInfo.hpp
@@ -104,7 +104,7 @@ inline String
 SystemInfo::getCPUModelName( int cpu_id )
 {
    static String CPUModelName;
-   if( CPUModelName == "" ) {
+   if( CPUModelName.empty() ) {
       CPUInfo info = parseCPUInfo();
       CPUModelName = info.CPUModelName;
    }
-- 
GitLab