2018-10-06 11:18:06 -05:00
|
|
|
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-09-15 02:21:58 -05:00
|
|
|
use crate::rustc::hir;
|
|
|
|
use crate::rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
2018-11-27 14:14:15 -06:00
|
|
|
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use crate::rustc::{declare_tool_lint, lint_array};
|
2018-09-15 02:21:58 -05:00
|
|
|
use crate::syntax::ast;
|
2018-05-30 03:15:50 -05:00
|
|
|
use crate::utils::{get_trait_def_id, span_lint};
|
2018-11-27 14:14:15 -06:00
|
|
|
use if_chain::if_chain;
|
2018-02-13 08:40:17 -06: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:**
|
|
|
|
/// ```rust
|
|
|
|
/// impl Add for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
|
|
|
/// fn add(self, other: Foo) -> Foo {
|
|
|
|
/// Foo(self.0 - other.0)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2018-02-13 08:40:17 -06:00
|
|
|
pub SUSPICIOUS_ARITHMETIC_IMPL,
|
2018-03-28 08:24:26 -05:00
|
|
|
correctness,
|
2018-02-13 08:40:17 -06:00
|
|
|
"suspicious use of operators in impl of arithmetic trait"
|
|
|
|
}
|
|
|
|
|
|
|
|
/// **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:**
|
|
|
|
/// ```rust
|
|
|
|
/// impl AddAssign for Foo {
|
|
|
|
/// fn add_assign(&mut self, other: Foo) {
|
|
|
|
/// *self = *self - other;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2018-02-13 08:40:17 -06:00
|
|
|
pub SUSPICIOUS_OP_ASSIGN_IMPL,
|
2018-03-28 08:24:26 -05:00
|
|
|
correctness,
|
2018-02-13 08:40:17 -06:00
|
|
|
"suspicious use of operators in impl of OpAssign trait"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct SuspiciousImpl;
|
|
|
|
|
|
|
|
impl LintPass for SuspiciousImpl {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array![SUSPICIOUS_ARITHMETIC_IMPL, SUSPICIOUS_OP_ASSIGN_IMPL]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let hir::ExprKind::Binary(binop, _, _) = expr.node {
|
2018-03-17 15:34:13 -05:00
|
|
|
match binop.node {
|
2018-11-27 14:14:15 -06:00
|
|
|
hir::BinOpKind::Eq
|
2018-07-16 08:07:39 -05:00
|
|
|
| hir::BinOpKind::Lt
|
|
|
|
| hir::BinOpKind::Le
|
|
|
|
| hir::BinOpKind::Ne
|
|
|
|
| hir::BinOpKind::Ge
|
2018-11-27 14:14:15 -06:00
|
|
|
| hir::BinOpKind::Gt => return,
|
2018-03-17 15:34:13 -05:00
|
|
|
_ => {},
|
|
|
|
}
|
2018-03-06 06:58:03 -06:00
|
|
|
// Check if the binary expression is part of another bi/unary expression
|
2018-02-13 08:40:17 -06:00
|
|
|
// as a child node
|
2018-12-07 18:56:03 -06:00
|
|
|
let mut parent_expr = cx.tcx.hir().get_parent_node(expr.id);
|
2018-02-13 08:40:17 -06:00
|
|
|
while parent_expr != ast::CRATE_NODE_ID {
|
2018-12-07 18:56:03 -06:00
|
|
|
if let hir::Node::Expr(e) = cx.tcx.hir().get(parent_expr) {
|
2018-03-06 06:58:03 -06:00
|
|
|
match e.node {
|
2018-07-12 02:30:57 -05:00
|
|
|
hir::ExprKind::Binary(..)
|
|
|
|
| hir::ExprKind::Unary(hir::UnOp::UnNot, _)
|
|
|
|
| hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => return,
|
2018-03-06 06:58:03 -06:00
|
|
|
_ => {},
|
2018-02-13 08:40:17 -06:00
|
|
|
}
|
|
|
|
}
|
2018-12-07 18:56:03 -06:00
|
|
|
parent_expr = cx.tcx.hir().get_parent_node(parent_expr);
|
2018-02-13 08:40:17 -06:00
|
|
|
}
|
|
|
|
// as a parent node
|
2018-11-27 14:14:15 -06:00
|
|
|
let mut visitor = BinaryExprVisitor { in_binary_expr: false };
|
2018-02-13 08:40:17 -06:00
|
|
|
walk_expr(&mut visitor, expr);
|
|
|
|
|
|
|
|
if visitor.in_binary_expr {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(impl_trait) = check_binop(
|
|
|
|
cx,
|
|
|
|
expr,
|
2018-05-31 13:15:48 -05:00
|
|
|
binop.node,
|
2018-02-13 08:40:17 -06:00
|
|
|
&["Add", "Sub", "Mul", "Div"],
|
2018-07-16 08:07:39 -05:00
|
|
|
&[
|
|
|
|
hir::BinOpKind::Add,
|
|
|
|
hir::BinOpKind::Sub,
|
|
|
|
hir::BinOpKind::Mul,
|
|
|
|
hir::BinOpKind::Div,
|
|
|
|
],
|
2018-02-13 08:40:17 -06:00
|
|
|
) {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
SUSPICIOUS_ARITHMETIC_IMPL,
|
|
|
|
binop.span,
|
2018-11-27 14:14:15 -06:00
|
|
|
&format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
|
2018-02-13 08:40:17 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(impl_trait) = check_binop(
|
|
|
|
cx,
|
|
|
|
expr,
|
2018-05-31 13:15:48 -05:00
|
|
|
binop.node,
|
2018-02-13 08:40:17 -06:00
|
|
|
&[
|
|
|
|
"AddAssign",
|
|
|
|
"SubAssign",
|
|
|
|
"MulAssign",
|
|
|
|
"DivAssign",
|
|
|
|
"BitAndAssign",
|
|
|
|
"BitOrAssign",
|
|
|
|
"BitXorAssign",
|
|
|
|
"RemAssign",
|
|
|
|
"ShlAssign",
|
|
|
|
"ShrAssign",
|
|
|
|
],
|
|
|
|
&[
|
2018-07-16 08:07:39 -05: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 08:40:17 -06:00
|
|
|
],
|
|
|
|
) {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
SUSPICIOUS_OP_ASSIGN_IMPL,
|
|
|
|
binop.span,
|
2018-11-27 14:14:15 -06:00
|
|
|
&format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
|
2018-02-13 08:40:17 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_binop<'a>(
|
2018-07-23 06:01:12 -05:00
|
|
|
cx: &LateContext<'_, '_>,
|
2018-02-13 08:40:17 -06:00
|
|
|
expr: &hir::Expr,
|
2018-07-12 02:50:09 -05:00
|
|
|
binop: hir::BinOpKind,
|
2018-02-13 08:40:17 -06:00
|
|
|
traits: &[&'a str],
|
2018-07-12 02:50:09 -05:00
|
|
|
expected_ops: &[hir::BinOpKind],
|
2018-02-13 08:40:17 -06:00
|
|
|
) -> Option<&'a str> {
|
|
|
|
let mut trait_ids = vec![];
|
2018-05-30 03:15:50 -05:00
|
|
|
let [krate, module] = crate::utils::paths::OPS_MODULE;
|
2018-02-13 08:40:17 -06:00
|
|
|
|
|
|
|
for t in traits {
|
|
|
|
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
|
2018-12-07 18:56:03 -06:00
|
|
|
let parent_fn = cx.tcx.hir().get_parent(expr.id);
|
|
|
|
let parent_impl = cx.tcx.hir().get_parent(parent_fn);
|
2018-02-13 08:40:17 -06:00
|
|
|
|
|
|
|
if_chain! {
|
|
|
|
if parent_impl != ast::CRATE_NODE_ID;
|
2018-12-07 18:56:03 -06:00
|
|
|
if let hir::Node::Item(item) = cx.tcx.hir().get(parent_impl);
|
2018-07-16 08:07:39 -05:00
|
|
|
if let hir::ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node;
|
2018-02-13 08:40:17 -06:00
|
|
|
if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.def.def_id());
|
2018-05-31 13:15:48 -05:00
|
|
|
if binop != expected_ops[idx];
|
2018-02-13 08:40:17 -06:00
|
|
|
then{
|
|
|
|
return Some(traits[idx])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BinaryExprVisitor {
|
|
|
|
in_binary_expr: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx: 'a> Visitor<'tcx> for BinaryExprVisitor {
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
|
2018-03-06 06:58:03 -06:00
|
|
|
match expr.node {
|
2018-07-12 02:30:57 -05:00
|
|
|
hir::ExprKind::Binary(..)
|
|
|
|
| hir::ExprKind::Unary(hir::UnOp::UnNot, _)
|
2018-11-27 14:14:15 -06:00
|
|
|
| hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => self.in_binary_expr = true,
|
2018-03-06 06:58:03 -06:00
|
|
|
_ => {},
|
2018-02-13 08:40:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
|
|
|
NestedVisitorMap::None
|
|
|
|
}
|
|
|
|
}
|