Remove code duplication
This commit is contained in:
parent
40218bae0c
commit
834ad76806
1 changed files with 21 additions and 45 deletions
|
@ -4,7 +4,6 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
||||||
use rustc::{declare_lint_pass, declare_tool_lint};
|
use rustc::{declare_lint_pass, declare_tool_lint};
|
||||||
|
|
||||||
use crate::consts::{constant, Constant};
|
use crate::consts::{constant, Constant};
|
||||||
use crate::syntax::ast::LitKind;
|
|
||||||
use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint};
|
use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint};
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
|
@ -43,51 +42,28 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
|
||||||
!in_macro(span)
|
!in_macro(span)
|
||||||
});
|
});
|
||||||
if let ExprKind::Unary(_, ref lit) = e.node;
|
if let ExprKind::Unary(_, ref lit) = e.node;
|
||||||
|
if let Some(bool_const) = constant(cx, cx.tables, lit);
|
||||||
then {
|
then {
|
||||||
if let ExprKind::Lit(ref inner) = lit.node {
|
match bool_const.0 {
|
||||||
match inner.node {
|
Constant::Bool(true) => {
|
||||||
LitKind::Bool(true) => {
|
span_help_and_lint(
|
||||||
span_help_and_lint(
|
cx,
|
||||||
cx,
|
ASSERTIONS_ON_CONSTANTS,
|
||||||
ASSERTIONS_ON_CONSTANTS,
|
e.span,
|
||||||
e.span,
|
"`assert!(true)` will be optimized out by the compiler",
|
||||||
"`assert!(true)` will be optimized out by the compiler",
|
"remove it"
|
||||||
"remove it"
|
);
|
||||||
);
|
},
|
||||||
},
|
Constant::Bool(false) if !is_debug_assert => {
|
||||||
LitKind::Bool(false) if !is_debug_assert => {
|
span_help_and_lint(
|
||||||
span_help_and_lint(
|
cx,
|
||||||
cx,
|
ASSERTIONS_ON_CONSTANTS,
|
||||||
ASSERTIONS_ON_CONSTANTS,
|
e.span,
|
||||||
e.span,
|
"`assert!(false)` should probably be replaced",
|
||||||
"`assert!(false)` should probably be replaced",
|
"use `panic!()` or `unreachable!()`"
|
||||||
"use `panic!()` or `unreachable!()`"
|
);
|
||||||
);
|
},
|
||||||
},
|
_ => (),
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
} else if let Some(bool_const) = constant(cx, cx.tables, lit) {
|
|
||||||
match bool_const.0 {
|
|
||||||
Constant::Bool(true) => {
|
|
||||||
span_help_and_lint(
|
|
||||||
cx,
|
|
||||||
ASSERTIONS_ON_CONSTANTS,
|
|
||||||
e.span,
|
|
||||||
"`assert!(const: true)` will be optimized out by the compiler",
|
|
||||||
"remove it"
|
|
||||||
);
|
|
||||||
},
|
|
||||||
Constant::Bool(false) if !is_debug_assert => {
|
|
||||||
span_help_and_lint(
|
|
||||||
cx,
|
|
||||||
ASSERTIONS_ON_CONSTANTS,
|
|
||||||
e.span,
|
|
||||||
"`assert!(const: false)` should probably be replaced",
|
|
||||||
"use `panic!()` or `unreachable!()`"
|
|
||||||
);
|
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue