Skip to content

Commit 31eb2c4

Browse files
committed
Move unit_cmp to its own module
1 parent 607f1c4 commit 31eb2c4

File tree

3 files changed

+68
-61
lines changed

3 files changed

+68
-61
lines changed

clippy_lints/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10861086
store.register_late_pass(|| box map_err_ignore::MapErrIgnore);
10871087
store.register_late_pass(|| box shadow::Shadow);
10881088
store.register_late_pass(|| box unit_types::UnitTypes);
1089-
store.register_late_pass(|| box unit_types::UnitCmp);
10901089
store.register_late_pass(|| box loops::Loops);
10911090
store.register_late_pass(|| box main_recursion::MainRecursion::default());
10921091
store.register_late_pass(|| box lifetimes::Lifetimes);

clippy_lints/src/unit_types/mod.rs

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
mod let_unit_value;
2+
mod unit_cmp;
23
mod utils;
34

45
use rustc_errors::Applicability;
56
use rustc_hir as hir;
6-
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, MatchSource, Node, Stmt, StmtKind};
7+
use rustc_hir::{Block, Expr, ExprKind, MatchSource, Node, Stmt, StmtKind};
78
use rustc_lint::{LateContext, LateLintPass};
89
use rustc_session::{declare_lint_pass, declare_tool_lint};
9-
use rustc_span::hygiene::{ExpnKind, MacroKind};
1010

1111
use if_chain::if_chain;
1212

1313
use crate::utils::source::{indent_of, reindent_multiline, snippet_opt};
14-
use crate::utils::{span_lint, span_lint_and_then};
14+
use crate::utils::span_lint_and_then;
1515

1616
use utils::{is_unit, is_unit_literal};
1717

@@ -34,14 +34,6 @@ declare_clippy_lint! {
3434
"creating a `let` binding to a value of unit type, which usually can't be used afterwards"
3535
}
3636

37-
declare_lint_pass!(UnitTypes => [LET_UNIT_VALUE]);
38-
39-
impl<'tcx> LateLintPass<'tcx> for UnitTypes {
40-
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
41-
let_unit_value::check(cx, stmt);
42-
}
43-
}
44-
4537
declare_clippy_lint! {
4638
/// **What it does:** Checks for comparisons to unit. This includes all binary
4739
/// comparisons (like `==` and `<`) and asserts.
@@ -89,56 +81,15 @@ declare_clippy_lint! {
8981
"comparing unit values"
9082
}
9183

92-
declare_lint_pass!(UnitCmp => [UNIT_CMP]);
84+
declare_lint_pass!(UnitTypes => [LET_UNIT_VALUE, UNIT_CMP]);
9385

94-
impl<'tcx> LateLintPass<'tcx> for UnitCmp {
95-
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
96-
if expr.span.from_expansion() {
97-
if let Some(callee) = expr.span.source_callee() {
98-
if let ExpnKind::Macro(MacroKind::Bang, symbol) = callee.kind {
99-
if let ExprKind::Binary(ref cmp, ref left, _) = expr.kind {
100-
let op = cmp.node;
101-
if op.is_comparison() && is_unit(cx.typeck_results().expr_ty(left)) {
102-
let result = match &*symbol.as_str() {
103-
"assert_eq" | "debug_assert_eq" => "succeed",
104-
"assert_ne" | "debug_assert_ne" => "fail",
105-
_ => return,
106-
};
107-
span_lint(
108-
cx,
109-
UNIT_CMP,
110-
expr.span,
111-
&format!(
112-
"`{}` of unit values detected. This will always {}",
113-
symbol.as_str(),
114-
result
115-
),
116-
);
117-
}
118-
}
119-
}
120-
}
121-
return;
122-
}
123-
if let ExprKind::Binary(ref cmp, ref left, _) = expr.kind {
124-
let op = cmp.node;
125-
if op.is_comparison() && is_unit(cx.typeck_results().expr_ty(left)) {
126-
let result = match op {
127-
BinOpKind::Eq | BinOpKind::Le | BinOpKind::Ge => "true",
128-
_ => "false",
129-
};
130-
span_lint(
131-
cx,
132-
UNIT_CMP,
133-
expr.span,
134-
&format!(
135-
"{}-comparison of unit values detected. This will always be {}",
136-
op.as_str(),
137-
result
138-
),
139-
);
140-
}
141-
}
86+
impl LateLintPass<'_> for UnitTypes {
87+
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
88+
let_unit_value::check(cx, stmt);
89+
}
90+
91+
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
92+
unit_cmp::check(cx, expr);
14293
}
14394
}
14495

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use rustc_hir::{BinOpKind, Expr, ExprKind};
2+
use rustc_lint::LateContext;
3+
use rustc_span::hygiene::{ExpnKind, MacroKind};
4+
5+
use crate::utils::span_lint;
6+
7+
use super::{utils, UNIT_CMP};
8+
9+
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
10+
if expr.span.from_expansion() {
11+
if let Some(callee) = expr.span.source_callee() {
12+
if let ExpnKind::Macro(MacroKind::Bang, symbol) = callee.kind {
13+
if let ExprKind::Binary(ref cmp, ref left, _) = expr.kind {
14+
let op = cmp.node;
15+
if op.is_comparison() && utils::is_unit(cx.typeck_results().expr_ty(left)) {
16+
let result = match &*symbol.as_str() {
17+
"assert_eq" | "debug_assert_eq" => "succeed",
18+
"assert_ne" | "debug_assert_ne" => "fail",
19+
_ => return,
20+
};
21+
span_lint(
22+
cx,
23+
UNIT_CMP,
24+
expr.span,
25+
&format!(
26+
"`{}` of unit values detected. This will always {}",
27+
symbol.as_str(),
28+
result
29+
),
30+
);
31+
}
32+
}
33+
}
34+
}
35+
return;
36+
}
37+
38+
if let ExprKind::Binary(ref cmp, ref left, _) = expr.kind {
39+
let op = cmp.node;
40+
if op.is_comparison() && utils::is_unit(cx.typeck_results().expr_ty(left)) {
41+
let result = match op {
42+
BinOpKind::Eq | BinOpKind::Le | BinOpKind::Ge => "true",
43+
_ => "false",
44+
};
45+
span_lint(
46+
cx,
47+
UNIT_CMP,
48+
expr.span,
49+
&format!(
50+
"{}-comparison of unit values detected. This will always be {}",
51+
op.as_str(),
52+
result
53+
),
54+
);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)