Menu ▾ ▴

FP: syntaxError in ternary

2026-10-02
2026-10-04
  • Dan Workman

    Dan Workman - 2026-10-02

    Came in when upgrading from 2.21 to 2.22

    Running
    cppcheck --std=c++20 --template=gcc
    on

    #include <algorithm>
    #include <vector>
    
    struct Box
    {
      float xMin, yMin, xMax, yMax;
      float Height() const { return yMax - yMin; }
    };
    
    struct Processor
    {
      std::vector<Box> Update(const std::vector<Box>& input, int begin, int end) const;
    };
    
    std::vector<Box> Processor::Update(const std::vector<Box>& input, int begin, int end) const
    {
      std::vector<Box> output;
      const auto visit = [&](int first, int last)
      {
        for (const auto& item : input)
        {
          const auto current = Box{item.xMin, item.yMin, item.xMax, item.yMax};
          auto previous = std::find_if(output.begin(), output.end(), [&](const Box& box) { return box.xMin == current.xMin; });
          if (previous != output.end())
          {
            const float right = last - current.xMax;
            const float left = current.xMin - first;
            const float previousRight = last - previous->xMax;
            const float previousLeft = previous->xMin - first;
    
            const bool replace = (right == previousRight && left == previousLeft)
                               ? previous->Height() < current.Height()
                               : std::min(previousLeft, previousRight) > std::min(left, right);
            if (!replace)
              continue;
    
            *previous = current;
          }
    
          output.push_back(current);
        }
      };
    
      visit(begin, end);
      return output;
    }
    

    gives

    repro.cpp:31:32: warning: syntax error [syntaxError]
                        ? previous->Height() < current.Height()
                                             ^
    

    A workaround is to put brackets around it

     

    Last edit: Dan Workman 2026-10-02
  • CHR

    CHR - 2026-10-02

    Reduced:

    struct S {
        bool f() const;
        int g() const;
        int x, y;
    };
    bool S::f() const {
        const bool b = x ? g() < 0 : 0 > y;
        return b;
    }
    
     
  • CHR

    CHR - 2026-10-04

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/15086

     

Log in to post a comment.