Rule S3241 FN: Methods should not return values that are never used (Code Smell warning) is not caught by sonar scanner and it is not displayed in the report on SonarQube 8.2 version community edition

I’m using SonarQube sonarqube-8.2.0.32929 community edition, sonar scanner sonar-scanner-msbuild-4.7.1.2311-net46

The warning for the rule S3241: Methods should not return values that are never used (Category: Code Smell) is not caught by sonar scanner and it is not displayed in the report.

E.g. Analyzing :

/// <title>Methods should not return values that are never used</title>
/// <summary>
/// Private methods are clearly intended for use only within their own scope. When such methods return values that are never used by 
/// any of their callers, then clearly there is no need to actually make the return, and it should be removed in the interests of efficiency and clarity.
/// </summary>

namespace SonarQubeToolVerification.CodeSmell.Warnings
{
    public class S3241Warning
    {
        private int NeverUsed(int neverUsedVal)
        {
            return neverUsedVal;
        }
    }
}

Waiting for Bug warning:
S3887: Mutable, non-private fields should not be “readonly”

but nothing displayed in the report.

Any suggestions?

thanks @mike1970fl03 , I’ve opened Fix S3241 FN: when the unused return value is a method parameter #3247 to track this false negative.

Thank you Andrei!

It’s working fine within version sonarqube-8.3.1

Felix!

Hello Andrei,

Within the version 8.4.1.35646 of SonarQube, the warning for he rule S3241: ‘Methods should not return values that are never used’ is not anymore caugth. It seems another rule has priority over the rule S3241 and this is S1144: ‘Unused private types or members should be removed’.

/// <title>Methods should not return values that are never used</title>
/// <summary>
/// Private methods are clearly intended for use only within their own scope. When such methods return values that are never used by
/// any of their callers, then clearly there is no need to actually make the return, and it should be removed in the interests of efficiency and clarity.
/// </summary>
namespace SonarQubeToolVerification.CodeSmell.Warnings
{
 public class S3241Warning
 {
    private int NeverUsed(int neverUsedVal)
    {
          return neverUsedVal;
    }

Remove the unused private method ‘NeverUsed’.Why is this an issue? csharpsquid: S1144

Do you have an example that is caugth by the rule S3241?

Thank you!

Felix


Actually, I did find positive and negative code that is working. The warning number is S3241 but the message is different.

namespace SonarQubeToolVerification.CodeSmell.Warnings
{
    public class S3241Warning
    {
        private int field = 5;

        public void Method()
        {
            this.ReturnValue();
        }

        private int ReturnValue()
        {
            return this.field++;
        }
    }
}

namespace SonarQubeToolVerification.CodeSmell.Pass
{
    public class S3241
    {
        int field = 5;

        public void Method()
        {
            this.field = this.ReturnValue();
        }

        private int ReturnValue()
        {
            return this.field++;
        }
    }
}

Thank you!

Felix

Hi @mike1970fl03 ,

Our rules don’t have priorities. Each rule operates and raises unaware of other rules.

This is explained in the S1144 rule description. It’s a dead code increasing maintenance cost. The warning is raised for the entire NeverUsed method, because you’re not calling the method.

Can you please clarify what’s your concern about

  • SonarQubeToolVerification.CodeSmell.Warnings.S3241Warning - it raises the issue where it should raise the issue.
  • SonarQubeToolVerification.CodeSmell.Pass.S3241 - it’s not raising the issue where it shouldn’t

So both seems to behave as specified to me.

Hello Pavel,

Thank you for your reply. I have found an compliant an non compliant example, so it is OK now. We started to use SonarQube in validation process on one of our project. We asked your company if there is any certificate for SonarQube and the response was No. So, before using SonarQube I needed to validate every rule we are using against our code that they are valid. It is required by FDA from USA when we are sending the submission for our product.

Thank you!

Felix

Hi Felix,

Our project is open sourced so you can take a look at our unit tests to make your life easier with writing test cases for each rule:

  1. Clone this repo https://github.com/SonarSource/sonar-dotnet
  2. Search in sonaranalyzer-dotnet\src\ for Sxxxx"; where xxxx is your rule id. Let’s say S3241";
  3. You’ll find implementation sonaranalyzer-dotnet\src\SonarAnalyzer.CSharp\Rules\UnusedReturnValue.cs. The UnusedReturnValue is important to find the tests.
  4. Take a look at sonaranalyzer-dotnet\tests\SonarAnalyzer.UnitTest\TestCases\UnusedReturnValue.cs

If you’d need to understand the link between the rule and the test case file, it’s in sonaranalyzer-dotnet\tests\SonarAnalyzer.UnitTest\Rules\UnusedReturnValue.cs

Thank you Pavel,

I really appreciate your information. It is very useful for us.

Felix

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.