Skip to content

Commit a9f8d3c

Browse files
author
A.A.Abroskin
committed
add assert(true/false, some message) tests
1 parent 7075015 commit a9f8d3c

File tree

3 files changed

+79
-23
lines changed

3 files changed

+79
-23
lines changed

clippy_lints/src/assertions_on_constants.rs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10+
use crate::consts::{constant, Constant};
1011
use crate::rustc::hir::{Expr, ExprKind};
1112
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
1213
use crate::rustc::{declare_tool_lint, lint_array};
1314
use crate::syntax::ast::LitKind;
14-
use crate::utils::{is_direct_expn_of, span_lint, span_lint_and_sugg};
15-
use rustc_errors::Applicability;
15+
use crate::utils::{is_direct_expn_of, span_help_and_lint};
1616
use if_chain::if_chain;
1717

18-
/// **What it does:** Check explicit call assert!(true/false)
18+
/// **What it does:** Check to call assert!(true/false)
1919
///
20-
/// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a panic!() or unreachable!()
20+
/// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
21+
/// panic!() or unreachable!()
2122
///
2223
/// **Known problems:** None
2324
///
@@ -49,24 +50,36 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
4950
if_chain! {
5051
if is_direct_expn_of(e.span, "assert").is_some();
5152
if let ExprKind::Unary(_, ref lit) = e.node;
52-
if let ExprKind::Lit(ref inner) = lit.node;
5353
then {
54-
match inner.node {
55-
LitKind::Bool(true) => {
56-
span_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
57-
"assert!(true) will be optimized out by the compiler");
58-
},
59-
LitKind::Bool(false) => {
60-
span_lint_and_sugg(
61-
cx,
62-
ASSERTIONS_ON_CONSTANTS,
63-
e.span,
64-
"assert!(false) should probably be replaced",
65-
"try",
66-
"panic!()".to_string(),
67-
Applicability::MachineApplicable);
68-
},
69-
_ => (),
54+
if let ExprKind::Lit(ref inner) = lit.node {
55+
match inner.node {
56+
LitKind::Bool(true) => {
57+
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
58+
"assert!(true) will be optimized out by the compiler",
59+
"remove it");
60+
},
61+
LitKind::Bool(false) => {
62+
span_help_and_lint(
63+
cx, ASSERTIONS_ON_CONSTANTS, e.span,
64+
"assert!(false) should probably be replaced",
65+
"use panic!() or unreachable!()");
66+
},
67+
_ => (),
68+
}
69+
} else if let Some(bool_const) = constant(cx, cx.tables, lit) {
70+
match bool_const.0 {
71+
Constant::Bool(true) => {
72+
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
73+
"assert!(const: true) will be optimized out by the compiler",
74+
"remove it");
75+
},
76+
Constant::Bool(false) => {
77+
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
78+
"assert!(const: false) should probably be replaced",
79+
"use panic!() or unreachable!()");
80+
},
81+
_ => (),
82+
}
7083
}
7184
}
7285
}

tests/ui/assertions_on_constants.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@
1010
fn main() {
1111
assert!(true);
1212
assert!(false);
13+
assert!(true, "true message");
14+
assert!(false, "false message");
15+
16+
const B: bool = true;
17+
assert!(B);
18+
19+
const C: bool = false;
20+
assert!(C);
1321
}

tests/ui/assertions_on_constants.stderr

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,47 @@ LL | assert!(true);
55
| ^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::assertions-on-constants` implied by `-D warnings`
8+
= help: remove it
89

910
error: assert!(false) should probably be replaced
1011
--> $DIR/assertions_on_constants.rs:12:5
1112
|
1213
LL | assert!(false);
13-
| ^^^^^^^^^^^^^^^ help: try: `panic!()`
14+
| ^^^^^^^^^^^^^^^
15+
|
16+
= help: use panic!() or unreachable!()
17+
18+
error: assert!(true) will be optimized out by the compiler
19+
--> $DIR/assertions_on_constants.rs:13:5
20+
|
21+
LL | assert!(true, "true message");
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= help: remove it
25+
26+
error: assert!(false) should probably be replaced
27+
--> $DIR/assertions_on_constants.rs:14:5
28+
|
29+
LL | assert!(false, "false message");
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
= help: use panic!() or unreachable!()
33+
34+
error: assert!(const: true) will be optimized out by the compiler
35+
--> $DIR/assertions_on_constants.rs:17:5
36+
|
37+
LL | assert!(B);
38+
| ^^^^^^^^^^^
39+
|
40+
= help: remove it
41+
42+
error: assert!(const: false) should probably be replaced
43+
--> $DIR/assertions_on_constants.rs:20:5
44+
|
45+
LL | assert!(C);
46+
| ^^^^^^^^^^^
47+
|
48+
= help: use panic!() or unreachable!()
1449

15-
error: aborting due to 2 previous errors
50+
error: aborting due to 6 previous errors
1651

0 commit comments

Comments
 (0)