Skip to content

Commit 769336c

Browse files
committed
only check immutable
1 parent d1ba632 commit 769336c

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

clippy_lints/src/needless_deref.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,34 @@ use rustc_errors::Applicability;
66
use rustc_hir::ExprKind;
77
use rustc_hir::UnOp;
88
use rustc_lint::{LateContext, LateLintPass};
9+
use rustc_middle::mir::Mutability;
910
use rustc_middle::ty;
1011
use rustc_session::{declare_lint_pass, declare_tool_lint};
1112

1213
declare_clippy_lint! {
1314
/// ### What it does
14-
/// Checks for manual deref in function(no generic type) parameters.
15+
/// Checks for `&*(&T)`.
1516
///
1617
/// ### Why is this bad?
17-
/// There is no need to deref manually. Compiler will auto deref.
18+
/// If you just want to reborrow, `&T` is enough (`&T` is Copy).
19+
/// if you want to deref explicitly, `&** (&T)` is what you need.
1820
///
1921
/// ### Known problems
20-
/// Complicate type is not handled. For example `foo(&*****(&&T))`.
22+
/// None.
2123
///
2224
/// ### Example
2325
/// ```rust
24-
/// fn foo(_: &str) {}
25-
/// let pf = &String::new();
26+
/// let s = &String::new();
27+
///
2628
/// // Bad
27-
/// foo(&**pf);
28-
/// foo(&*String::new());
29+
/// let a: &String = &* s;
30+
/// foo(&*s);
2931
///
3032
/// // Good
31-
/// foo(pf);
32-
/// foo(&String::new());
33+
/// let a: &String = s;
34+
/// foo(&**s);
35+
///
36+
/// fn foo(_: &str){ }
3337
/// ```
3438
pub NEEDLESS_DEREF,
3539
complexity,
@@ -42,12 +46,11 @@ impl LateLintPass<'_> for NeedlessDeref {
4246
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx rustc_hir::Expr<'_>) {
4347
if_chain! {
4448
if !e.span.from_expansion() && !in_macro(e.span);
45-
if let ExprKind::AddrOf(_, ref outer_mutability, addrof_target) = e.kind;
49+
if let ExprKind::AddrOf(_, Mutability::Not, addrof_target) = e.kind;
4650
if let ExprKind::Unary(UnOp::Deref, deref_target) = addrof_target.kind;
4751
if !matches!(deref_target.kind, ExprKind::Unary(UnOp::Deref, ..) );
4852
let inner_ty = cx.typeck_results().expr_ty(deref_target);
49-
if let ty::Ref(_, _, inner_mutability) = inner_ty.kind();
50-
if inner_mutability == outer_mutability ;
53+
if let ty::Ref(_, _, Mutability::Not) = inner_ty.kind();
5154
then{
5255

5356
let map=cx.tcx.hir();

0 commit comments

Comments
 (0)