Skip to content

Commit 45501f8

Browse files
committed
Move unit_cmp to its own module
1 parent aa7d0cb commit 45501f8

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
@@ -1084,7 +1084,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10841084
store.register_late_pass(|| box map_err_ignore::MapErrIgnore);
10851085
store.register_late_pass(|| box shadow::Shadow);
10861086
store.register_late_pass(|| box unit_types::UnitTypes);
1087-
store.register_late_pass(|| box unit_types::UnitCmp);
10881087
store.register_late_pass(|| box loops::Loops);
10891088
store.register_late_pass(|| box main_recursion::MainRecursion::default());
10901089
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,16 +1,16 @@
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

13-
use crate::utils::{indent_of, reindent_multiline, snippet_opt, span_lint, span_lint_and_then};
13+
use crate::utils::{indent_of, reindent_multiline, snippet_opt, span_lint_and_then};
1414

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

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

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

91-
declare_lint_pass!(UnitCmp => [UNIT_CMP]);
83+
declare_lint_pass!(UnitTypes => [LET_UNIT_VALUE, UNIT_CMP]);
9284

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

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)