Skip to content

WIP: Lint using bitwise operators for boolean expressions #3385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions clippy_lints/src/booleans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@


use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use crate::rustc::{declare_tool_lint, lint_array, ty};
use crate::rustc::hir::*;
use crate::rustc::hir::intravisit::*;
use crate::syntax::ast::{LitKind, NodeId, DUMMY_NODE_ID};
use crate::syntax::source_map::{dummy_spanned, Span, DUMMY_SP};
use crate::rustc_data_structures::thin_vec::ThinVec;
use crate::utils::{in_macro, paths, match_type, snippet_opt, span_lint_and_then, SpanlessEq, get_trait_def_id, implements_trait};
use crate::utils::{in_macro, paths, match_type, snippet_opt, span_lint_and_then, SpanlessEq, get_trait_def_id, implements_trait, span_lint};
use crate::rustc_errors::Applicability;

/// **What it does:** Checks for boolean expressions that can be written more
Expand Down Expand Up @@ -485,3 +485,32 @@ fn implements_ord<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, expr: &Expr) -> bool
get_trait_def_id(cx, &paths::ORD)
.map_or(false, |id| implements_trait(cx, ty, id, &[]))
}


declare_clippy_lint! {
pub BITWISE_BOOL_EXPR,
style,
"Boolean expressions that use bitwise rather than logical operators"
}

#[derive(Copy, Clone)]
pub struct BitwiseBooleanPass;

impl LintPass for BitwiseBooleanPass{
fn get_lints(&self) -> LintArray {
lint_array![BITWISE_BOOL_EXPR]
}
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitwiseBooleanPass{
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
let ty = cx.tables.expr_ty(expr);
if let (&ExprKind::Binary(ref op, _, _), &ty::Bool) = (&expr.node, &ty.sty) {
if op.node == BinOpKind::BitAnd || op.node == BinOpKind::BitOr {
span_lint(cx, BITWISE_BOOL_EXPR, expr.span,
"Use of bitwise operator instead of logical operator"
);
}
}
}
}
3 changes: 3 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
reg.register_late_lint_pass(box utils::author::Pass);
reg.register_late_lint_pass(box types::TypePass);
reg.register_late_lint_pass(box booleans::NonminimalBool);
reg.register_late_lint_pass(box booleans::BitwiseBooleanPass);
reg.register_late_lint_pass(box eq_op::EqOp);
reg.register_early_lint_pass(box enum_variants::EnumVariantNames::new(conf.enum_variant_name_threshold));
reg.register_late_lint_pass(box enum_glob_use::EnumGlobUse);
Expand Down Expand Up @@ -540,6 +541,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT,
booleans::LOGIC_BUG,
booleans::NONMINIMAL_BOOL,
booleans::BITWISE_BOOL_EXPR,
bytecount::NAIVE_BYTECOUNT,
collapsible_if::COLLAPSIBLE_IF,
const_static_lifetime::CONST_STATIC_LIFETIME,
Expand Down Expand Up @@ -751,6 +753,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
blacklisted_name::BLACKLISTED_NAME,
block_in_if_condition::BLOCK_IN_IF_CONDITION_EXPR,
block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT,
booleans::BITWISE_BOOL_EXPR,
collapsible_if::COLLAPSIBLE_IF,
const_static_lifetime::CONST_STATIC_LIFETIME,
enum_variants::ENUM_VARIANT_NAMES,
Expand Down