Skip to content

Commit 6c1c506

Browse files
committed
Added checking for hir_dir
1 parent 4f7e938 commit 6c1c506

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

clippy_lints/src/always_true_conditions.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
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};
34
use rustc_lint::{LateContext, LateLintPass};
45
use rustc_session::declare_lint_pass;
56

@@ -30,18 +31,27 @@ declare_clippy_lint! {
3031

3132
declare_lint_pass!(AlwaysTrueConditions => [ALWAYS_TRUE_CONDITIONS]);
3233

33-
fn context_applicable(expr: &Expr<'_>) -> bool {
34+
fn context_applicable(expr: &Expr<'_>) -> Option<Res> {
3435
if let ExprKind::Binary(new_op, new_f, new_l) = expr.kind {
3536
if new_op.node == BinOpKind::Or {
36-
//only continue DOWN if its an or.give me the zuck
3737
let f = context_applicable(new_f);
3838
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)
4042
} else {
41-
new_op.node == BinOpKind::Ne
43+
None
4244
}
4345
} 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
4555
}
4656
}
4757

@@ -53,7 +63,7 @@ impl LateLintPass<'_> for AlwaysTrueConditions {
5363
&& let BinOpKind::Or = f_op_kind.node
5464
{
5565
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) {
5767
span_lint(cx, ALWAYS_TRUE_CONDITIONS, e.span, msg);
5868
}
5969
}

tests/ui/always_true_conditions.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ fn foo_s(name: &str) {
2828
}
2929
}
3030

31-
fn catch_fails(input: &str) {
31+
fn catch_or_failure(input: &str) {
3232
let b = true;
3333
if b || input != "foo" {
3434
println!("should not fire!");
3535
}
3636
}
37+
38+
fn catch_diff_var_failure(input: &str) {
39+
let b = "value";
40+
if b != "bar" || input != "foo" {
41+
println!("should not fire!");
42+
}
43+
}

0 commit comments

Comments
 (0)