Struct clippy::bit_mask::BitMask [] [src]

pub struct BitMask;

Checks for incompatible bit masks in comparisons, e.g. x & 1 == 2. This cannot work because the bit that makes up the value two was zeroed out by the bit-and with 1. So the formula for detecting if an expression of the type _ <bit_op> m <cmp_op> c (where <bit_op> is one of {&, '|'} and <cmp_op> is one of {!=, >=, > , !=, >=, >}) can be determined from the following table:

Comparison Bit-Op Example is always Formula
== or != & x & 2 == 3 false c & m != c
< or >= & x & 2 < 3 true m < c
> or <= & x & 1 > 1 false m <= c
== or != | x | 1 == 0 false c | m != c
< or >= | x | 1 < 1 false m >= c
<= or > | x | 1 > 0 true m > c

This lint is deny by default

There is also a lint that warns on ineffective masks that is warn by default.

|Comparison|Bit-Op |Example |equals |Formula| |> / <=|| / ^|x | 2 > 3|x > 3|¹ && m <= c| |< / >=|| / ^|x ^ 1 < 4|x < 4|¹ && m < c |

¹ power_of_two(c + 1)

Trait Implementations

impl LintPass for BitMask

fn get_lints(&self) -> LintArray

impl LateLintPass for BitMask

fn check_expr(&mut self, cx: &LateContext, e: &Expr)

fn check_name(&mut self, &LateContext, Span, Name)

fn check_crate(&mut self, &LateContext, &Crate)

fn check_mod(&mut self, &LateContext, &Mod, Span, u32)

fn check_foreign_item(&mut self, &LateContext, &ForeignItem)

fn check_item(&mut self, &LateContext, &Item)

fn check_local(&mut self, &LateContext, &Local)

fn check_block(&mut self, &LateContext, &Block)

fn check_stmt(&mut self, &LateContext, &Spanned<Stmt_>)

fn check_arm(&mut self, &LateContext, &Arm)

fn check_pat(&mut self, &LateContext, &Pat)

fn check_decl(&mut self, &LateContext, &Spanned<Decl_>)

fn check_expr_post(&mut self, &LateContext, &Expr)

fn check_ty(&mut self, &LateContext, &Ty)

fn check_generics(&mut self, &LateContext, &Generics)

fn check_fn(&mut self, &LateContext, FnKind, &FnDecl, &Block, Span, u32)

fn check_trait_item(&mut self, &LateContext, &TraitItem)

fn check_impl_item(&mut self, &LateContext, &ImplItem)

fn check_struct_def(&mut self, &LateContext, &VariantData, Name, &Generics, u32)

fn check_struct_def_post(&mut self, &LateContext, &VariantData, Name, &Generics, u32)

fn check_struct_field(&mut self, &LateContext, &Spanned<StructField_>)

fn check_variant(&mut self, &LateContext, &Spanned<Variant_>, &Generics)

fn check_variant_post(&mut self, &LateContext, &Spanned<Variant_>, &Generics)

fn check_lifetime(&mut self, &LateContext, &Lifetime)

fn check_lifetime_def(&mut self, &LateContext, &LifetimeDef)

fn check_explicit_self(&mut self, &LateContext, &Spanned<ExplicitSelf_>)

fn check_path(&mut self, &LateContext, &Path, u32)

fn check_path_list_item(&mut self, &LateContext, &Spanned<PathListItem_>)

fn check_attribute(&mut self, &LateContext, &Spanned<Attribute_>)

fn enter_lint_attrs(&mut self, &LateContext, &[Spanned<Attribute_>])

fn exit_lint_attrs(&mut self, &LateContext, &[Spanned<Attribute_>])

Derived Implementations

impl Clone for BitMask

fn clone(&self) -> BitMask

fn clone_from(&mut self, source: &Self)

impl Copy for BitMask