csharpsquid:S3358 considers lambdas for nesting

Version: SonarQube 9.5

I have noticed this code snippet raises S3358 (avoid nested ternary operators):

public Func<TObject, ReportCellStyle> GetFormat<TObject>(bool withTime, Func<TObject, bool> withTimeAccessor)
{
    return withTimeAccessor != null
        ? item => withTimeAccessor(item) ? ReportCellStyle.DateTime : ReportCellStyle.Date
        : item => withTime ? ReportCellStyle.DateTime : ReportCellStyle.Date;
}

This is a case of a ternary operator returning lambdas which contain a ternary operator.
This is different from having nested ternary operators in the same “code flow” and I don’t know if this rule should apply to this specific case.

I have encountered this issue with C# but it might apply to other languages with similar constructs.

Hello,

I think this is a false positive. I created a PR here to fix it. We will discuss it internally, and you can track progress on GitHub at the PR.

Thanks.

I’m not sure if I agree on this being a false positive; the rule is about readability, and lambda’s involved do make a difference in that respect.

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