rspec/rules/S1006/cfamily/rule.adoc
Fred Tingaud 51369b610e
Make sure that includes are always surrounded by empty lines (#2270)
When an include is not surrounded by empty lines, its content is inlined
on the same line as the adjacent content. That can lead to broken tags
and other display issues.
This PR fixes all such includes and introduces a validation step that
forbids introducing the same problem again.
2023-06-22 10:38:01 +02:00

87 lines
1.7 KiB
Plaintext

== Why is this an issue?
Overriding the default parameter value inherited from a parent class will lead to unexpected results when the child class is referenced from a pointer to the parent class.
=== Noncompliant code example
[source,cpp]
----
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
[source,cpp]
----
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)
}
----
== Resources
* MISRA {cpp} 2008, 8-3-1 - Parameters in a overriding virtual function shall either use the same default arguments as the function they override, or else shall not specify any default arguments.
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove the default value for parameter "xxx" or set it to the same value as in the base class.
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]