Sonarcloud incorrectly reports that a boolean literal is unnecessary

This is code in a repo class for gracefully handling an error in mongoose (nodejs ORM for MongoDb) when a transaction is involved.

This message is misleading at best, but could easily introduce breaking changes. Had the developer followed the recommendation and removed the false, this would have broken the transaction handling and created additional runtime errors in the program. Indeed this is what had happened, except that the removal was caught by a human during PR review and was restored. The error depicted above is the result of that restoration.


Do you mean that along with the removal of literal, a negation should be added?

A negation is already present in the form of == false. It’s not a negation operator (but it means the same thing, and there is less chance for a later reader to miss this style of negation and create a bug).

Sonar shouldn’t be suggesting what is present is wrong or worse in any way, and definitely should not be advising that it be removed.

Sorry but I don’t follow your reasoning.

Rule is suggesting to change if (session.hasEnded == false) by if (!session.hasEnded). I believe second option is equivalent and more conventional and faster to understand. So to me this issue is valid.

If you have different opinion you can disable the rule on your projects.

The suggestion will literally break people’s code. It’s not telling me to do what you think it suggests. It is telling me to remove the == false, not replace it with anything or make any other changes.

On the topic of redundancy, if you will be able to see if (!IsReady()) as well as if (IsReady() == false) in a few years, then good for you. I still can (for now but probably not much longer), but have worked with others whose vision was affected by age or medical condition, and there were bugs (more than one, and by multiple people) due to missing the harder to notice/read negation operator. I’d like to keep following that practice without having sonar tell me that accessibility and readability should be ignored in this case solely because it’s what most of the herd does.

So as much as we may want the part of the rule that does work, we can’t use it because of the parts that are incorrect.

Which is confusing, this whole sonar program suite is about improving quality and reducing the chance of bugs. There are rules for requiring braces on single line conditionals; using == false instead of ! is the same reasoning, but sonar has a wobbly about it.

I created ticket to improve the message Improve message for S1125 `no-redundant-boolean` · Issue #2824 · SonarSource/SonarJS · GitHub but I don’t think we will go for recommending == false over negation as it’s more conventional.

It was probably conventional to have sidewalk aprons have a small step all the way around too, since everyone knows there is a small four inch step up that they have to navigate. Now they are required by law to have ramps. Sometimes the convention is wrong.

In this case it’s also placed in a rule that checks for redundancy when it’s not a redundant condition.

1 Like

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