False positive with ternary in C# code

70 views
Skip to first unread message

anthony...@just-eat.com

unread,
Jan 4, 2018, 11:18:57 AM1/4/18
to SonarQube
Hi

Sonarqube reports a bug:
"This operation returns the same value whether the condition is 'true' or 'false'." because "When the second and third operands of a ternary operator are the same, the operator will always return the same value regardless of the condition. Either the operator itself is pointless, or a mistake was made in coding it."


but the code in question is:

  string resultBucket = response.IsSuccessStatusCode
      ? $"{statsBucket}.success.{(int) response.StatusCode}"
      : $"{statsBucket}.error.{(int) response.StatusCode}";

these 2 operands are clearly *not* the same. They differ in the "success" / "failure" part. Looks like an issue in Sonarqube to me?

anthony...@just-eat.com

unread,
Jan 5, 2018, 7:09:46 AM1/5/18
to SonarQube
So today I changed the code to

            var successOrError = response.IsSuccessStatusCode ? "success" : "error";
            var resultBucket = $"{statsBucket}.{successOrError}.{(int)response.StatusCode}";

Which is IMHO better code, and SonarQube is happy with it - no "bugs". 
If SonarQube was trying to tell me "your ternary is too complex, simplify it" then I would agree, but the message is still incorrect.

Amaury Leve

unread,
Jan 5, 2018, 7:56:51 AM1/5/18
to anthony...@just-eat.com, SonarQube
Hi Anthony,

Thanks for reporting this bug. We are aware of it and sadly we cannot fix it while keeping compatibility with Roslyn 1.0 (VS 2015 no update) because the bugs is on Roslyn side.


Cheers,
Amaury


This is an e-mail from the company JUST EAT plc. This e-mail may contain confidential information and/or privileged or personal advice. This e-mail is intended for the addressee(s) stated above only. If you receive this e-mail by mistake, please return it to JUST EAT plc without producing, distributing or retaining copies hereof. Thank you.

--
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/91662003-cdcb-4e02-80d4-86ad1f534b57%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Amaury Levé | SonarSource

Software Developer - .Net Team

http://sonarsource.com


Are you using SonarLint in your IDE? 
Reply all
Reply to author
Forward
0 new messages