False Positive S3240 SonarQube requires applying ternary operator syntax when not needed?

Version: SonarQube Developer Edition Version 9.3 (build 51899)

Issue: S3240 The simplest possible condition syntax should be used

We have an ASP.NET Core 6 Web Api application, using C# 10.

When an unhandled exception is raised in the application, we have different methods that handle it in Production and in Development. As recommended by Microsoft documentation - in our Program.cs we have the following code:

if (app.Environment.IsDevelopment())
{
app.UseExceptionHandler(“/development-error-action”);
}
else
{
app.UseExceptionHandler(“/production-error-action”);
}

SonarQube raises an error and requires us to fix it with a ternary operator:

var appBuilder = app.Environment.IsDevelopment() ? app.UseExceptionHandler(“/development-error-action”) : app.UseExceptionHandler(“/production-error-action”);

So I need to introduce the variable appBuilder - which is absolutely not needed.

Generally, I believe this error raised by SonarQube is wrong. Since in this case - I don’t have to assign anything to a variable - I think this error shouldn’t be raised and the if-syntax should be kept.

I saw a similar issue here, but I am not sure it covers our case.

P.S. By the way - when I want to report a False-positive error - should I post here, or directly on the GitHub page?

No, It suggests you change your code like this:

app.UseExceptionHandler(app.Environment.IsDevelopment()
  ? "/development-error-action"
  : "/production-error-action");
1 Like

Hi @MiBuena, I think that the solution suggested by @Corniel is the right one. Rule S3240 suggests that the simplest possible condition syntax should be used. In this case, we don’t need to duplicate the app.UseExceptionHandler call.

Thank you! You can close the topic!

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