Skip to content

Commit 99c5388

Browse files
committed
Auto merge of #3582 - Arkweid:add-lints-aseert-checks, r=flip1995
Add assert(true) and assert(false) lints This PR add two lints on assert!(true) and assert!(false). #3575
2 parents a069320 + c771f33 commit 99c5388

12 files changed

+154
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ All notable changes to this project will be documented in this file.
616616
[`absurd_extreme_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons
617617
[`almost_swapped`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_swapped
618618
[`approx_constant`]: https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant
619+
[`assertions_on_constants`]: https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants
619620
[`assign_op_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#assign_op_pattern
620621
[`assign_ops`]: https://rust-lang.github.io/rust-clippy/master/index.html#assign_ops
621622
[`bad_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#bad_bit_mask

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
99

10-
[There are 291 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
10+
[There are 292 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1111

1212
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1313

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use crate::consts::{constant, Constant};
2+
use crate::rustc::hir::{Expr, ExprKind};
3+
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4+
use crate::rustc::{declare_tool_lint, lint_array};
5+
use crate::syntax::ast::LitKind;
6+
use crate::utils::{is_direct_expn_of, span_help_and_lint};
7+
use if_chain::if_chain;
8+
9+
/// **What it does:** Check to call assert!(true/false)
10+
///
11+
/// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
12+
/// panic!() or unreachable!()
13+
///
14+
/// **Known problems:** None
15+
///
16+
/// **Example:**
17+
/// ```rust
18+
/// assert!(false)
19+
/// // or
20+
/// assert!(true)
21+
/// // or
22+
/// const B: bool = false;
23+
/// assert!(B)
24+
/// ```
25+
declare_clippy_lint! {
26+
pub ASSERTIONS_ON_CONSTANTS,
27+
style,
28+
"assert!(true/false) will be optimized out by the compiler/should probably be replaced by a panic!() or unreachable!()"
29+
}
30+
31+
pub struct AssertionsOnConstants;
32+
33+
impl LintPass for AssertionsOnConstants {
34+
fn get_lints(&self) -> LintArray {
35+
lint_array![ASSERTIONS_ON_CONSTANTS]
36+
}
37+
}
38+
39+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
40+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
41+
if_chain! {
42+
if is_direct_expn_of(e.span, "assert").is_some();
43+
if let ExprKind::Unary(_, ref lit) = e.node;
44+
then {
45+
if let ExprKind::Lit(ref inner) = lit.node {
46+
match inner.node {
47+
LitKind::Bool(true) => {
48+
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
49+
"assert!(true) will be optimized out by the compiler",
50+
"remove it");
51+
},
52+
LitKind::Bool(false) => {
53+
span_help_and_lint(
54+
cx, ASSERTIONS_ON_CONSTANTS, e.span,
55+
"assert!(false) should probably be replaced",
56+
"use panic!() or unreachable!()");
57+
},
58+
_ => (),
59+
}
60+
} else if let Some(bool_const) = constant(cx, cx.tables, lit) {
61+
match bool_const.0 {
62+
Constant::Bool(true) => {
63+
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
64+
"assert!(const: true) will be optimized out by the compiler",
65+
"remove it");
66+
},
67+
Constant::Bool(false) => {
68+
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
69+
"assert!(const: false) should probably be replaced",
70+
"use panic!() or unreachable!()");
71+
},
72+
_ => (),
73+
}
74+
}
75+
}
76+
}
77+
}
78+
}

clippy_lints/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ mod utils;
7878
// begin lints modules, do not remove this comment, it’s used in `update_lints`
7979
pub mod approx_const;
8080
pub mod arithmetic;
81+
pub mod assertions_on_constants;
8182
pub mod assign_ops;
8283
pub mod attrs;
8384
pub mod bit_mask;
@@ -477,6 +478,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
477478
reg.register_late_lint_pass(box redundant_clone::RedundantClone);
478479
reg.register_late_lint_pass(box slow_vector_initialization::Pass);
479480
reg.register_late_lint_pass(box types::RefToMut);
481+
reg.register_late_lint_pass(box assertions_on_constants::AssertionsOnConstants);
480482

481483
reg.register_lint_group("clippy::restriction", Some("clippy_restriction"), vec![
482484
arithmetic::FLOAT_ARITHMETIC,
@@ -554,6 +556,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
554556

555557
reg.register_lint_group("clippy::all", Some("clippy"), vec![
556558
approx_const::APPROX_CONSTANT,
559+
assertions_on_constants::ASSERTIONS_ON_CONSTANTS,
557560
assign_ops::ASSIGN_OP_PATTERN,
558561
assign_ops::MISREFACTORED_ASSIGN_OP,
559562
attrs::DEPRECATED_CFG_ATTR,
@@ -776,6 +779,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
776779
]);
777780

778781
reg.register_lint_group("clippy::style", Some("clippy_style"), vec![
782+
assertions_on_constants::ASSERTIONS_ON_CONSTANTS,
779783
assign_ops::ASSIGN_OP_PATTERN,
780784
attrs::UNKNOWN_CLIPPY_LINTS,
781785
bit_mask::VERBOSE_BIT_MASK,

tests/missing-test-files.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::assertions_on_constants)]
2+
13
use std::fs::{self, DirEntry};
24
use std::path::Path;
35

tests/ui/assertions_on_constants.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {
2+
assert!(true);
3+
assert!(false);
4+
assert!(true, "true message");
5+
assert!(false, "false message");
6+
7+
const B: bool = true;
8+
assert!(B);
9+
10+
const C: bool = false;
11+
assert!(C);
12+
}
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error: assert!(true) will be optimized out by the compiler
2+
--> $DIR/assertions_on_constants.rs:2:5
3+
|
4+
LL | assert!(true);
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::assertions-on-constants` implied by `-D warnings`
8+
= help: remove it
9+
10+
error: assert!(false) should probably be replaced
11+
--> $DIR/assertions_on_constants.rs:3:5
12+
|
13+
LL | assert!(false);
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:4: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:5: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:8: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:11:5
44+
|
45+
LL | assert!(C);
46+
| ^^^^^^^^^^^
47+
|
48+
= help: use panic!() or unreachable!()
49+
50+
error: aborting due to 6 previous errors
51+

tests/ui/attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![warn(clippy::inline_always, clippy::deprecated_semver)]
2-
2+
#![allow(clippy::assertions_on_constants)]
33
#[inline(always)]
44
fn test_attr_lint() {
55
assert!(true)

tests/ui/collapsible_if.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
#![allow(clippy::cyclomatic_complexity)]
2+
#![allow(clippy::cyclomatic_complexity, clippy::assertions_on_constants)]
33

44
#[rustfmt::skip]
55
#[warn(clippy::collapsible_if)]

tests/ui/collapsible_if.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
#![allow(clippy::cyclomatic_complexity)]
2+
#![allow(clippy::cyclomatic_complexity, clippy::assertions_on_constants)]
33

44
#[rustfmt::skip]
55
#[warn(clippy::collapsible_if)]

tests/ui/empty_line_after_outer_attribute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![warn(clippy::empty_line_after_outer_attr)]
2-
2+
#![allow(clippy::assertions_on_constants)]
33
// This should produce a warning
44
#[crate_type = "lib"]
55

tests/ui/panic_unimplemented.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![warn(clippy::panic_params, clippy::unimplemented)]
2-
2+
#![allow(clippy::assertions_on_constants)]
33
fn missing() {
44
if true {
55
panic!("{}");

0 commit comments

Comments
 (0)