Function pointer caused so many false positive

  • What language is this for?
    C
  • Which rule?
    C S1172
  • Why do you believe it’s a false-positive/false-negative?
    It is normal usage in C
  • Are you using
    SonarQube Developer Edition Version 9.9 (build 65466)
  • How can we reproduce the problem? Give us a self-contained snippet (best) or screenshot (good)
    typedef void (*Run)(int, int);

void BirdRun(int leg, int wing)
{
printf(“birds run %d”, wing);
}

void BirdRun(int leg, int wing)
{
printf(“birds run %d”, wing);
}

void DogRun(int leg, int wing)
{
printf(“dogs run %d”, leg);
}

int main(int argc, char** argv)
{
Run dog = DogRun;
dog(1, 2);
Run bird = BirdRun;
bird(3, 4);
return 0;
}

Remove the unused parameter “leg”.

Hi,

That is not a false positive, because there is an unused named parameter.
Either remove the name or comment them out, up to you.

#include <stdio.h>

// Option 1: Comment the name
void BirdRun(int /*leg*/, int wing)
{
printf("birds run %d", wing);
}

// Option 2: Remove the name
void DogRun(int leg, int)
{
printf("dogs run %d", leg);
}

typedef void (*Run)(int, int);

int main(int argc, char** argv)
{
    Run dog = DogRun;
    dog(1, 2);
    Run bird = BirdRun;
    bird(3, 4);
    return 0;
}

Regards,
Alejandro

Thanks for reply,I comment the name,but get an error like this “error: parameter name omitted”.

I use clang as compiler

True, clang emits a warning (I guess you use -Werror), but gcc does not. The classical approach for C, that I should have mentioned, for unused parameters is to cast them to (void). i.e.

void BirdRun(int leg, int wing)
{
    (void)leg;
    printf("birds run %d", wing);
}