2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
2020-03-27 09:34:29 -05:00
|
|
|
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-30 04:02:14 -05:00
|
|
|
use rustc_middle::ty::subst::Subst;
|
|
|
|
use rustc_middle::ty::{self, Ty};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-03-08 17:57:44 -06:00
|
|
|
use std::iter;
|
2015-09-29 06:11:19 -05:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2020-05-28 08:45:24 -05:00
|
|
|
/// **What it does:** Detects passing a mutable reference to a function that only
|
2019-03-05 10:50:33 -06:00
|
|
|
/// requires an immutable reference.
|
|
|
|
///
|
2020-08-28 09:10:16 -05:00
|
|
|
/// **Why is this bad?** The mutable reference rules out all other references to
|
|
|
|
/// the value. Also the code misleads about the intent of the call site.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-03-05 16:23:50 -06:00
|
|
|
/// ```ignore
|
2020-06-09 09:36:01 -05:00
|
|
|
/// // Bad
|
2019-03-05 10:50:33 -06:00
|
|
|
/// my_vec.push(&mut value)
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// my_vec.push(&value)
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2018-11-27 14:49:09 -06:00
|
|
|
pub UNNECESSARY_MUT_PASSED,
|
|
|
|
style,
|
|
|
|
"an argument passed as a mutable reference although the callee only demands an immutable reference"
|
2015-09-29 06:11:19 -05:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(UnnecessaryMutPassed => [UNNECESSARY_MUT_PASSED]);
|
2015-09-29 06:11:19 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for UnnecessaryMutPassed {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
2019-09-27 10:16:06 -05:00
|
|
|
match e.kind {
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::Call(fn_expr, arguments) => {
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ExprKind::Path(ref path) = fn_expr.kind {
|
2018-11-27 14:14:15 -06:00
|
|
|
check_arguments(
|
|
|
|
cx,
|
|
|
|
arguments,
|
2020-07-17 03:47:04 -05:00
|
|
|
cx.typeck_results().expr_ty(fn_expr),
|
2020-03-27 09:34:29 -05:00
|
|
|
&rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)),
|
2020-08-28 09:10:16 -05:00
|
|
|
"function",
|
2018-11-27 14:14:15 -06:00
|
|
|
);
|
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::MethodCall(path, _, arguments, _) => {
|
2020-07-17 03:47:04 -05:00
|
|
|
let def_id = cx.typeck_results().type_dependent_def_id(e.hir_id).unwrap();
|
|
|
|
let substs = cx.typeck_results().node_substs(e.hir_id);
|
2017-06-04 16:28:01 -05:00
|
|
|
let method_type = cx.tcx.type_of(def_id).subst(cx.tcx, substs);
|
2021-06-03 01:41:37 -05:00
|
|
|
check_arguments(cx, arguments, method_type, &path.ident.as_str(), "method");
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-04-14 13:14:03 -05:00
|
|
|
_ => (),
|
2015-09-29 06:11:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 06:08:29 -05:00
|
|
|
|
2020-08-28 09:10:16 -05:00
|
|
|
fn check_arguments<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
arguments: &[Expr<'_>],
|
|
|
|
type_definition: Ty<'tcx>,
|
|
|
|
name: &str,
|
|
|
|
fn_kind: &str,
|
|
|
|
) {
|
2020-08-03 17:18:29 -05:00
|
|
|
match type_definition.kind() {
|
2018-08-22 16:34:52 -05:00
|
|
|
ty::FnDef(..) | ty::FnPtr(_) => {
|
2017-06-29 08:38:25 -05:00
|
|
|
let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
|
2021-03-08 17:57:44 -06:00
|
|
|
for (argument, parameter) in iter::zip(arguments, parameters) {
|
2020-08-03 17:18:29 -05:00
|
|
|
match parameter.kind() {
|
2019-12-21 12:38:45 -06:00
|
|
|
ty::Ref(_, _, Mutability::Not)
|
2018-11-27 14:14:15 -06:00
|
|
|
| ty::RawPtr(ty::TypeAndMut {
|
2019-12-21 12:38:45 -06:00
|
|
|
mutbl: Mutability::Not, ..
|
2018-11-27 14:14:15 -06:00
|
|
|
}) => {
|
2019-12-21 12:38:45 -06:00
|
|
|
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _) = argument.kind {
|
2018-11-27 14:14:15 -06:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
UNNECESSARY_MUT_PASSED,
|
|
|
|
argument.span,
|
2020-08-28 09:10:16 -05:00
|
|
|
&format!("the {} `{}` doesn't need a mutable reference", fn_kind, name),
|
2018-11-27 14:14:15 -06:00
|
|
|
);
|
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-04-14 13:14:03 -05:00
|
|
|
_ => (),
|
2015-11-16 22:39:42 -06:00
|
|
|
}
|
2015-09-30 06:08:29 -05:00
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-03-10 11:13:49 -06:00
|
|
|
_ => (),
|
2015-09-30 06:08:29 -05:00
|
|
|
}
|
|
|
|
}
|