2021-06-03 06:41:37 +00:00
|
|
|
use clippy_utils::consts::{constant, Constant};
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2022-01-04 22:32:02 +00:00
|
|
|
use clippy_utils::macros::{find_assert_args, root_macro_call_first_node, PanicExpn};
|
|
|
|
use rustc_hir::Expr;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2022-01-04 22:32:02 +00:00
|
|
|
use rustc_span::sym;
|
2018-12-25 22:29:03 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for `assert!(true)` and `assert!(false)` calls.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Will be optimized out by the compiler or should probably be replaced by a
|
2020-03-18 01:50:39 +00:00
|
|
|
/// `panic!()` or `unreachable!()`
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Known problems
|
|
|
|
/// None
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-03-10 22:01:56 +00:00
|
|
|
/// ```rust,ignore
|
2019-01-31 01:15:29 +00:00
|
|
|
/// assert!(false)
|
|
|
|
/// assert!(true)
|
2019-03-05 16:50:33 +00:00
|
|
|
/// const B: bool = false;
|
2019-01-31 01:15:29 +00:00
|
|
|
/// assert!(B)
|
2019-03-05 16:50:33 +00: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 19:06:26 +00:00
|
|
|
#[clippy::version = "1.34.0"]
|
2019-01-09 10:38:38 +00:00
|
|
|
pub ASSERTIONS_ON_CONSTANTS,
|
|
|
|
style,
|
2019-03-10 17:19:47 +00: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 22:29:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
|
2018-12-25 22:29:03 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
2022-01-04 22:32:02 +00:00
|
|
|
let Some(macro_call) = root_macro_call_first_node(cx, e) else { return };
|
|
|
|
let is_debug = match cx.tcx.get_diagnostic_name(macro_call.def_id) {
|
|
|
|
Some(sym::debug_assert_macro) => true,
|
|
|
|
Some(sym::assert_macro) => false,
|
|
|
|
_ => return,
|
2019-10-07 20:08:00 +00:00
|
|
|
};
|
2022-01-04 22:32:02 +00:00
|
|
|
let Some((condition, panic_expn)) = find_assert_args(cx, e, macro_call.expn) else { return };
|
|
|
|
let Some((Constant::Bool(val), _)) = constant(cx, cx.typeck_results(), condition) else { return };
|
|
|
|
if val {
|
2020-01-27 01:56:22 +00:00
|
|
|
span_lint_and_help(
|
2019-10-07 20:08:00 +00:00
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
2022-01-04 22:32:02 +00:00
|
|
|
macro_call.span,
|
|
|
|
&format!(
|
|
|
|
"`{}!(true)` will be optimized out by the compiler",
|
|
|
|
cx.tcx.item_name(macro_call.def_id)
|
|
|
|
),
|
2020-04-18 10:28:29 +00:00
|
|
|
None,
|
2022-01-04 22:32:02 +00:00
|
|
|
"remove it",
|
2019-10-07 20:08:00 +00:00
|
|
|
);
|
2022-01-04 22:32:02 +00:00
|
|
|
} else if !is_debug {
|
|
|
|
let (assert_arg, panic_arg) = match panic_expn {
|
|
|
|
PanicExpn::Empty => ("", ""),
|
|
|
|
_ => (", ..", ".."),
|
|
|
|
};
|
2020-01-27 01:56:22 +00:00
|
|
|
span_lint_and_help(
|
2019-10-07 20:08:00 +00:00
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
2022-01-04 22:32:02 +00:00
|
|
|
macro_call.span,
|
|
|
|
&format!("`assert!(false{})` should probably be replaced", assert_arg),
|
2020-04-18 10:28:29 +00:00
|
|
|
None,
|
2022-01-04 22:32:02 +00:00
|
|
|
&format!("use `panic!({})` or `unreachable!({0})`", panic_arg),
|
2021-06-03 06:41:37 +00:00
|
|
|
);
|
2018-12-25 22:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-06 18:10:30 +00:00
|
|
|
}
|