C++: Ternary operator / S1174

Hello,

Why does Sonar does not allow ternary operator to define const “variable” ?
It suggests if/else, but then, the variable cannot be const.

1 Like

Hey @Oodini

S1174 is a Java-only rule that doesn’t seem related to your question. Can you double-check the Rule ID?

It’s also a big help when you report questions like this and share some code samples to demonstrate your point.

You’re right: after having checked, it is a Java rule. So why does it appear in Sonar for VS 8.10.0.11758 for my C++ project ??
My VS 2022 project is linked to the Sonar project, and this one is declared a C/C++ project.
In VS 2022, the “Sonar issue vizualization” tab is empty, with just the text:

Please select a SonarQube for Visual Studio issue with secondary locations.
Click here to go online and find out more about this and other SonarQube for Visual Studio features.

How about sharing a screenshot of what you’re seeing?

Actually, this rule is for many languages (C++ included):
https://next.sonarqube.com/sonarqube/coding_rules?q=S1774&selected=javascript%3AS1774
(so my question in the original post is still valid)

Below a screen capture of what I described in the previous message:

Ah, okay, S1774 is a different rule than S1174 :wink: But don’t worry, I mix up Rule IDs all the time.

I’ve flagged this for attention! That being said, this rule is not included in the built-in Sonar Way Quality Profile – you must have added it. If it’s too noisy, I suggest removing it.

You’re right, it has been added in the quality profile.
Anyway, I think this rule should be amended.

Hi @Oodini ,
As mentioned by Colin, this rule is not part of the default SonarWay profile. The rule was developed long ago and kept in case some specific projects have a use for it, but it is not a rule we would recommend enabling in your code.

1 Like