Skip to content

Commit 3557084

Browse files
committed
Add check for assert_eq macros to unit_cmp lint
1 parent 737f0a6 commit 3557084

File tree

6 files changed

+69
-3
lines changed

6 files changed

+69
-3
lines changed

clippy_lints/src/types.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use rustc_target::spec::abi::Abi;
1717
use rustc_typeck::hir_ty_to_ty;
1818
use syntax::ast::{FloatTy, IntTy, LitIntType, LitKind, UintTy};
1919
use syntax::errors::DiagnosticBuilder;
20+
use syntax::ext::base::MacroKind;
21+
use syntax::ext::hygiene::ExpnKind;
2022
use syntax::source_map::Span;
2123
use syntax::symbol::{sym, Symbol};
2224

@@ -527,6 +529,30 @@ declare_lint_pass!(UnitCmp => [UNIT_CMP]);
527529
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitCmp {
528530
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
529531
if expr.span.from_expansion() {
532+
if let Some(callee) = expr.span.source_callee() {
533+
if let ExpnKind::Macro(MacroKind::Bang, symbol) = callee.kind {
534+
if let ExprKind::Binary(ref cmp, ref left, _) = expr.kind {
535+
let op = cmp.node;
536+
if op.is_comparison() && is_unit(cx.tables.expr_ty(left)) {
537+
let result = match &*symbol.as_str() {
538+
"assert_eq" | "debug_assert_eq" => "succeed",
539+
"assert_ne" | "debug_assert_ne" => "fail",
540+
_ => return,
541+
};
542+
span_lint(
543+
cx,
544+
UNIT_CMP,
545+
expr.span,
546+
&format!(
547+
"{} of unit values detected. This will always {}",
548+
symbol.as_str(),
549+
result
550+
),
551+
);
552+
}
553+
}
554+
}
555+
}
530556
return;
531557
}
532558
if let ExprKind::Binary(ref cmp, ref left, _) = expr.kind {

tests/ui/unit_cmp.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ fn main() {
2020
} > {
2121
false;
2222
} {}
23+
24+
assert_eq!((), ());
25+
debug_assert_eq!((), ());
26+
27+
assert_ne!((), ());
28+
debug_assert_ne!((), ());
2329
}

tests/ui/unit_cmp.stderr

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,37 @@ LL | | false;
2222
LL | | } {}
2323
| |_____^
2424

25-
error: aborting due to 2 previous errors
25+
error: assert_eq of unit values detected. This will always succeed
26+
--> $DIR/unit_cmp.rs:24:5
27+
|
28+
LL | assert_eq!((), ());
29+
| ^^^^^^^^^^^^^^^^^^^
30+
|
31+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
32+
33+
error: debug_assert_eq of unit values detected. This will always succeed
34+
--> $DIR/unit_cmp.rs:25:5
35+
|
36+
LL | debug_assert_eq!((), ());
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
38+
|
39+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
40+
41+
error: assert_ne of unit values detected. This will always fail
42+
--> $DIR/unit_cmp.rs:27:5
43+
|
44+
LL | assert_ne!((), ());
45+
| ^^^^^^^^^^^^^^^^^^^
46+
|
47+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
48+
49+
error: debug_assert_ne of unit values detected. This will always fail
50+
--> $DIR/unit_cmp.rs:28:5
51+
|
52+
LL | debug_assert_ne!((), ());
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
54+
|
55+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
56+
57+
error: aborting due to 6 previous errors
2658

tests/ui/unused_unit.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fn return_unit() { }
3434

3535
#[allow(clippy::needless_return)]
3636
#[allow(clippy::never_loop)]
37+
#[allow(clippy::unit_cmp)]
3738
fn main() {
3839
let u = Unitter;
3940
assert_eq!(u.get_unit(|| {}, return_unit), u.into());

tests/ui/unused_unit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fn return_unit() -> () { () }
3535

3636
#[allow(clippy::needless_return)]
3737
#[allow(clippy::never_loop)]
38+
#[allow(clippy::unit_cmp)]
3839
fn main() {
3940
let u = Unitter;
4041
assert_eq!(u.get_unit(|| {}, return_unit), u.into());

tests/ui/unused_unit.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ LL | fn return_unit() -> () { () }
3737
| ^^ help: remove the final `()`
3838

3939
error: unneeded `()`
40-
--> $DIR/unused_unit.rs:43:14
40+
--> $DIR/unused_unit.rs:44:14
4141
|
4242
LL | break();
4343
| ^^ help: remove the `()`
4444

4545
error: unneeded `()`
46-
--> $DIR/unused_unit.rs:45:11
46+
--> $DIR/unused_unit.rs:46:11
4747
|
4848
LL | return();
4949
| ^^ help: remove the `()`

0 commit comments

Comments
 (0)