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};
|
2022-05-08 14:12:56 -05:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::declare_lint_pass;
|
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! {
|
2021-07-29 05:16:06 -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.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// The mutable reference rules out all other references to
|
2020-08-28 09:10:16 -05:00
|
|
|
/// the value. Also the code misleads about the intent of the call site.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-06-16 10:39:06 -05:00
|
|
|
/// # let mut vec = Vec::new();
|
|
|
|
/// # let mut value = 5;
|
|
|
|
/// vec.push(&mut value);
|
|
|
|
/// ```
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
2022-06-16 10:39:06 -05:00
|
|
|
/// Use instead:
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-06-16 10:39:06 -05:00
|
|
|
/// # let mut vec = Vec::new();
|
|
|
|
/// # let value = 5;
|
|
|
|
/// vec.push(&value);
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
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<'_>) {
|
2023-08-11 07:05:13 -05:00
|
|
|
if e.span.from_expansion() {
|
|
|
|
// Issue #11268
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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,
|
2022-09-01 04:43:35 -05:00
|
|
|
arguments.iter().collect(),
|
2020-07-17 03:47:04 -05:00
|
|
|
cx.typeck_results().expr_ty(fn_expr),
|
2023-10-09 22:02:42 -05:00
|
|
|
&rustc_hir_pretty::qpath_to_string(path),
|
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
|
|
|
},
|
2022-09-01 04:43:35 -05:00
|
|
|
ExprKind::MethodCall(path, receiver, arguments, _) => {
|
2020-07-17 03:47:04 -05:00
|
|
|
let def_id = cx.typeck_results().type_dependent_def_id(e.hir_id).unwrap();
|
2023-07-11 16:35:29 -05:00
|
|
|
let args = cx.typeck_results().node_args(e.hir_id);
|
|
|
|
let method_type = cx.tcx.type_of(def_id).instantiate(cx.tcx, args);
|
2022-09-01 04:43:35 -05:00
|
|
|
check_arguments(
|
|
|
|
cx,
|
|
|
|
std::iter::once(receiver).chain(arguments.iter()).collect(),
|
|
|
|
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>,
|
2022-09-01 04:43:35 -05:00
|
|
|
arguments: Vec<&Expr<'_>>,
|
2020-08-28 09:10:16 -05:00
|
|
|
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,
|
2022-10-06 02:44:38 -05:00
|
|
|
&format!("the {fn_kind} `{name}` doesn't need a mutable reference"),
|
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
|
|
|
}
|
|
|
|
}
|