Identical sub-expression when calling functions with side-effects

Noticed using SonarLint for Visual Studio (

Rule 1764 which detects identical sub-expressions on both sides of operator && (very useful for finding copy&paste errors) should not be triggered if the expressions are non-const function calls. Example

if (!DoSomething()) return xml->GoToParentNode() && xml->GoToParentNode();

As you can probably tell, this return jumps up twice in the XML structure, if the first jump is successful. Otherwise it will return false (GoToParentNode has bool return type).

While it might be debatable if this is ideal and whether it wouldn’t be clearer to write this in multiple lines with individiual ifs etc., this is valid and the most succinct way.

In general, I would argue that calling the identical non-const function twice is not an indication of an error, and there is no reason why one should not connect this via && if the second calls depends on the success of the first. As such, I propose excluding non-const function call expressions from this rule or splitting the rule up to allow disabling this case depending on style preference.

Hello @awjan,

Thanks for the detailed explanation.
I agree with your point. I created this ticket to improve the rule.

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