Skip to content

Hoist common expressions out of ternary expressions #739

@kwhitefoot

Description

@kwhitefoot

I suspect this one might be difficult but I think it would be very useful.

New analyzer:

I deal with a lot of code that has if then statements that have very similar expressions in the two branches. CC0014 'You can use a ternary operator' helps but I would like to go furher and take common sub-expressions out of the two branches and place them before the ternary.

Before:

          x = If(b, 
                  a * c, 
                  d * c)

After:

          x = c * If(b, 
                        a,
                        d )

a, b,c, and d are arbitrary expressions not necessarily simple variable references.

Diagnostic Id: CC0115
Category: Style
Severity: Warning

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions