The analyzer generates “warning S1125: Remove the unnecessary Boolean literal(s).” on the line of code for the ‘required’ boolean assignment. In the code snippet below, ‘item’ object is an optional (can be null) ‘to be cloned’ item of DataItem type. Code intent is 'use values from the cloned item, if there is one, otherwise use defaults. ‘titleHtml’ property is of string type (and no warning is generated), ‘required’ is a bool type. Looks like S1905 warning is caused by the first (true) part of the ternary statement, but since 2nd (false) ternary statement is not a ‘true’ literal, the warning really should not be shown.
I’ve took a short look and as far as I can tell the solution suggested by the code fix provider required = !(item == null) && item.required respects the rule specifications.
Yes it is a boolean literal, however that is not the point. Contrary to the rule violation text, this literal is 100% necessary. Removing it makes the code take the wrong condition and creates many possible bugs. For me it reports that its S1125 and no S1905.
I would like to know in both this case and the OP’s, if this code is wrong, then what is SonarSource’s idea of the correct and clearer/easier to read and understand code that should be getting used instead? To me this just looks like the incorrect/broken redundant boolean evaluation of S1125 is leaking further.