Skip to content

Commit d468dca

Browse files
committed
added needless_bitwise_bool lint
1 parent 0d4e24e commit d468dca

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,6 +2549,7 @@ Released 2018-09-13
25492549
[`mutex_integer`]: https://rust-lang.github.io/rust-clippy/master/index.html#mutex_integer
25502550
[`naive_bytecount`]: https://rust-lang.github.io/rust-clippy/master/index.html#naive_bytecount
25512551
[`needless_arbitrary_self_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_arbitrary_self_type
2552+
[`needless_bitwise_bool`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_bitwise_bool
25522553
[`needless_bool`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_bool
25532554
[`needless_borrow`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
25542555
[`needless_borrowed_reference`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrowed_reference

clippy_lints/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ mod mut_reference;
288288
mod mutable_debug_assertion;
289289
mod mutex_atomic;
290290
mod needless_arbitrary_self_type;
291+
mod needless_bitwise_bool;
291292
mod needless_bool;
292293
mod needless_borrow;
293294
mod needless_borrowed_ref;
@@ -833,6 +834,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
833834
mutex_atomic::MUTEX_ATOMIC,
834835
mutex_atomic::MUTEX_INTEGER,
835836
needless_arbitrary_self_type::NEEDLESS_ARBITRARY_SELF_TYPE,
837+
needless_bitwise_bool::NEEDLESS_BITWISE_BOOL,
836838
needless_bool::BOOL_COMPARISON,
837839
needless_bool::NEEDLESS_BOOL,
838840
needless_borrow::NEEDLESS_BORROW,
@@ -1018,6 +1020,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10181020
let type_complexity_threshold = conf.type_complexity_threshold;
10191021
store.register_late_pass(move || box types::Types::new(vec_box_size_threshold, type_complexity_threshold));
10201022
store.register_late_pass(|| box booleans::NonminimalBool);
1023+
store.register_late_pass(|| box needless_bitwise_bool::NeedlessBitwiseBool);
10211024
store.register_late_pass(|| box eq_op::EqOp);
10221025
store.register_late_pass(|| box enum_clike::UnportableVariant);
10231026
store.register_late_pass(|| box float_literal::FloatLiteral);
@@ -1392,6 +1395,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13921395
LintId::of(misc::USED_UNDERSCORE_BINDING),
13931396
LintId::of(misc_early::UNSEPARATED_LITERAL_SUFFIX),
13941397
LintId::of(mut_mut::MUT_MUT),
1398+
LintId::of(needless_bitwise_bool::NEEDLESS_BITWISE_BOOL),
13951399
LintId::of(needless_continue::NEEDLESS_CONTINUE),
13961400
LintId::of(needless_for_each::NEEDLESS_FOR_EACH),
13971401
LintId::of(needless_pass_by_value::NEEDLESS_PASS_BY_VALUE),
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::in_macro;
3+
use clippy_utils::source::snippet_opt;
4+
use if_chain::if_chain;
5+
use rustc_errors::Applicability;
6+
use rustc_hir::{BinOpKind, Expr, ExprKind};
7+
use rustc_lint::{LateContext, LateLintPass};
8+
use rustc_middle::ty;
9+
use rustc_session::{declare_lint_pass, declare_tool_lint};
10+
11+
declare_clippy_lint! {
12+
/// **What it does:**
13+
/// Checks for uses of bitwise and/or operators between booleans, where performance may be improved by using
14+
/// a lazy and.
15+
///
16+
/// **Why is this bad?**
17+
/// The bitwise operators do not support short-circuiting, so it may hinder code performance.
18+
/// Additionally, boolean logic "masked" as bitwise logic is not caught by lints like `unnecessary_fold`
19+
///
20+
/// **Known problems:**
21+
/// This lint evaluates only when the right side is determined to have no side effects. At this time, that
22+
/// determination is quite conservative.
23+
///
24+
/// **Example:**
25+
///
26+
/// ```rust
27+
/// if x & !y {} // where both x and y are booleans
28+
/// ```
29+
/// Use instead:
30+
/// ```rust
31+
/// if x && !y {}
32+
/// ```
33+
pub NEEDLESS_BITWISE_BOOL,
34+
pedantic,
35+
"Boolean expressions that use bitwise rather than lazy operators"
36+
}
37+
38+
declare_lint_pass!(NeedlessBitwiseBool => [NEEDLESS_BITWISE_BOOL]);
39+
40+
fn is_bitwise_operation(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
41+
let ty = cx.typeck_results().expr_ty(expr);
42+
if_chain! {
43+
if !in_macro(expr.span);
44+
if let (&ExprKind::Binary(ref op, _, right), &ty::Bool) = (&expr.kind, &ty.kind());
45+
if op.node == BinOpKind::BitAnd || op.node == BinOpKind::BitOr;
46+
if let ExprKind::Call(..) | ExprKind::MethodCall(..) | ExprKind::Binary(..) | ExprKind::Unary(..) = right.kind;
47+
if !right.can_have_side_effects();
48+
then {
49+
return true;
50+
}
51+
}
52+
false
53+
}
54+
55+
fn suggession_snippet(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<String> {
56+
if let ExprKind::Binary(ref op, left, right) = expr.kind {
57+
if let (Some(l_snippet), Some(r_snippet)) = (snippet_opt(cx, left.span), snippet_opt(cx, right.span)) {
58+
let op_snippet = match op.node {
59+
BinOpKind::BitAnd => "&&",
60+
_ => "||",
61+
};
62+
return Some(format!("{} {} {}", l_snippet, op_snippet, r_snippet));
63+
}
64+
}
65+
None
66+
}
67+
68+
impl LateLintPass<'_> for NeedlessBitwiseBool {
69+
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
70+
if is_bitwise_operation(cx, expr) {
71+
span_lint_and_then(
72+
cx,
73+
NEEDLESS_BITWISE_BOOL,
74+
expr.span,
75+
"use of bitwise operator instead of logical operator between booleans",
76+
|diag| {
77+
if let Some(sugg) = suggession_snippet(cx, expr) {
78+
diag.span_suggestion(expr.span, "try", sugg, Applicability::MachineApplicable);
79+
}
80+
},
81+
);
82+
}
83+
}
84+
}

tests/ui/needless_bitwise_bool.fixed

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::needless_bitwise_bool)]
4+
5+
fn returns_bool() -> bool {
6+
true
7+
}
8+
9+
const fn const_returns_bool() -> bool {
10+
false
11+
}
12+
13+
fn main() {
14+
let (x, y) = (false, true);
15+
if x & y {
16+
println!("true")
17+
}
18+
if returns_bool() & x {
19+
println!("true")
20+
}
21+
if !returns_bool() & returns_bool() {
22+
println!("true")
23+
}
24+
if y && !x {
25+
println!("true")
26+
}
27+
28+
// BELOW: lints we hope to catch as `Expr::can_have_side_effects` improves.
29+
if y & !const_returns_bool() {
30+
println!("true") // This is a const function, in an UnOp
31+
}
32+
33+
if y & "abcD".is_empty() {
34+
println!("true") // This is a const method call
35+
}
36+
37+
if y & (0 < 1) {
38+
println!("true") // This is a BinOp with no side effects
39+
}
40+
}

tests/ui/needless_bitwise_bool.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::needless_bitwise_bool)]
4+
5+
fn returns_bool() -> bool {
6+
true
7+
}
8+
9+
const fn const_returns_bool() -> bool {
10+
false
11+
}
12+
13+
fn main() {
14+
let (x, y) = (false, true);
15+
if x & y {
16+
println!("true")
17+
}
18+
if returns_bool() & x {
19+
println!("true")
20+
}
21+
if !returns_bool() & returns_bool() {
22+
println!("true")
23+
}
24+
if y & !x {
25+
println!("true")
26+
}
27+
28+
// BELOW: lints we hope to catch as `Expr::can_have_side_effects` improves.
29+
if y & !const_returns_bool() {
30+
println!("true") // This is a const function, in an UnOp
31+
}
32+
33+
if y & "abcD".is_empty() {
34+
println!("true") // This is a const method call
35+
}
36+
37+
if y & (0 < 1) {
38+
println!("true") // This is a BinOp with no side effects
39+
}
40+
}

tests/ui/needless_bitwise_bool.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: use of bitwise operator instead of logical operator between booleans
2+
--> $DIR/needless_bitwise_bool.rs:24:8
3+
|
4+
LL | if y & !x {
5+
| ^^^^^^ help: try: `y && !x`
6+
|
7+
= note: `-D clippy::needless-bitwise-bool` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)