Skip to content

Commit 010c2d3

Browse files
[explicit_deref_methods]: do not lint on method chains (#14921)
changelog: [`explicit_deref_methods`]: do not lint on method chains fixes rust-lang/rust-clippy#14593
2 parents 10ec6fc + bb19ae5 commit 010c2d3

File tree

4 files changed

+15
-25
lines changed

4 files changed

+15
-25
lines changed

clippy_lints/src/dereference.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
305305
RefOp::Method { mutbl, is_ufcs }
306306
if !is_lint_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id)
307307
// Allow explicit deref in method chains. e.g. `foo.deref().bar()`
308-
&& (is_ufcs || !in_postfix_position(cx, expr)) =>
308+
&& (is_ufcs || !is_in_method_chain(cx, expr)) =>
309309
{
310310
let ty_changed_count = usize::from(!deref_method_same_type(expr_ty, typeck.expr_ty(sub_expr)));
311311
self.state = Some((
@@ -728,7 +728,13 @@ fn deref_method_same_type<'tcx>(result_ty: Ty<'tcx>, arg_ty: Ty<'tcx>) -> bool {
728728
}
729729
}
730730

731-
fn in_postfix_position<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> bool {
731+
fn is_in_method_chain<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> bool {
732+
if let ExprKind::MethodCall(_, recv, _, _) = e.kind
733+
&& matches!(recv.kind, ExprKind::MethodCall(..))
734+
{
735+
return true;
736+
}
737+
732738
if let Some(parent) = get_parent_expr(cx, e)
733739
&& parent.span.eq_ctxt(e.span)
734740
{

tests/ui/explicit_deref_methods.fixed

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,10 @@ fn main() {
8181
let b: String = concat(just_return(a));
8282
//~^ explicit_deref_methods
8383

84-
let b: &str = &**a;
85-
//~^ explicit_deref_methods
84+
let b: &str = a.deref().deref();
8685

8786
let opt_a = Some(a.clone());
88-
let b = &*opt_a.unwrap();
89-
//~^ explicit_deref_methods
87+
let b = opt_a.unwrap().deref();
9088

9189
Aaa::deref(&Aaa);
9290
Aaa::deref_mut(&mut Aaa);

tests/ui/explicit_deref_methods.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,9 @@ fn main() {
8282
//~^ explicit_deref_methods
8383

8484
let b: &str = a.deref().deref();
85-
//~^ explicit_deref_methods
8685

8786
let opt_a = Some(a.clone());
8887
let b = opt_a.unwrap().deref();
89-
//~^ explicit_deref_methods
9088

9189
Aaa::deref(&Aaa);
9290
Aaa::deref_mut(&mut Aaa);

tests/ui/explicit_deref_methods.stderr

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,40 +56,28 @@ LL | let b: String = concat(just_return(a).deref());
5656
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `just_return(a)`
5757

5858
error: explicit `deref` method call
59-
--> tests/ui/explicit_deref_methods.rs:84:19
60-
|
61-
LL | let b: &str = a.deref().deref();
62-
| ^^^^^^^^^^^^^^^^^ help: try: `&**a`
63-
64-
error: explicit `deref` method call
65-
--> tests/ui/explicit_deref_methods.rs:88:13
66-
|
67-
LL | let b = opt_a.unwrap().deref();
68-
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*opt_a.unwrap()`
69-
70-
error: explicit `deref` method call
71-
--> tests/ui/explicit_deref_methods.rs:123:31
59+
--> tests/ui/explicit_deref_methods.rs:121:31
7260
|
7361
LL | let b: &str = expr_deref!(a.deref());
7462
| ^^^^^^^^^ help: try: `&*a`
7563

7664
error: explicit `deref` method call
77-
--> tests/ui/explicit_deref_methods.rs:141:14
65+
--> tests/ui/explicit_deref_methods.rs:139:14
7866
|
7967
LL | let _ = &Deref::deref(&"foo");
8068
| ^^^^^^^^^^^^^^^^^^^^ help: try: `*&"foo"`
8169

8270
error: explicit `deref_mut` method call
83-
--> tests/ui/explicit_deref_methods.rs:143:14
71+
--> tests/ui/explicit_deref_methods.rs:141:14
8472
|
8573
LL | let _ = &DerefMut::deref_mut(&mut x);
8674
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut **&mut x`
8775

8876
error: explicit `deref_mut` method call
89-
--> tests/ui/explicit_deref_methods.rs:144:14
77+
--> tests/ui/explicit_deref_methods.rs:142:14
9078
|
9179
LL | let _ = &DerefMut::deref_mut((&mut &mut x).deref_mut());
9280
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut ***(&mut &mut x)`
9381

94-
error: aborting due to 15 previous errors
82+
error: aborting due to 13 previous errors
9583

0 commit comments

Comments
 (0)