1
1
use clippy_utils:: diagnostics:: span_lint;
2
- use rustc_hir:: { BinOpKind , Expr , ExprKind } ;
2
+ use rustc_hir:: def:: Res ;
3
+ use rustc_hir:: { BinOpKind , Expr , ExprKind , QPath } ;
3
4
use rustc_lint:: { LateContext , LateLintPass } ;
4
5
use rustc_session:: declare_lint_pass;
5
6
@@ -30,18 +31,27 @@ declare_clippy_lint! {
30
31
31
32
declare_lint_pass ! ( AlwaysTrueConditions => [ ALWAYS_TRUE_CONDITIONS ] ) ;
32
33
33
- fn context_applicable ( expr : & Expr < ' _ > ) -> bool {
34
+ fn context_applicable ( expr : & Expr < ' _ > ) -> Option < Res > {
34
35
if let ExprKind :: Binary ( new_op, new_f, new_l) = expr. kind {
35
36
if new_op. node == BinOpKind :: Or {
36
- //only continue DOWN if its an or.give me the zuck
37
37
let f = context_applicable ( new_f) ;
38
38
let l = context_applicable ( new_l) ;
39
- l && f
39
+ if l == f { l } else { None }
40
+ } else if new_op. node == BinOpKind :: Ne {
41
+ find_path ( new_f)
40
42
} else {
41
- new_op . node == BinOpKind :: Ne
43
+ None
42
44
}
43
45
} else {
44
- false
46
+ None
47
+ }
48
+ }
49
+
50
+ fn find_path ( expr : & Expr < ' _ > ) -> Option < Res > {
51
+ if let ExprKind :: Path ( QPath :: Resolved ( _, path) ) = expr. kind {
52
+ Some ( path. res )
53
+ } else {
54
+ None
45
55
}
46
56
}
47
57
@@ -53,7 +63,7 @@ impl LateLintPass<'_> for AlwaysTrueConditions {
53
63
&& let BinOpKind :: Or = f_op_kind. node
54
64
{
55
65
let msg = "expression will always be true, did you mean &&?" ;
56
- if context_applicable ( f_cond) && context_applicable ( l_cond) {
66
+ if context_applicable ( f_cond) == context_applicable ( l_cond) {
57
67
span_lint ( cx, ALWAYS_TRUE_CONDITIONS , e. span , msg) ;
58
68
}
59
69
}
0 commit comments