Some C++ sonar cloud bad smell question

  • versions used (SonarQube, Scanner, Plugin, and any relevant extension)
    sonar cloud
  • error observed (wrap logs/code around triple quote ``` for proper formatting)
    code smell:
    (1)

(2)


class Rectangle : public Shape
{
public:
 draw(E_ShapeColor color = E_RED)  { .. }

Looking forward to your reply, thanks

Hello @freeeyes,
For the first issue, it seems like a false-positive. I created this issue to investigate the problem. Feel free to watch it.

For the second issue, you are right the example needs modification. I’ll update it to:
Noncompliant Code Example:

enum E_ShapeColor {E_RED, E_GREEN, E_BLUE};    
class Shape
    { 
    public:
      virtual void draw(E_ShapeColor color = E_RED) const 
      {
        ...
      }
    };
    class Rectangle : public Shape 
    {
    public:
      virtual void draw(E_ShapeColor color = E_BLUE) const override // Non-compliant
      {
       ... 
      }
    };
    int main(){
      Shape *shape = new Rectangle{};
      shape->draw();  // unexpectedly calls Rectangle::draw(RED)
    }

Compliant Solution:

enum E_ShapeColor {E_RED, E_GREEN, E_BLUE}; 
class Shape
{ 
public:
  virtual void draw(E_ShapeColor color = E_RED) const 
  {
    ...
  }
};

class Rectangle : public Shape 
{
public:
  virtual void draw(E_ShapeColor color) const override
  // OR: virtual void draw(E_ShapeColor color = E_RED) const override
  {
    ... 
  }
}; 

int main(){
  Shape *shape = new Rectangle{};
  shape->draw(); // expectedly calls Rectangle::draw(RED)
}

On the other side, your code still has an issue. The value of the MACRO __LINE__ will be different between the base and the derived functions. The virtual call to the derived class function will have the value of __LINE__ of the base class function.

Thanks a lot for your feedback !

thanks for your feedback.
about second question.
I try.

virtual IBuffPacket* Create(const char* pFileName, uint32 u4Line);
1 Like