Skip to content

Enum comparisons receive superflous and verbose casts #128

@TheDan64

Description

@TheDan64
typedef enum {
    FIND, ENTER
} ACTION;

int main(void) {
    ACTION action = FIND;
    if (action == FIND) {
        return 2;
    }

    return 1;
}

becomes

pub type ACTION = libc::c_uint;
pub const ENTER: ACTION = 1;
pub const FIND: ACTION = 0;
unsafe fn main_0() -> libc::c_int {
    let mut action: ACTION = FIND;
    if action as libc::c_uint == FIND as libc::c_int as libc::c_uint {
        return 2i32
    }
    return 1i32;
}

This does seem to be true to the AST (but is unnecessarily verbose):

  `-CompoundStmt 0x5293368 <col:16, line:12:1>
    |-DeclStmt 0x52931d0 <line:6:5, col:25>
    | `-VarDecl 0x5293130 <col:5, col:21> col:12 used action 'ACTION':'ACTION' cinit
    |   `-ImplicitCastExpr 0x52931b8 <col:21> 'ACTION':'ACTION' <IntegralCast>
    |     `-DeclRefExpr 0x5293190 <col:21> 'int' EnumConstant 0x5292dd0 'FIND' 'int'
    |-IfStmt 0x52932f8 <line:7:5, line:9:5>
    | |-<<<NULL>>>
    | |-<<<NULL>>>
    | |-BinaryOperator 0x5293280 <line:7:9, col:19> 'int' '=='
    | | |-ImplicitCastExpr 0x5293250 <col:9> 'unsigned int' <IntegralCast>
    | | | `-ImplicitCastExpr 0x5293238 <col:9> 'ACTION':'ACTION' <LValueToRValue>
    | | |   `-DeclRefExpr 0x52931e8 <col:9> 'ACTION':'ACTION' lvalue Var 0x5293130 'action' 'ACTION':'ACTION'
    | | `-ImplicitCastExpr 0x5293268 <col:19> 'unsigned int' <IntegralCast>
    | |   `-DeclRefExpr 0x5293210 <col:19> 'int' EnumConstant 0x5292dd0 'FIND' 'int'

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestreadabilityGenerated code is hard to read and can be simplified

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions