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
- 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} ;
14
14
15
15
use utils:: { is_unit, is_unit_literal} ;
16
16
@@ -33,14 +33,6 @@ declare_clippy_lint! {
33
33
"creating a `let` binding to a value of unit type, which usually can't be used afterwards"
34
34
}
35
35
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
-
44
36
declare_clippy_lint ! {
45
37
/// **What it does:** Checks for comparisons to unit. This includes all binary
46
38
/// comparisons (like `==` and `<`) and asserts.
@@ -88,56 +80,15 @@ declare_clippy_lint! {
88
80
"comparing unit values"
89
81
}
90
82
91
- declare_lint_pass ! ( UnitCmp => [ UNIT_CMP ] ) ;
83
+ declare_lint_pass ! ( UnitTypes => [ LET_UNIT_VALUE , UNIT_CMP ] ) ;
92
84
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) ;
141
92
}
142
93
}
143
94
0 commit comments