1
1
mod let_unit_value;
2
+ mod unit_cmp;
2
3
mod utils;
3
4
4
5
use rustc_errors:: Applicability ;
5
6
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 } ;
7
8
use rustc_lint:: { LateContext , LateLintPass } ;
8
9
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
9
- use rustc_span:: hygiene:: { ExpnKind , MacroKind } ;
10
10
11
11
use if_chain:: if_chain;
12
12
13
13
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;
15
15
16
16
use utils:: { is_unit, is_unit_literal} ;
17
17
@@ -34,14 +34,6 @@ declare_clippy_lint! {
34
34
"creating a `let` binding to a value of unit type, which usually can't be used afterwards"
35
35
}
36
36
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
-
45
37
declare_clippy_lint ! {
46
38
/// **What it does:** Checks for comparisons to unit. This includes all binary
47
39
/// comparisons (like `==` and `<`) and asserts.
@@ -89,56 +81,15 @@ declare_clippy_lint! {
89
81
"comparing unit values"
90
82
}
91
83
92
- declare_lint_pass ! ( UnitCmp => [ UNIT_CMP ] ) ;
84
+ declare_lint_pass ! ( UnitTypes => [ LET_UNIT_VALUE , UNIT_CMP ] ) ;
93
85
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) ;
142
93
}
143
94
}
144
95
0 commit comments