2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
2024-06-13 05:30:48 -05:00
|
|
|
use clippy_utils::visitors::for_each_expr_without_closures;
|
2021-07-19 04:52:05 -05:00
|
|
|
use clippy_utils::{binop_traits, trait_ref_of_method, BINOP_TRAITS, OP_ASSIGN_TRAITS};
|
2022-10-06 02:44:38 -05:00
|
|
|
use core::ops::ControlFlow;
|
2020-01-06 10:39:50 -06:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::declare_lint_pass;
|
2018-02-13 08:40:17 -06:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Lints for suspicious operations in impls of arithmetic operators, e.g.
|
2019-03-05 10:50:33 -06:00
|
|
|
/// subtracting elements in an Add impl.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// This is probably a typo or copy-and-paste error and not intended.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 16:23:50 -06:00
|
|
|
/// ```ignore
|
2019-03-05 10:50:33 -06:00
|
|
|
/// impl Add for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
|
|
|
/// fn add(self, other: Foo) -> Foo {
|
|
|
|
/// Foo(self.0 - other.0)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2018-02-13 08:40:17 -06:00
|
|
|
pub SUSPICIOUS_ARITHMETIC_IMPL,
|
2021-07-01 11:17:38 -05:00
|
|
|
suspicious,
|
2018-02-13 08:40:17 -06:00
|
|
|
"suspicious use of operators in impl of arithmetic trait"
|
|
|
|
}
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Lints for suspicious operations in impls of OpAssign, e.g.
|
2019-03-05 10:50:33 -06:00
|
|
|
/// subtracting elements in an AddAssign impl.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// This is probably a typo or copy-and-paste error and not intended.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 16:23:50 -06:00
|
|
|
/// ```ignore
|
2019-03-05 10:50:33 -06:00
|
|
|
/// impl AddAssign for Foo {
|
|
|
|
/// fn add_assign(&mut self, other: Foo) {
|
|
|
|
/// *self = *self - other;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2018-02-13 08:40:17 -06:00
|
|
|
pub SUSPICIOUS_OP_ASSIGN_IMPL,
|
2021-07-01 11:17:38 -05:00
|
|
|
suspicious,
|
2018-02-13 08:40:17 -06:00
|
|
|
"suspicious use of operators in impl of OpAssign trait"
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(SuspiciousImpl => [SUSPICIOUS_ARITHMETIC_IMPL, SUSPICIOUS_OP_ASSIGN_IMPL]);
|
2018-02-13 08:40:17 -06:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for SuspiciousImpl {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
|
2023-11-16 12:13:24 -06:00
|
|
|
if let hir::ExprKind::Binary(binop, _, _) | hir::ExprKind::AssignOp(binop, ..) = expr.kind
|
|
|
|
&& let Some((binop_trait_lang, op_assign_trait_lang)) = binop_traits(binop.node)
|
|
|
|
&& let Some(binop_trait_id) = cx.tcx.lang_items().get(binop_trait_lang)
|
|
|
|
&& let Some(op_assign_trait_id) = cx.tcx.lang_items().get(op_assign_trait_lang)
|
2020-08-11 08:43:21 -05:00
|
|
|
|
|
|
|
// Check for more than one binary operation in the implemented function
|
|
|
|
// Linting when multiple operations are involved can result in false positives
|
2023-11-16 12:13:24 -06:00
|
|
|
&& let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id).def_id
|
2023-12-01 07:28:34 -06:00
|
|
|
&& let hir::Node::ImplItem(impl_item) = cx.tcx.hir_node_by_def_id(parent_fn)
|
2023-11-16 12:13:24 -06:00
|
|
|
&& let hir::ImplItemKind::Fn(_, body_id) = impl_item.kind
|
|
|
|
&& let body = cx.tcx.hir().body(body_id)
|
|
|
|
&& let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id).def_id
|
|
|
|
&& let Some(trait_ref) = trait_ref_of_method(cx, parent_fn)
|
|
|
|
&& let trait_id = trait_ref.path.res.def_id()
|
|
|
|
&& ![binop_trait_id, op_assign_trait_id].contains(&trait_id)
|
|
|
|
&& let Some(&(_, lint)) = [
|
2021-07-19 04:52:05 -05:00
|
|
|
(&BINOP_TRAITS, SUSPICIOUS_ARITHMETIC_IMPL),
|
|
|
|
(&OP_ASSIGN_TRAITS, SUSPICIOUS_OP_ASSIGN_IMPL),
|
|
|
|
]
|
|
|
|
.iter()
|
2023-11-16 12:13:24 -06:00
|
|
|
.find(|&(ts, _)| ts.iter().any(|&t| Some(trait_id) == cx.tcx.lang_items().get(t)))
|
|
|
|
&& count_binops(body.value) == 1
|
|
|
|
{
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
lint,
|
|
|
|
binop.span,
|
2024-04-04 12:52:55 -05:00
|
|
|
format!(
|
2023-11-16 12:13:24 -06:00
|
|
|
"suspicious use of `{}` in `{}` impl",
|
|
|
|
binop.node.as_str(),
|
|
|
|
cx.tcx.item_name(trait_id)
|
|
|
|
),
|
|
|
|
);
|
2018-02-13 08:40:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 04:52:05 -05:00
|
|
|
fn count_binops(expr: &hir::Expr<'_>) -> u32 {
|
2022-10-06 02:44:38 -05:00
|
|
|
let mut count = 0u32;
|
2024-06-13 05:30:48 -05:00
|
|
|
let _: Option<!> = for_each_expr_without_closures(expr, |e| {
|
2022-10-06 02:44:38 -05:00
|
|
|
if matches!(
|
|
|
|
e.kind,
|
2018-07-12 02:30:57 -05:00
|
|
|
hir::ExprKind::Binary(..)
|
2022-10-06 02:44:38 -05:00
|
|
|
| hir::ExprKind::Unary(hir::UnOp::Not | hir::UnOp::Neg, _)
|
|
|
|
| hir::ExprKind::AssignOp(..)
|
|
|
|
) {
|
|
|
|
count += 1;
|
2018-02-13 08:40:17 -06:00
|
|
|
}
|
2022-10-06 02:44:38 -05:00
|
|
|
ControlFlow::Continue(())
|
|
|
|
});
|
|
|
|
count
|
2018-02-13 08:40:17 -06:00
|
|
|
}
|