Skip to content

False negative: manual_rotate doesn't catch variable shift amounts #13028

@llogiq

Description

@llogiq

Summary

For now, manual_rotate only lints when the shift amounts are constant. However, there is also the option of shifting by some expression x and either BIT_WIDTH - x or (BIT_WIDTH - 1) ^ x. This is a bit harder to detect, as we don't know whether the subtraction will be in the first or second part of the binary operation, but it should certainly be possible.

cc @frp who implemented the original lint.

Lint Name

manual_rotate

Reproducer

I tried this code:

let s = 5;
let x = 123456_u32;

let _ = (x << s) | (x >> (32 - s));
let _ = (x << s) | (x >> (31 ^ s));

The lint should be extended to catch both cases, suggesting x.rotate_left(s).

Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions