Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ff96025

Browse files
committed
Code clean-up and formatting
1 parent 3d3af07 commit ff96025

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

clippy_lints/src/needless_bool.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
//! This lint is **warn** by default
44
55
use crate::utils::sugg::Sugg;
6-
use crate::utils::{higher, parent_node_is_if_expr, span_lint, span_lint_and_help, span_lint_and_sugg, snippet_with_applicability};
6+
use crate::utils::{higher, parent_node_is_if_expr, snippet_with_applicability, span_lint, span_lint_and_sugg};
77
use if_chain::if_chain;
88
use rustc_ast::ast::LitKind;
99
use rustc_errors::Applicability;
1010
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp};
1111
use rustc_lint::{LateContext, LateLintPass};
1212
use rustc_session::{declare_lint_pass, declare_tool_lint};
1313
use rustc_span::source_map::Spanned;
14+
use rustc_span::Span;
1415

1516
declare_clippy_lint! {
1617
/// **What it does:** Checks for expressions of the form `if c { true } else {
@@ -189,7 +190,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison {
189190
}
190191
}
191192

192-
fn is_unary_not<'tcx>(e: &'tcx Expr<'_>) -> (bool, rustc_span::Span) {
193+
struct ExpressionInfoWithSpan {
194+
one_side_is_unary_not: bool,
195+
left_span: Span,
196+
right_span: Span,
197+
}
198+
199+
fn is_unary_not(e: &Expr<'_>) -> (bool, Span) {
193200
if_chain! {
194201
if let ExprKind::Unary(unop, operand) = e.kind;
195202
if let UnOp::UnNot = unop;
@@ -200,12 +207,15 @@ fn is_unary_not<'tcx>(e: &'tcx Expr<'_>) -> (bool, rustc_span::Span) {
200207
(false, e.span)
201208
}
202209

203-
fn one_side_is_unary_not<'tcx>(left_side: &'tcx Expr<'_>, right_side: &'tcx Expr<'_>) -> (bool, rustc_span::Span, rustc_span::Span) {
210+
fn one_side_is_unary_not<'tcx>(left_side: &'tcx Expr<'_>, right_side: &'tcx Expr<'_>) -> ExpressionInfoWithSpan {
204211
let left = is_unary_not(left_side);
205212
let right = is_unary_not(right_side);
206213

207-
let retval = left.0 ^ right.0;
208-
(retval, left.1, right.1)
214+
ExpressionInfoWithSpan {
215+
one_side_is_unary_not: left.0 ^ right.0,
216+
left_span: left.1,
217+
right_span: right.1,
218+
}
209219
}
210220

211221
fn check_comparison<'a, 'tcx>(
@@ -224,20 +234,20 @@ fn check_comparison<'a, 'tcx>(
224234
if l_ty.is_bool() && r_ty.is_bool() {
225235
let mut applicability = Applicability::MachineApplicable;
226236

227-
if let BinOpKind::Eq = op.node
228-
{
229-
let xxx = one_side_is_unary_not(&left_side, &right_side);
230-
if xxx.0
231-
{
237+
if let BinOpKind::Eq = op.node {
238+
let expression_info = one_side_is_unary_not(&left_side, &right_side);
239+
if expression_info.one_side_is_unary_not {
232240
span_lint_and_sugg(
233241
cx,
234242
BOOL_COMPARISON,
235243
e.span,
236244
"This comparison might be written more concisely",
237245
"try simplifying it as shown",
238-
format!("{} != {}",
239-
snippet_with_applicability(cx, xxx.1, "..", &mut applicability),
240-
snippet_with_applicability(cx, xxx.2, "..", &mut applicability)),
246+
format!(
247+
"{} != {}",
248+
snippet_with_applicability(cx, expression_info.left_span, "..", &mut applicability),
249+
snippet_with_applicability(cx, expression_info.right_span, "..", &mut applicability)
250+
),
241251
applicability,
242252
)
243253
}

tests/ui/bool_comparison.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ fn issue3703() {
112112
if false < Foo {}
113113
}
114114

115+
#[allow(dead_code)]
115116
fn issue4983() {
116117
let a = true;
117118
let b = false;
@@ -120,4 +121,9 @@ fn issue4983() {
120121
if a != b {};
121122
if a == b {};
122123
if !a == !b {};
124+
125+
if b != a {};
126+
if b != a {};
127+
if b == a {};
128+
if !b == !a {};
123129
}

tests/ui/bool_comparison.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ fn issue3703() {
112112
if false < Foo {}
113113
}
114114

115+
#[allow(dead_code)]
115116
fn issue4983() {
116117
let a = true;
117118
let b = false;

tests/ui/bool_comparison.stderr

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,28 @@ LL | if x > y {
8585
| ^^^^^ help: try simplifying it as shown: `x & !y`
8686

8787
error: This comparison might be written more concisely
88-
--> $DIR/bool_comparison.rs:119:8
88+
--> $DIR/bool_comparison.rs:120:8
8989
|
9090
LL | if a == !b {};
9191
| ^^^^^^^ help: try simplifying it as shown: `a != b`
9292

9393
error: This comparison might be written more concisely
94-
--> $DIR/bool_comparison.rs:120:8
94+
--> $DIR/bool_comparison.rs:121:8
9595
|
9696
LL | if !a == b {};
9797
| ^^^^^^^ help: try simplifying it as shown: `a != b`
9898

99-
error: aborting due to 16 previous errors
99+
error: This comparison might be written more concisely
100+
--> $DIR/bool_comparison.rs:125:8
101+
|
102+
LL | if b == !a {};
103+
| ^^^^^^^ help: try simplifying it as shown: `b != a`
104+
105+
error: This comparison might be written more concisely
106+
--> $DIR/bool_comparison.rs:126:8
107+
|
108+
LL | if !b == a {};
109+
| ^^^^^^^ help: try simplifying it as shown: `b != a`
110+
111+
error: aborting due to 18 previous errors
100112

0 commit comments

Comments
 (0)