2021-08-26 09:14:37 -05:00
|
|
|
use clippy_utils::{diagnostics::span_lint_and_sugg, higher, is_direct_expn_of, ty::implements_trait};
|
|
|
|
use rustc_ast::ast::LitKind;
|
2021-04-15 08:26:57 -05:00
|
|
|
use rustc_errors::Applicability;
|
2021-08-26 11:18:17 -05:00
|
|
|
use rustc_hir::{Expr, ExprKind, Lit};
|
2021-08-26 09:14:37 -05:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_middle::ty;
|
2021-04-15 08:26:57 -05:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-08-26 09:14:37 -05:00
|
|
|
use rustc_span::symbol::Ident;
|
2021-04-15 08:26:57 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// This lint warns about boolean comparisons in assert-like macros.
|
2021-04-15 08:26:57 -05:00
|
|
|
///
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It is shorter to use the equivalent.
|
2021-04-15 08:26:57 -05:00
|
|
|
///
|
2021-07-02 13:37:11 -05:00
|
|
|
/// ### Example
|
2021-04-15 08:26:57 -05:00
|
|
|
/// ```rust
|
|
|
|
/// // Bad
|
|
|
|
/// assert_eq!("a".is_empty(), false);
|
|
|
|
/// assert_ne!("a".is_empty(), true);
|
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// assert!(!"a".is_empty());
|
|
|
|
/// ```
|
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.53.0"]
|
2021-04-15 08:26:57 -05:00
|
|
|
pub BOOL_ASSERT_COMPARISON,
|
|
|
|
style,
|
|
|
|
"Using a boolean as comparison value in an assert_* macro when there is no need"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(BoolAssertComparison => [BOOL_ASSERT_COMPARISON]);
|
|
|
|
|
2021-08-26 09:14:37 -05:00
|
|
|
fn is_bool_lit(e: &Expr<'_>) -> bool {
|
2021-04-15 08:26:57 -05:00
|
|
|
matches!(
|
|
|
|
e.kind,
|
|
|
|
ExprKind::Lit(Lit {
|
2021-08-26 09:14:37 -05:00
|
|
|
node: LitKind::Bool(_),
|
2021-04-15 08:26:57 -05:00
|
|
|
..
|
|
|
|
})
|
|
|
|
) && !e.span.from_expansion()
|
|
|
|
}
|
|
|
|
|
2021-08-31 02:06:14 -05:00
|
|
|
fn is_impl_not_trait_with_bool_out(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> bool {
|
2021-08-26 09:14:37 -05:00
|
|
|
let ty = cx.typeck_results().expr_ty(e);
|
|
|
|
|
|
|
|
cx.tcx
|
|
|
|
.lang_items()
|
|
|
|
.not_trait()
|
2021-08-31 02:06:14 -05:00
|
|
|
.filter(|trait_id| implements_trait(cx, ty, *trait_id, &[]))
|
|
|
|
.and_then(|trait_id| {
|
|
|
|
cx.tcx.associated_items(trait_id).find_by_name_and_kind(
|
2021-08-26 09:14:37 -05:00
|
|
|
cx.tcx,
|
|
|
|
Ident::from_str("Output"),
|
|
|
|
ty::AssocKind::Type,
|
2021-08-31 02:06:14 -05:00
|
|
|
trait_id,
|
2021-08-26 09:14:37 -05:00
|
|
|
)
|
|
|
|
})
|
2021-08-31 02:06:14 -05:00
|
|
|
.map_or(false, |assoc_item| {
|
|
|
|
let proj = cx.tcx.mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(ty, &[]));
|
2021-08-26 09:14:37 -05:00
|
|
|
let nty = cx.tcx.normalize_erasing_regions(cx.param_env, proj);
|
|
|
|
|
|
|
|
nty.is_bool()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2021-04-15 08:26:57 -05:00
|
|
|
let macros = ["assert_eq", "debug_assert_eq"];
|
|
|
|
let inverted_macros = ["assert_ne", "debug_assert_ne"];
|
|
|
|
|
|
|
|
for mac in macros.iter().chain(inverted_macros.iter()) {
|
2021-08-26 09:14:37 -05:00
|
|
|
if let Some(span) = is_direct_expn_of(expr.span, mac) {
|
|
|
|
if let Some(args) = higher::extract_assert_macro_args(expr) {
|
|
|
|
if let [a, b, ..] = args[..] {
|
2021-11-08 14:44:50 -06:00
|
|
|
let nb_bool_args = usize::from(is_bool_lit(a)) + usize::from(is_bool_lit(b));
|
2021-08-26 09:14:37 -05:00
|
|
|
|
|
|
|
if nb_bool_args != 1 {
|
|
|
|
// If there are two boolean arguments, we definitely don't understand
|
|
|
|
// what's going on, so better leave things as is...
|
|
|
|
//
|
|
|
|
// Or there is simply no boolean and then we can leave things as is!
|
|
|
|
return;
|
|
|
|
}
|
2021-04-15 08:26:57 -05:00
|
|
|
|
2021-08-31 02:06:14 -05:00
|
|
|
if !is_impl_not_trait_with_bool_out(cx, a) || !is_impl_not_trait_with_bool_out(cx, b) {
|
2021-08-26 09:14:37 -05:00
|
|
|
// At this point the expression which is not a boolean
|
|
|
|
// literal does not implement Not trait with a bool output,
|
|
|
|
// so we cannot suggest to rewrite our code
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let non_eq_mac = &mac[..mac.len() - 3];
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
BOOL_ASSERT_COMPARISON,
|
|
|
|
span,
|
|
|
|
&format!("used `{}!` with a literal bool", mac),
|
|
|
|
"replace it with",
|
|
|
|
format!("{}!(..)", non_eq_mac),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
2021-04-15 08:26:57 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|