Skip to content

identity_op warns for bitflag constant #3430

@konstin

Description

@konstin

pyo3 has to map certain constants and flags from the python headers, which includes the code below:

// Flag bits for printing:
pub const Py_PRINT_RAW: c_int = 1; // No string quotes etc.

// PyBufferProcs contains bf_getcharbuffer
pub const Py_TPFLAGS_HAVE_GETCHARBUFFER: c_long = (1 << 0);

// PySequenceMethods contains sq_contains
pub const Py_TPFLAGS_HAVE_SEQUENCE_IN: c_long = (1 << 1);

// PySequenceMethods and PyNumberMethods contain in-place operators
pub const Py_TPFLAGS_HAVE_INPLACEOPS: c_long = (1 << 3);

// PyNumberMethods do their own coercion
pub const Py_TPFLAGS_CHECKTYPES: c_long = (1 << 4);

This code raises a warning:

warning: the operation is ineffective. Consider reducing it to `1`
   --> src/ffi2/object.rs:676:51
    |
676 | pub const Py_TPFLAGS_HAVE_GETCHARBUFFER: c_long = (1 << 0);
    |                                                   ^^^^^^^^
    |
    = note: #[warn(clippy::identity_op)] on by default

The explanation says:

This code can be removed without changing the meaning. So it just obscures what’s going on. Delete it mercilessly.

IMHO (1 << 0) is not obscuring the code, but relevant for pointing out that this constant is the first entry in a bitfield. Therefore this is imho a false positive in clippy.

cargo clippy -V: clippy 0.0.212 (a20599a 2018-11-01)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesL-styleLint: Belongs in the style lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions