@@ -2899,10 +2899,21 @@ void CheckStl::useStlAlgorithm()
28992899 return !astIsContainer (tok); // don't warn for containers, where overloaded operators can be costly
29002900 };
29012901
2902- auto isConditionWithoutSideEffects = [this ](const Token* tok) -> bool {
2902+ enum class ConditionOpType : std::uint8_t { OTHER, MIN, MAX };
2903+ auto isConditionWithoutSideEffects = [this ](const Token* tok, ConditionOpType& type) -> bool {
29032904 if (!Token::simpleMatch (tok, " {" ) || !Token::simpleMatch (tok->previous (), " )" ))
29042905 return false ;
2905- return isConstExpression (tok->linkAt (-1 )->astOperand2 (), mSettings ->library );
2906+ const Token* condTok = tok->linkAt (-1 )->astOperand2 ();
2907+ if (isConstExpression (condTok, mSettings ->library )) {
2908+ if (condTok->str () == " <" )
2909+ type = ConditionOpType::MIN;
2910+ else if (condTok->str () == " >" )
2911+ type = ConditionOpType::MAX;
2912+ else
2913+ type = ConditionOpType::OTHER;
2914+ return true ;
2915+ }
2916+ return false ;
29062917 };
29072918
29082919 auto isAccumulation = [](const Token* tok, int varId) {
@@ -3035,14 +3046,27 @@ void CheckStl::useStlAlgorithm()
30353046 else
30363047 algo = " std::replace_if" ;
30373048 } else {
3049+ ConditionOpType type{};
30383050 if (addByOne (assignTok, assignVarId))
30393051 algo = " std::count_if" ;
30403052 else if (accumulateBoolLiteral (assignTok, assignVarId))
30413053 algo = " std::any_of, std::all_of, std::none_of, or std::accumulate" ;
30423054 else if (assignTok->str () != " =" )
30433055 algo = " std::accumulate" ;
3044- else if (hasBreak && isConditionWithoutSideEffects (condBodyTok))
3045- algo = " std::any_of, std::all_of, std::none_of" ;
3056+ else if (isConditionWithoutSideEffects (condBodyTok, type)) {
3057+ if (hasBreak)
3058+ algo = " std::any_of, std::all_of, std::none_of" ;
3059+ else if (assignTok->astOperand2 ()->varId () == loopVar->varId ()) {
3060+ if (type == ConditionOpType::MIN)
3061+ algo = " std::min_element" ;
3062+ else if (type == ConditionOpType::MAX)
3063+ algo = " std::max_element" ;
3064+ else
3065+ continue ;
3066+ }
3067+ else
3068+ continue ;
3069+ }
30463070 else
30473071 continue ;
30483072 }
0 commit comments