Skip to content

from_bits accepts non existing flags #275

@arturoc

Description

@arturoc
    #[test]
    fn test_from_bits_edge_cases() {
        bitflags! {
            struct Flags: u8 {
                const A = 0b00000001;
                const BC = 0b00000110;
            }
        }


        let flags = Flags::from_bits(0b00000100);
        assert!(flags.is_none());
    }

Unless I'm missing something this test should pass but it fails cause from_bits accepts flags that are not declared.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6fd4adbddc8b8740cbd35af2306073ca

This is related to this issue in the implementation of iterators in this PR #204 (comment) Using from_bits instead of from_bits_unchecked should allow to produce any valid flags that are not a combination of other flags but at the moment from_bits seems to accept any flag that is included in a combination even if it's not declared. I can try to send a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions