Skip to content

Commit 98c5f37

Browse files
author
A.A.Abroskin
committed
Add assert(true) and assert(false) lints
1 parent 81fa266 commit 98c5f37

File tree

7 files changed

+118
-3
lines changed

7 files changed

+118
-3
lines changed

clippy_lints/src/assert_checks.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
use crate::rustc::hir::{Expr, ExprKind};
11+
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12+
use crate::rustc::{declare_tool_lint, lint_array};
13+
use crate::syntax::ast::LitKind;
14+
use crate::utils::{is_direct_expn_of, span_lint};
15+
use if_chain::if_chain;
16+
17+
/// **What it does:** Check explicit call assert!(true)
18+
///
19+
/// **Why is this bad?** Will be optimized out by the compiler
20+
///
21+
/// **Known problems:** None
22+
///
23+
/// **Example:**
24+
/// ```rust
25+
/// assert!(true)
26+
/// ```
27+
declare_clippy_lint! {
28+
pub EXPLICIT_TRUE,
29+
correctness,
30+
"assert!(true) will be optimized out by the compiler"
31+
}
32+
33+
/// **What it does:** Check explicit call assert!(false)
34+
///
35+
/// **Why is this bad?** Should probably be replaced by a panic!()
36+
///
37+
/// **Known problems:** None
38+
///
39+
/// **Example:**
40+
/// ```rust
41+
/// assert!(false)
42+
/// ```
43+
declare_clippy_lint! {
44+
pub EXPLICIT_FALSE,
45+
correctness,
46+
"assert!(false) should probably be replaced by a panic!()r"
47+
}
48+
49+
pub struct AssertChecks;
50+
51+
impl LintPass for AssertChecks {
52+
fn get_lints(&self) -> LintArray {
53+
lint_array![EXPLICIT_TRUE, EXPLICIT_FALSE]
54+
}
55+
}
56+
57+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertChecks {
58+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
59+
if_chain! {
60+
if is_direct_expn_of(e.span, "assert").is_some();
61+
if let ExprKind::Unary(_, ref lit) = e.node;
62+
if let ExprKind::Lit(ref inner) = lit.node;
63+
then {
64+
match inner.node {
65+
LitKind::Bool(true) => {
66+
span_lint(cx, EXPLICIT_TRUE, e.span,
67+
"assert!(true) will be optimized out by the compiler");
68+
},
69+
LitKind::Bool(false) => {
70+
span_lint(cx, EXPLICIT_FALSE, e.span,
71+
"assert!(false) should probably be replaced by a panic!()");
72+
},
73+
_ => (),
74+
}
75+
}
76+
}
77+
}
78+
}

clippy_lints/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ mod utils;
8888
// begin lints modules, do not remove this comment, it’s used in `update_lints`
8989
pub mod approx_const;
9090
pub mod arithmetic;
91+
pub mod assert_checks;
9192
pub mod assign_ops;
9293
pub mod attrs;
9394
pub mod bit_mask;
@@ -486,6 +487,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
486487
reg.register_late_lint_pass(box ptr_offset_with_cast::Pass);
487488
reg.register_late_lint_pass(box redundant_clone::RedundantClone);
488489
reg.register_late_lint_pass(box slow_vector_initialization::Pass);
490+
reg.register_late_lint_pass(box assert_checks::AssertChecks);
489491

490492
reg.register_lint_group("clippy::restriction", Some("clippy_restriction"), vec![
491493
arithmetic::FLOAT_ARITHMETIC,
@@ -563,6 +565,8 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
563565

564566
reg.register_lint_group("clippy::all", Some("clippy"), vec![
565567
approx_const::APPROX_CONSTANT,
568+
assert_checks::EXPLICIT_TRUE,
569+
assert_checks::EXPLICIT_FALSE,
566570
assign_ops::ASSIGN_OP_PATTERN,
567571
assign_ops::MISREFACTORED_ASSIGN_OP,
568572
attrs::DEPRECATED_CFG_ATTR,
@@ -940,6 +944,8 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
940944

941945
reg.register_lint_group("clippy::correctness", Some("clippy_correctness"), vec![
942946
approx_const::APPROX_CONSTANT,
947+
assert_checks::EXPLICIT_TRUE,
948+
assert_checks::EXPLICIT_FALSE,
943949
attrs::DEPRECATED_SEMVER,
944950
attrs::USELESS_ATTRIBUTE,
945951
bit_mask::BAD_BIT_MASK,

tests/ui/assert_checks.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
fn main() {
11+
assert!(true);
12+
assert!(false);
13+
}

tests/ui/assert_checks.stderr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: assert!(true) will be optimized out by the compiler
2+
--> $DIR/assert_checks.rs:11:5
3+
|
4+
11 | assert!(true);
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: #[deny(clippy::explicit_true)] on by default
8+
9+
error: assert!(false) should probably be replaced by a panic!()
10+
--> $DIR/assert_checks.rs:12:5
11+
|
12+
12 | assert!(false);
13+
| ^^^^^^^^^^^^^^^
14+
|
15+
= note: #[deny(clippy::explicit_false)] on by default
16+
17+
error: aborting due to 2 previous errors
18+

tests/ui/attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// except according to those terms.
99

1010
#![warn(clippy::inline_always, clippy::deprecated_semver)]
11-
11+
#![allow(clippy::assert_checks::explicit_true)]
1212
#[inline(always)]
1313
fn test_attr_lint() {
1414
assert!(true)

tests/ui/empty_line_after_outer_attribute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// except according to those terms.
99

1010
#![warn(clippy::empty_line_after_outer_attr)]
11-
11+
#![allow(clippy::assert_checks::explicit_true)]
1212
// This should produce a warning
1313
#[crate_type = "lib"]
1414

tests/ui/panic_unimplemented.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// except according to those terms.
99

1010
#![warn(clippy::panic_params, clippy::unimplemented)]
11-
11+
#![allow(clippy::assert_checks::explicit_true)]
1212
fn missing() {
1313
if true {
1414
panic!("{}");

0 commit comments

Comments
 (0)