2021-06-03 01:41:37 -05:00
|
|
|
use clippy_utils::consts::{constant, Constant};
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2021-08-08 09:49:13 -05:00
|
|
|
use clippy_utils::higher;
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::source::snippet_opt;
|
2021-11-29 08:34:36 -06:00
|
|
|
use clippy_utils::{is_direct_expn_of, is_expn_of, match_panic_call, peel_blocks};
|
2019-10-05 09:45:02 -05:00
|
|
|
use if_chain::if_chain;
|
2021-01-01 12:38:11 -06:00
|
|
|
use rustc_hir::{Expr, ExprKind, UnOp};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2018-12-25 16:29:03 -06:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for `assert!(true)` and `assert!(false)` calls.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Will be optimized out by the compiler or should probably be replaced by a
|
2020-03-17 20:50:39 -05:00
|
|
|
/// `panic!()` or `unreachable!()`
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Known problems
|
|
|
|
/// None
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-10 17:01:56 -05:00
|
|
|
/// ```rust,ignore
|
2019-01-30 19:15:29 -06:00
|
|
|
/// assert!(false)
|
|
|
|
/// assert!(true)
|
2019-03-05 10:50:33 -06:00
|
|
|
/// const B: bool = false;
|
2019-01-30 19:15:29 -06:00
|
|
|
/// assert!(B)
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
|
|
|
#[clippy::version = "1.34.0"]
|
2019-01-09 04:38:38 -06:00
|
|
|
pub ASSERTIONS_ON_CONSTANTS,
|
|
|
|
style,
|
2019-03-10 12:19:47 -05:00
|
|
|
"`assert!(true)` / `assert!(false)` will be optimized out by the compiler, and should probably be replaced by a `panic!()` or `unreachable!()`"
|
2018-12-25 16:29:03 -06:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
|
2018-12-25 16:29:03 -06:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
2020-01-19 20:52:58 -06:00
|
|
|
let lint_true = |is_debug: bool| {
|
2020-01-26 19:56:22 -06:00
|
|
|
span_lint_and_help(
|
2019-10-07 15:08:00 -05:00
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
|
|
|
e.span,
|
2020-01-19 20:52:58 -06:00
|
|
|
if is_debug {
|
|
|
|
"`debug_assert!(true)` will be optimized out by the compiler"
|
|
|
|
} else {
|
|
|
|
"`assert!(true)` will be optimized out by the compiler"
|
|
|
|
},
|
2020-04-18 05:28:29 -05:00
|
|
|
None,
|
2019-10-07 15:08:00 -05:00
|
|
|
"remove it",
|
|
|
|
);
|
|
|
|
};
|
|
|
|
let lint_false_without_message = || {
|
2020-01-26 19:56:22 -06:00
|
|
|
span_lint_and_help(
|
2019-10-07 15:08:00 -05:00
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
|
|
|
e.span,
|
|
|
|
"`assert!(false)` should probably be replaced",
|
2020-04-18 05:28:29 -05:00
|
|
|
None,
|
2019-10-07 15:08:00 -05:00
|
|
|
"use `panic!()` or `unreachable!()`",
|
|
|
|
);
|
2019-08-17 13:46:44 -05:00
|
|
|
};
|
2019-10-07 15:08:00 -05:00
|
|
|
let lint_false_with_message = |panic_message: String| {
|
2020-01-26 19:56:22 -06:00
|
|
|
span_lint_and_help(
|
2019-10-07 15:08:00 -05:00
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
|
|
|
e.span,
|
|
|
|
&format!("`assert!(false, {})` should probably be replaced", panic_message),
|
2020-04-18 05:28:29 -05:00
|
|
|
None,
|
2019-10-07 15:08:00 -05:00
|
|
|
&format!("use `panic!({})` or `unreachable!({})`", panic_message, panic_message),
|
2021-06-03 01:41:37 -05:00
|
|
|
);
|
2019-10-07 15:08:00 -05:00
|
|
|
};
|
|
|
|
|
2019-08-17 13:46:44 -05:00
|
|
|
if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") {
|
2019-08-19 11:30:32 -05:00
|
|
|
if debug_assert_span.from_expansion() {
|
2019-08-17 13:46:44 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-10-07 15:08:00 -05:00
|
|
|
if_chain! {
|
2021-04-08 10:50:13 -05:00
|
|
|
if let ExprKind::Unary(_, lit) = e.kind;
|
2020-07-17 03:47:04 -05:00
|
|
|
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.typeck_results(), lit);
|
2019-10-07 15:08:00 -05:00
|
|
|
if is_true;
|
|
|
|
then {
|
2020-01-19 20:52:58 -06:00
|
|
|
lint_true(true);
|
2019-10-07 15:08:00 -05:00
|
|
|
}
|
|
|
|
};
|
2019-08-17 13:46:44 -05:00
|
|
|
} else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") {
|
2019-08-19 11:30:32 -05:00
|
|
|
if assert_span.from_expansion() {
|
2019-08-17 13:46:44 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-04-08 10:50:13 -05:00
|
|
|
if let Some(assert_match) = match_assert_with_message(cx, e) {
|
2019-10-07 15:08:00 -05:00
|
|
|
match assert_match {
|
|
|
|
// matched assert but not message
|
|
|
|
AssertKind::WithoutMessage(false) => lint_false_without_message(),
|
2020-01-19 20:52:58 -06:00
|
|
|
AssertKind::WithoutMessage(true) | AssertKind::WithMessage(_, true) => lint_true(false),
|
2019-10-07 15:08:00 -05:00
|
|
|
AssertKind::WithMessage(panic_message, false) => lint_false_with_message(panic_message),
|
|
|
|
};
|
2019-10-05 09:45:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 15:08:00 -05:00
|
|
|
/// Result of calling `match_assert_with_message`.
|
|
|
|
enum AssertKind {
|
|
|
|
WithMessage(String, bool),
|
|
|
|
WithoutMessage(bool),
|
|
|
|
}
|
|
|
|
|
2019-10-06 13:10:30 -05:00
|
|
|
/// Check if the expression matches
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
2021-01-01 12:38:11 -06:00
|
|
|
/// if !c {
|
|
|
|
/// {
|
|
|
|
/// ::std::rt::begin_panic(message, _)
|
|
|
|
/// }
|
|
|
|
/// }
|
2019-10-06 13:10:30 -05:00
|
|
|
/// ```
|
|
|
|
///
|
2019-10-07 15:08:00 -05:00
|
|
|
/// where `message` is any expression and `c` is a constant bool.
|
2020-06-25 15:41:36 -05:00
|
|
|
fn match_assert_with_message<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<AssertKind> {
|
2019-10-05 09:45:02 -05:00
|
|
|
if_chain! {
|
2021-08-08 09:49:13 -05:00
|
|
|
if let Some(higher::If { cond, then, .. }) = higher::If::hir(expr);
|
2021-08-26 10:25:14 -05:00
|
|
|
if let ExprKind::Unary(UnOp::Not, expr) = cond.kind;
|
2019-10-06 13:10:30 -05:00
|
|
|
// bind the first argument of the `assert!` macro
|
2020-07-17 03:47:04 -05:00
|
|
|
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.typeck_results(), expr);
|
2021-11-29 08:34:36 -06:00
|
|
|
let begin_panic_call = peel_blocks(then);
|
2019-10-05 09:45:02 -05:00
|
|
|
// function call
|
2021-04-22 04:31:13 -05:00
|
|
|
if let Some(arg) = match_panic_call(cx, begin_panic_call);
|
2019-10-07 15:08:00 -05:00
|
|
|
// bind the second argument of the `assert!` macro if it exists
|
2021-04-22 04:31:13 -05:00
|
|
|
if let panic_message = snippet_opt(cx, arg.span);
|
2019-10-06 13:10:30 -05:00
|
|
|
// second argument of begin_panic is irrelevant
|
|
|
|
// as is the second match arm
|
2019-10-05 09:45:02 -05:00
|
|
|
then {
|
2019-10-07 15:08:00 -05:00
|
|
|
// an empty message occurs when it was generated by the macro
|
|
|
|
// (and not passed by the user)
|
|
|
|
return panic_message
|
|
|
|
.filter(|msg| !msg.is_empty())
|
|
|
|
.map(|msg| AssertKind::WithMessage(msg, is_true))
|
|
|
|
.or(Some(AssertKind::WithoutMessage(is_true)));
|
2018-12-25 16:29:03 -06:00
|
|
|
}
|
|
|
|
}
|
2019-10-06 13:10:30 -05:00
|
|
|
None
|
|
|
|
}
|