2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
|
|
|
use clippy_utils::{get_trait_def_id, paths, trait_ref_of_method};
|
2018-11-27 20:14:15 +00:00
|
|
|
use if_chain::if_chain;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir as hir;
|
2020-01-09 07:13:22 +00:00
|
|
|
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::hir::map::Map;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2018-02-13 14:40:17 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Lints for suspicious operations in impls of arithmetic operators, e.g.
|
|
|
|
/// subtracting elements in an Add impl.
|
|
|
|
///
|
|
|
|
/// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-03-05 22:23:50 +00:00
|
|
|
/// ```ignore
|
2019-03-05 16:50:33 +00:00
|
|
|
/// impl Add for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
|
|
|
/// fn add(self, other: Foo) -> Foo {
|
|
|
|
/// Foo(self.0 - other.0)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-02-13 14:40:17 +00:00
|
|
|
pub SUSPICIOUS_ARITHMETIC_IMPL,
|
2018-03-28 13:24:26 +00:00
|
|
|
correctness,
|
2018-02-13 14:40:17 +00:00
|
|
|
"suspicious use of operators in impl of arithmetic trait"
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Lints for suspicious operations in impls of OpAssign, e.g.
|
|
|
|
/// subtracting elements in an AddAssign impl.
|
|
|
|
///
|
|
|
|
/// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-03-05 22:23:50 +00:00
|
|
|
/// ```ignore
|
2019-03-05 16:50:33 +00:00
|
|
|
/// impl AddAssign for Foo {
|
|
|
|
/// fn add_assign(&mut self, other: Foo) {
|
|
|
|
/// *self = *self - other;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-02-13 14:40:17 +00:00
|
|
|
pub SUSPICIOUS_OP_ASSIGN_IMPL,
|
2018-03-28 13:24:26 +00:00
|
|
|
correctness,
|
2018-02-13 14:40:17 +00:00
|
|
|
"suspicious use of operators in impl of OpAssign trait"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(SuspiciousImpl => [SUSPICIOUS_ARITHMETIC_IMPL, SUSPICIOUS_OP_ASSIGN_IMPL]);
|
2018-02-13 14:40:17 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for SuspiciousImpl {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
|
2020-04-05 18:40:51 +00:00
|
|
|
if let hir::ExprKind::Binary(binop, _, _) | hir::ExprKind::AssignOp(binop, ..) = expr.kind {
|
2018-03-17 20:34:13 +00:00
|
|
|
match binop.node {
|
2018-11-27 20:14:15 +00:00
|
|
|
hir::BinOpKind::Eq
|
2018-07-16 13:07:39 +00:00
|
|
|
| hir::BinOpKind::Lt
|
|
|
|
| hir::BinOpKind::Le
|
|
|
|
| hir::BinOpKind::Ne
|
|
|
|
| hir::BinOpKind::Ge
|
2018-11-27 20:14:15 +00:00
|
|
|
| hir::BinOpKind::Gt => return,
|
2018-03-17 20:34:13 +00:00
|
|
|
_ => {},
|
|
|
|
}
|
2020-08-11 13:43:21 +00:00
|
|
|
|
|
|
|
// Check for more than one binary operation in the implemented function
|
|
|
|
// Linting when multiple operations are involved can result in false positives
|
2021-04-08 15:50:13 +00:00
|
|
|
let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);
|
2020-08-11 13:43:21 +00:00
|
|
|
if_chain! {
|
|
|
|
if let hir::Node::ImplItem(impl_item) = cx.tcx.hir().get(parent_fn);
|
|
|
|
if let hir::ImplItemKind::Fn(_, body_id) = impl_item.kind;
|
|
|
|
then {
|
2021-04-08 15:50:13 +00:00
|
|
|
let body = cx.tcx.hir().body(body_id);
|
|
|
|
let mut visitor = BinaryExprVisitor { nb_binops: 0 };
|
2020-08-11 13:43:21 +00:00
|
|
|
walk_expr(&mut visitor, &body.value);
|
|
|
|
if visitor.nb_binops > 1 {
|
|
|
|
return;
|
2018-02-13 14:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(impl_trait) = check_binop(
|
|
|
|
cx,
|
|
|
|
expr,
|
2018-05-31 18:15:48 +00:00
|
|
|
binop.node,
|
2020-08-28 14:10:16 +00:00
|
|
|
&[
|
|
|
|
"Add", "Sub", "Mul", "Div", "Rem", "BitAnd", "BitOr", "BitXor", "Shl", "Shr",
|
|
|
|
],
|
2018-07-16 13:07:39 +00:00
|
|
|
&[
|
|
|
|
hir::BinOpKind::Add,
|
|
|
|
hir::BinOpKind::Sub,
|
|
|
|
hir::BinOpKind::Mul,
|
|
|
|
hir::BinOpKind::Div,
|
2020-08-28 14:10:16 +00:00
|
|
|
hir::BinOpKind::Rem,
|
|
|
|
hir::BinOpKind::BitAnd,
|
|
|
|
hir::BinOpKind::BitOr,
|
|
|
|
hir::BinOpKind::BitXor,
|
|
|
|
hir::BinOpKind::Shl,
|
|
|
|
hir::BinOpKind::Shr,
|
2018-07-16 13:07:39 +00:00
|
|
|
],
|
2018-02-13 14:40:17 +00:00
|
|
|
) {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
SUSPICIOUS_ARITHMETIC_IMPL,
|
|
|
|
binop.span,
|
2020-08-11 13:43:21 +00:00
|
|
|
&format!("suspicious use of binary operator in `{}` impl", impl_trait),
|
2018-02-13 14:40:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(impl_trait) = check_binop(
|
|
|
|
cx,
|
|
|
|
expr,
|
2018-05-31 18:15:48 +00:00
|
|
|
binop.node,
|
2018-02-13 14:40:17 +00:00
|
|
|
&[
|
2019-05-17 21:53:54 +00:00
|
|
|
"AddAssign",
|
|
|
|
"SubAssign",
|
|
|
|
"MulAssign",
|
|
|
|
"DivAssign",
|
|
|
|
"BitAndAssign",
|
|
|
|
"BitOrAssign",
|
|
|
|
"BitXorAssign",
|
|
|
|
"RemAssign",
|
|
|
|
"ShlAssign",
|
|
|
|
"ShrAssign",
|
2018-02-13 14:40:17 +00:00
|
|
|
],
|
|
|
|
&[
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::BinOpKind::Add,
|
|
|
|
hir::BinOpKind::Sub,
|
|
|
|
hir::BinOpKind::Mul,
|
|
|
|
hir::BinOpKind::Div,
|
|
|
|
hir::BinOpKind::BitAnd,
|
|
|
|
hir::BinOpKind::BitOr,
|
|
|
|
hir::BinOpKind::BitXor,
|
|
|
|
hir::BinOpKind::Rem,
|
|
|
|
hir::BinOpKind::Shl,
|
|
|
|
hir::BinOpKind::Shr,
|
2018-02-13 14:40:17 +00:00
|
|
|
],
|
|
|
|
) {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
SUSPICIOUS_OP_ASSIGN_IMPL,
|
|
|
|
binop.span,
|
2020-08-11 13:43:21 +00:00
|
|
|
&format!("suspicious use of binary operator in `{}` impl", impl_trait),
|
2018-02-13 14:40:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 23:34:08 +00:00
|
|
|
fn check_binop(
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &LateContext<'_>,
|
2019-12-27 07:12:26 +00:00
|
|
|
expr: &hir::Expr<'_>,
|
2018-07-12 07:50:09 +00:00
|
|
|
binop: hir::BinOpKind,
|
2019-05-17 21:53:54 +00:00
|
|
|
traits: &[&'static str],
|
2018-07-12 07:50:09 +00:00
|
|
|
expected_ops: &[hir::BinOpKind],
|
2019-05-17 21:53:54 +00:00
|
|
|
) -> Option<&'static str> {
|
2018-02-13 14:40:17 +00:00
|
|
|
let mut trait_ids = vec![];
|
2021-03-25 18:29:11 +00:00
|
|
|
let [krate, module] = paths::OPS_MODULE;
|
2018-02-13 14:40:17 +00:00
|
|
|
|
2019-05-13 23:34:08 +00:00
|
|
|
for &t in traits {
|
2018-02-13 14:40:17 +00:00
|
|
|
let path = [krate, module, t];
|
|
|
|
if let Some(trait_id) = get_trait_def_id(cx, &path) {
|
|
|
|
trait_ids.push(trait_id);
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the actually implemented trait
|
2019-02-24 18:43:15 +00:00
|
|
|
let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);
|
2018-02-13 14:40:17 +00:00
|
|
|
|
|
|
|
if_chain! {
|
2019-03-07 06:42:38 +00:00
|
|
|
if let Some(trait_ref) = trait_ref_of_method(cx, parent_fn);
|
2019-05-04 00:03:12 +00:00
|
|
|
if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.res.def_id());
|
2018-05-31 18:15:48 +00:00
|
|
|
if binop != expected_ops[idx];
|
2018-02-13 14:40:17 +00:00
|
|
|
then{
|
|
|
|
return Some(traits[idx])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BinaryExprVisitor {
|
2020-08-11 13:43:21 +00:00
|
|
|
nb_binops: u32,
|
2018-02-13 14:40:17 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 15:05:22 +00:00
|
|
|
impl<'tcx> Visitor<'tcx> for BinaryExprVisitor {
|
2020-01-09 07:13:22 +00:00
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
|
2019-09-27 15:16:06 +00:00
|
|
|
match expr.kind {
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Binary(..)
|
2021-02-09 08:15:53 +00:00
|
|
|
| hir::ExprKind::Unary(hir::UnOp::Not | hir::UnOp::Neg, _)
|
2020-08-11 13:43:21 +00:00
|
|
|
| hir::ExprKind::AssignOp(..) => self.nb_binops += 1,
|
2018-03-06 12:58:03 +00:00
|
|
|
_ => {},
|
2018-02-13 14:40:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
walk_expr(self, expr);
|
|
|
|
}
|
2020-08-11 13:43:21 +00:00
|
|
|
|
2020-03-15 22:41:20 +00:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
2018-02-13 14:40:17 +00:00
|
|
|
NestedVisitorMap::None
|
|
|
|
}
|
|
|
|
}
|