V524. It is suspicious that the body of 'Foo_1' function is fully equivalent to the body of 'Foo_2' function.
The analyzer has detected two functions with identical implementations. Although having two identical functions is not an error, they should still be reviewed.
The diagnostic rule detects the following error types:
class Point
{
...
float GetX() { return m_x; }
float GetY() { return m_x; }
};
A typo causes two logically different functions to perform the same actions. The fixed code:
float GetX() { return m_x; }
float GetY() { return m_y; }
In the example, the identical bodies of the GetX() and GetY() functions indicate an error. However, issuing warnings for all identical functions could result in many false positives. Therefore, the analyzer has a number of exceptions where a warning about identical function bodies is not required.
Some examples of exceptions:
- the analyzer does not report about identical functions' bodies if they do not use variables except for arguments. For example:
bool IsXYZ() { return true; }; - functions use static objects and have different inner states. For example:
int Get() { static int x = 1; return x++; }; - functions are type casting operators;
- functions with identical bodies are repeated more than twice;
- and so on.
However, in some cases the analyzer may mistakenly determine identical function bodies as an error, even if they are not dangerous:
PolynomialMod2 Plus(const PolynomialMod2 &b) const
{return Xor(b);}
PolynomialMod2 Minus(const PolynomialMod2 &b) const
{return Xor(b);}
You can suppress false positives using several approaches:
- If false positives refer to files of external libraries, add this library (i.e. its path) to exceptions.
- If false positives refer to your own code, use the comment of the
//-V524type to suppress false-positive warnings. - If there are too many false positives, completely disable this diagnostic rule in the analyzer's settings
- Modify the code to make one function call another with the same code.
The last approach is often the best because it reduces the amount of code and makes it easier to support. You need to edit only one function instead of both.
The following is a real-world example where calling one function from another would be beneficial:
static void PreSave(void) {
int x;
for(x=0;x<TotalSides;x++) {
int b;
for(b=0; b<65500; b++)
diskdata[x][b] ^= diskdatao[x][b];
}
}
static void PostSave (void) {
int x;
for(x=0;x<TotalSides;x++) {
int b;
for(b=0; b<65500; b++)
diskdata[x][b] ^= diskdatao[x][b];
}
}
The code can be modified with the following:
static void PreSave(void) {
int x;
for(x=0;x<TotalSides;x++) {
int b;
for(b=0; b<65500; b++)
diskdata[x][b] ^= diskdatao[x][b];
}
}
static void PostSave (void) {
PreSave();
}
There was no error to fix in this example, but refactoring removed the V524 warning and simplified the code.
You can look at examples of errors detected by the V524 diagnostic. |