]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/suspicious_trait_impl.rs
Rollup merge of #106287 - Nilstrieb:its-bugging-me-how-we-dont-have-docs, r=jyn514
[rust.git] / src / tools / clippy / clippy_lints / src / suspicious_trait_impl.rs
1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::visitors::for_each_expr;
3 use clippy_utils::{binop_traits, trait_ref_of_method, BINOP_TRAITS, OP_ASSIGN_TRAITS};
4 use core::ops::ControlFlow;
5 use if_chain::if_chain;
6 use rustc_hir as hir;
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
9
10 declare_clippy_lint! {
11     /// ### What it does
12     /// Lints for suspicious operations in impls of arithmetic operators, e.g.
13     /// subtracting elements in an Add impl.
14     ///
15     /// ### Why is this bad?
16     /// This is probably a typo or copy-and-paste error and not intended.
17     ///
18     /// ### Example
19     /// ```ignore
20     /// impl Add for Foo {
21     ///     type Output = Foo;
22     ///
23     ///     fn add(self, other: Foo) -> Foo {
24     ///         Foo(self.0 - other.0)
25     ///     }
26     /// }
27     /// ```
28     #[clippy::version = "pre 1.29.0"]
29     pub SUSPICIOUS_ARITHMETIC_IMPL,
30     suspicious,
31     "suspicious use of operators in impl of arithmetic trait"
32 }
33
34 declare_clippy_lint! {
35     /// ### What it does
36     /// Lints for suspicious operations in impls of OpAssign, e.g.
37     /// subtracting elements in an AddAssign impl.
38     ///
39     /// ### Why is this bad?
40     /// This is probably a typo or copy-and-paste error and not intended.
41     ///
42     /// ### Example
43     /// ```ignore
44     /// impl AddAssign for Foo {
45     ///     fn add_assign(&mut self, other: Foo) {
46     ///         *self = *self - other;
47     ///     }
48     /// }
49     /// ```
50     #[clippy::version = "pre 1.29.0"]
51     pub SUSPICIOUS_OP_ASSIGN_IMPL,
52     suspicious,
53     "suspicious use of operators in impl of OpAssign trait"
54 }
55
56 declare_lint_pass!(SuspiciousImpl => [SUSPICIOUS_ARITHMETIC_IMPL, SUSPICIOUS_OP_ASSIGN_IMPL]);
57
58 impl<'tcx> LateLintPass<'tcx> for SuspiciousImpl {
59     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
60         if_chain! {
61             if let hir::ExprKind::Binary(binop, _, _) | hir::ExprKind::AssignOp(binop, ..) = expr.kind;
62             if let Some((binop_trait_lang, op_assign_trait_lang)) = binop_traits(binop.node);
63             if let Some(binop_trait_id) = cx.tcx.lang_items().get(binop_trait_lang);
64             if let Some(op_assign_trait_id) = cx.tcx.lang_items().get(op_assign_trait_lang);
65
66             // Check for more than one binary operation in the implemented function
67             // Linting when multiple operations are involved can result in false positives
68             let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id).def_id;
69             if let hir::Node::ImplItem(impl_item) = cx.tcx.hir().get_by_def_id(parent_fn);
70             if let hir::ImplItemKind::Fn(_, body_id) = impl_item.kind;
71             let body = cx.tcx.hir().body(body_id);
72             let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id).def_id;
73             if let Some(trait_ref) = trait_ref_of_method(cx, parent_fn);
74             let trait_id = trait_ref.path.res.def_id();
75             if ![binop_trait_id, op_assign_trait_id].contains(&trait_id);
76             if let Some(&(_, lint)) = [
77                 (&BINOP_TRAITS, SUSPICIOUS_ARITHMETIC_IMPL),
78                 (&OP_ASSIGN_TRAITS, SUSPICIOUS_OP_ASSIGN_IMPL),
79             ]
80                 .iter()
81                 .find(|&(ts, _)| ts.iter().any(|&t| Some(trait_id) == cx.tcx.lang_items().get(t)));
82             if count_binops(body.value) == 1;
83             then {
84                 span_lint(
85                     cx,
86                     lint,
87                     binop.span,
88                     &format!("suspicious use of `{}` in `{}` impl", binop.node.as_str(), cx.tcx.item_name(trait_id)),
89                 );
90             }
91         }
92     }
93 }
94
95 fn count_binops(expr: &hir::Expr<'_>) -> u32 {
96     let mut count = 0u32;
97     let _: Option<!> = for_each_expr(expr, |e| {
98         if matches!(
99             e.kind,
100             hir::ExprKind::Binary(..)
101                 | hir::ExprKind::Unary(hir::UnOp::Not | hir::UnOp::Neg, _)
102                 | hir::ExprKind::AssignOp(..)
103         ) {
104             count += 1;
105         }
106         ControlFlow::Continue(())
107     });
108     count
109 }