From efb41d47018caf0d9c33c77188f2bc11d5fcbc74 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Thu, 25 Apr 2019 20:58:01 +0200 Subject: [PATCH] fix some more cppcheck warnings --- alib2cli/src/lexer/Lexer.h | 8 ++++---- alib2data/src/common/SparseBoolVector.hpp | 4 ++-- alib2std/src/extensions/common/tuple_common.hpp | 13 ++++++++++++- alib2std/src/extensions/container/string.hpp | 4 ++-- alib2std/src/extensions/container/variant.hpp | 8 ++++---- alib2std/src/extensions/functional.hpp | 2 +- alib2std/src/extensions/iterator.hpp | 8 ++++---- alib2std/src/extensions/typeinfo.cpp | 14 +++++++------- 8 files changed, 36 insertions(+), 25 deletions(-) diff --git a/alib2cli/src/lexer/Lexer.h b/alib2cli/src/lexer/Lexer.h index 80dce138ee..4ea693227a 100644 --- a/alib2cli/src/lexer/Lexer.h +++ b/alib2cli/src/lexer/Lexer.h @@ -141,16 +141,16 @@ public: }; template < class T > - Lexer ( T && source ) : Lexer ( std::unique_ptr < CharSequenceBase > ( new T ( std::forward < T && > ( source ) ) ) ) { + explicit Lexer ( T && source ) : Lexer ( std::unique_ptr < CharSequenceBase > ( new T ( std::forward < T && > ( source ) ) ) ) { } - Lexer ( std::unique_ptr < CharSequenceBase > source ) : m_source ( std::move ( source ) ), m_hint ( Hint::NONE ) { + explicit Lexer ( std::unique_ptr < CharSequenceBase > source ) : m_source ( std::move ( source ) ), m_hint ( Hint::NONE ) { } - Lexer ( std::string source ) : Lexer ( std::unique_ptr < CharSequenceBase > ( new StringCharSequence ( std::move ( source ) ) ) ) { + explicit Lexer ( std::string source ) : Lexer ( std::unique_ptr < CharSequenceBase > ( new StringCharSequence ( std::move ( source ) ) ) ) { } - Lexer ( const char * source ) : Lexer ( std::string ( source ) ) { + explicit Lexer ( const char * source ) : Lexer ( std::string ( source ) ) { } Token nextToken ( bool readNextLine = false ); diff --git a/alib2data/src/common/SparseBoolVector.hpp b/alib2data/src/common/SparseBoolVector.hpp index 853d3a0215..b894cb778f 100644 --- a/alib2data/src/common/SparseBoolVector.hpp +++ b/alib2data/src/common/SparseBoolVector.hpp @@ -202,7 +202,7 @@ public: res.push_back ( false ); for ( unsigned i = 0; i < sizeof ( unsigned ) * 8; ++i ) { - res.push_back ( elem.word & 1 << i ); + res.push_back ( elem.word & 1u << i ); } } @@ -320,7 +320,7 @@ public: friend std::ostream & operator << ( std::ostream & out, const common::SparseBoolVector::element & elem ) { out << "(" << elem.run << ", "; for ( unsigned i = 0; i < sizeof ( elem.word ) * 8; ++ i ) - out << (bool) ( elem.word & 1 << i ); + out << (bool) ( elem.word & 1u << i ); out << ")"; return out; } diff --git a/alib2std/src/extensions/common/tuple_common.hpp b/alib2std/src/extensions/common/tuple_common.hpp index 428f1b3c97..49d2415126 100644 --- a/alib2std/src/extensions/common/tuple_common.hpp +++ b/alib2std/src/extensions/common/tuple_common.hpp @@ -50,6 +50,16 @@ struct tuple_element < I, const volatile T > { typedef typename std::add_cv < typename ext::tuple_element < I, T >::type >::type type; }; +template < std::size_t I, class T > +struct tuple_element < I, T & > { + typedef typename std::add_lvalue_reference < typename ext::tuple_element < I, T >::type >::type type; +}; + +template < std::size_t I, class T > +struct tuple_element < I, T && > { + typedef typename std::add_rvalue_reference < typename ext::tuple_element < I, T >::type >::type type; +}; + template < unsigned I > @@ -82,8 +92,9 @@ template < unsigned I > struct foreach_helper { template < class Tuple, class Callable > static void foreach_fn ( Tuple && t, Callable callback ) { + auto && element = std::forward < typename tuple_element < I - 1, Tuple >::type > ( t.template get < I - 1 > ( ) ); foreach_helper < I - 1 >::foreach_fn ( std::forward < Tuple && > ( t ), callback ); - callback ( std::forward < Tuple && > ( t ).template get < I - 1 > ( ) ); + callback ( std::forward < typename tuple_element < I - 1, Tuple >::type > ( element ) ); } }; diff --git a/alib2std/src/extensions/container/string.hpp b/alib2std/src/extensions/container/string.hpp index 52e7df994e..61b6f1e6f6 100644 --- a/alib2std/src/extensions/container/string.hpp +++ b/alib2std/src/extensions/container/string.hpp @@ -81,7 +81,7 @@ public: * * \param the standard string */ - string ( const std::string & other ) noexcept : std::string ( other ) { + explicit string ( const std::string & other ) noexcept : std::string ( other ) { } /** @@ -90,7 +90,7 @@ public: * * \param the standard string */ - string ( std::string && other ) noexcept : std::string ( std::move ( other ) ) { + explicit string ( std::string && other ) noexcept : std::string ( std::move ( other ) ) { } /** diff --git a/alib2std/src/extensions/container/variant.hpp b/alib2std/src/extensions/container/variant.hpp index 5e014a9e04..4c3827aeb0 100644 --- a/alib2std/src/extensions/container/variant.hpp +++ b/alib2std/src/extensions/container/variant.hpp @@ -327,7 +327,7 @@ protected: * * \param id the identification of type stored inside the variant */ - variant_base( ext::type_index id ) : type_id ( id ) { + explicit variant_base( ext::type_index id ) : type_id ( id ) { // just to make the -Werror=maybe-uninitialized go away std::memset( & data, 0, ST ); } @@ -358,7 +358,7 @@ protected: * * \param id the identification of type stored inside the variant */ - variant_base( ext::type_index id ) : variant_base<ST, AT, Ts...>::variant_base ( id ) { } + explicit variant_base( ext::type_index id ) : variant_base<ST, AT, Ts...>::variant_base ( id ) { } public: /** @@ -408,7 +408,7 @@ protected: * * \param id the identification of type stored inside the variant */ - variant_base( ext::type_index id ) : variant_base<ST, AT, Ts...>::variant_base ( id ) { } + explicit variant_base( ext::type_index id ) : variant_base<ST, AT, Ts...>::variant_base ( id ) { } public: /** @@ -475,7 +475,7 @@ class variant : public variant_base < max ( SizeOf < Ts >::size ... ), max ( Ali * * \param out the output stream to use in function call operators. */ - VariantToStream ( std::ostream & out ) : m_out ( out ) { + explicit VariantToStream ( std::ostream & out ) : m_out ( out ) { } /** diff --git a/alib2std/src/extensions/functional.hpp b/alib2std/src/extensions/functional.hpp index 4992b050e2..46f6840371 100644 --- a/alib2std/src/extensions/functional.hpp +++ b/alib2std/src/extensions/functional.hpp @@ -78,7 +78,7 @@ template < class ... Ts > struct SliceComp { std::tuple < const Ts & ... > m_data; - SliceComp ( const Ts & ... data ) : m_data ( data ... ) { + explicit SliceComp ( const Ts & ... data ) : m_data ( data ... ) { } template < class T, size_t ... I > diff --git a/alib2std/src/extensions/iterator.hpp b/alib2std/src/extensions/iterator.hpp index 13f6c51e85..a190d583f4 100644 --- a/alib2std/src/extensions/iterator.hpp +++ b/alib2std/src/extensions/iterator.hpp @@ -421,7 +421,7 @@ public: * \brief * Constructor of the mover adaptor class. */ - value_mover ( T && param ) : m_Container ( std::move ( param ) ) {} + explicit value_mover ( T && param ) : m_Container ( std::move ( param ) ) {} /** * \brief @@ -458,7 +458,7 @@ public: * \brief * Constructor of the mover adaptor class. */ - reference_mover ( T && param ) : m_Container ( std::move ( param ) ) {} + explicit reference_mover ( T && param ) : m_Container ( std::move ( param ) ) {} /** * \brief @@ -528,7 +528,7 @@ public: * * \param container the adapted container */ - reverser ( T && container ) : m_Container ( std::forward < T && > ( container ) ) { + explicit reverser ( T && container ) : m_Container ( std::forward < T && > ( container ) ) { } /** @@ -1108,7 +1108,7 @@ callback_iterator < T > make_callback_iterator ( const std::function < void ( T template < typename map_type > class key_iterator : public map_type::const_iterator { public: - key_iterator ( const typename map_type::const_iterator & other ) : map_type::const_iterator ( other ) { + explicit key_iterator ( const typename map_type::const_iterator & other ) : map_type::const_iterator ( other ) { } const typename map_type::value_type::first_type & operator * ( ) const { diff --git a/alib2std/src/extensions/typeinfo.cpp b/alib2std/src/extensions/typeinfo.cpp index 771b5f0450..62844fbe23 100644 --- a/alib2std/src/extensions/typeinfo.cpp +++ b/alib2std/src/extensions/typeinfo.cpp @@ -13,12 +13,12 @@ namespace ext { std::string erase_template_info ( std::string str ) { for ( ; ; ) { - std::pair < std::string::iterator, std::string::iterator > range = ext::find_range ( str.begin ( ), str.end ( ), '<', '>' ); + std::pair < std::string::iterator, std::string::iterator > rangeRes = ext::find_range ( str.begin ( ), str.end ( ), '<', '>' ); - if ( range.first == range.second ) + if ( rangeRes.first == rangeRes.second ) break; - str.erase ( range.first, range.second ); + str.erase ( rangeRes.first, rangeRes.second ); } return str; } @@ -26,15 +26,15 @@ std::string erase_template_info ( std::string str ) { ext::vector < std::string > get_template_info ( std::string str ) { ext::vector < std::string > res; for ( ; ; ) { - std::pair < std::string::iterator, std::string::iterator > range = ext::find_range ( str.begin ( ), str.end ( ), '<', '>' ); + std::pair < std::string::iterator, std::string::iterator > rangeRes = ext::find_range ( str.begin ( ), str.end ( ), '<', '>' ); - if ( range.first == range.second ) + if ( rangeRes.first == rangeRes.second ) break; - std::string tmp ( range.first + 1, range.second - 1 ); + std::string tmp ( rangeRes.first + 1, rangeRes.second - 1 ); ext::trim ( tmp ); res.push_back ( tmp ); - str.erase ( range.first, range.second ); + str.erase ( rangeRes.first, rangeRes.second ); } return res; -- GitLab