squid:S3516 - Methods returns should not be invariant - false positive

641 views
Skip to first unread message

s53...@gmail.com

unread,
Oct 25, 2017, 6:14:04 AM10/25/17
to SonarQube
Hello, this method gives an issue in the squid:S3516 - return should not be invariant

boolean checkRule(..)
{
  try
  {
    doSomething();
  }
  catch (Exception exc)
  {
    return false;
  }
  return true;
}

Please check, thanks,
Stefan.

Michael Gumowski

unread,
Oct 30, 2017, 1:13:47 PM10/30/17
to s53...@gmail.com, SonarQube
Hello Stefan,

Assuming you are speaking about java code, I can not reproduce the issue with latest release of SonarJava (4.14.0.11784).
Note that I had to modify a bit your example to make it self contained as following:

abstract class A {

  boolean checkRule() {
    try {
      doSomething();
    } catch (Exception exc) {
      return false;
    }
    return true;
  }

  abstract void doSomething() throws Exception;

}

Can you please check that you are using the latest version of SonarJava, and maybe provide a code snippet a bit more complete, which would allow me to reproduce the issue systematically?

Thanks,
Michael


--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/df89c1f3-857a-4cd4-be5b-dbdc231c140d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Michael Gumowski | SonarSource
Software Developer, Language Team
https://www.sonarsource.com

s53...@gmail.com

unread,
Oct 30, 2017, 3:28:49 PM10/30/17
to SonarQube
Hi Michael,
yes, Java,
yes, latest version as you stated (4.14.0.11784 - in another Eclipse I use SonarLint 3.2.0.2101706271328 which seems to use the same rule implementation).

Only difference in my code: class is public, not abstract, and checkRule is private, not default in my setting (but I get the issue also when it's default), and I can also omit doSomething(); (i.e. have an empty try block).

The funny thing is: when I put it as the only method in my class, I don't get this false positive, if I put it as one of many methods in an arbitrary class, I get that issue, even I don't call that method there.

So, I can get an issue, if I put the following piece of code in an arbitrary class as long as there's a bunch of other methods in the class - I don't have an idea what's exactly necessary - just try to put it in a bigger class you have at hand.

boolean checkFalseRule()
{
  try
  {
  }
  catch(Exception exc)
  {
    return false;
  }
  return true;
}

I get an issue about an empty code block and that false positive here (in the original code there's some code in the try block, and I get only the false positive).

For a second, I thought that SonarJava might check if the catch-block could be reached - but then, the issue would have also come if it's the only method in a class, so there must be something else.

My suggestion: put that small method in some bigger class and you'll get that issue - I tried a few with 500+ lines, and always got that false positive.

Thanks, see you,
Stefan.
Reply all
Reply to author
Forward
0 new messages