11mod let_unit_value;
2+ mod unit_cmp;
23mod utils;
34
45use rustc_errors:: Applicability ;
56use 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 } ;
78use rustc_lint:: { LateContext , LateLintPass } ;
89use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
9- use rustc_span:: hygiene:: { ExpnKind , MacroKind } ;
1010
1111use if_chain:: if_chain;
1212
13- use crate :: utils:: diagnostics:: { span_lint , span_lint_and_then} ;
13+ use crate :: utils:: diagnostics:: span_lint_and_then;
1414use crate :: utils:: source:: { indent_of, reindent_multiline, snippet_opt} ;
1515
1616use utils:: { is_unit, is_unit_literal} ;
@@ -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-
4537declare_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
0 commit comments