Don't lint explicit_auto_deref on reborrows

This commit is contained in:
Jason Newcomb 2022-01-27 10:17:14 -05:00
parent 8a74d33570
commit ee532c0222
5 changed files with 76 additions and 32 deletions

View file

@ -183,6 +183,10 @@ enum State {
deref_span: Span,
deref_hir_id: HirId,
},
Reborrow {
deref_span: Span,
deref_hir_id: HirId,
},
Borrow,
}
@ -395,10 +399,38 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
));
},
(Some((State::Borrow, data)), RefOp::Deref) => {
if typeck.expr_ty(sub_expr).is_ref() {
self.state = Some((
State::Reborrow {
deref_span: expr.span,
deref_hir_id: expr.hir_id,
},
data,
));
} else {
self.state = Some((
State::ExplicitDeref {
deref_span: expr.span,
deref_hir_id: expr.hir_id,
},
data,
));
}
},
(
Some((
State::Reborrow {
deref_span,
deref_hir_id,
},
data,
)),
RefOp::Deref,
) => {
self.state = Some((
State::ExplicitDeref {
deref_span: expr.span,
deref_hir_id: expr.hir_id,
deref_span,
deref_hir_id,
},
data,
));
@ -959,7 +991,7 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, state: State, data
},
);
},
State::Borrow => (),
State::Borrow | State::Reborrow { .. } => (),
}
}

View file

@ -1,4 +1,4 @@
#![allow(dead_code, unused_variables, clippy::explicit_auto_deref)]
#![allow(dead_code, unused_variables)]
fn main() {}

View file

@ -8,7 +8,8 @@
clippy::needless_borrow,
clippy::ptr_arg,
clippy::redundant_field_names,
clippy::too_many_arguments
clippy::too_many_arguments,
clippy::borrow_deref_ref
)]
trait CallableStr {
@ -48,6 +49,7 @@ impl<U: ?Sized> CallableT<U> for i32 {
}
fn f_str(_: &str) {}
fn f_string(_: &String) {}
fn f_t<T>(_: T) {}
fn f_ref_t<T: ?Sized>(_: &T) {}
@ -158,4 +160,8 @@ fn main() {
}
let _ = E2::S1(&*s); // Don't lint. Inferred type would change.
let _ = E2::S2 { s: &*s }; // Don't lint. Inferred type would change.
let ref_s = &s;
let _: &String = &*ref_s; // Don't lint reborrow.
f_string(&*ref_s); // Don't lint reborrow.
}

View file

@ -8,7 +8,8 @@
clippy::needless_borrow,
clippy::ptr_arg,
clippy::redundant_field_names,
clippy::too_many_arguments
clippy::too_many_arguments,
clippy::borrow_deref_ref
)]
trait CallableStr {
@ -48,6 +49,7 @@ impl<U: ?Sized> CallableT<U> for i32 {
}
fn f_str(_: &str) {}
fn f_string(_: &String) {}
fn f_t<T>(_: T) {}
fn f_ref_t<T: ?Sized>(_: &T) {}
@ -158,4 +160,8 @@ fn main() {
}
let _ = E2::S1(&*s); // Don't lint. Inferred type would change.
let _ = E2::S2 { s: &*s }; // Don't lint. Inferred type would change.
let ref_s = &s;
let _: &String = &*ref_s; // Don't lint reborrow.
f_string(&*ref_s); // Don't lint reborrow.
}

View file

@ -1,5 +1,5 @@
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:61:20
--> $DIR/explicit_auto_deref.rs:63:20
|
LL | let _: &str = &*s;
| ^^ help: try this: `s`
@ -7,151 +7,151 @@ LL | let _: &str = &*s;
= note: `-D clippy::explicit-auto-deref` implied by `-D warnings`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:65:12
--> $DIR/explicit_auto_deref.rs:67:12
|
LL | f_str(&*s);
| ^^ help: try this: `s`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:69:14
--> $DIR/explicit_auto_deref.rs:71:14
|
LL | f_str_t(&*s, &*s); // Don't lint second param.
| ^^ help: try this: `s`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:72:25
--> $DIR/explicit_auto_deref.rs:74:25
|
LL | let _: &Box<i32> = &**b;
| ^^^ help: try this: `b`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:78:8
--> $DIR/explicit_auto_deref.rs:80:8
|
LL | c(&*s);
| ^^ help: try this: `s`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:84:9
--> $DIR/explicit_auto_deref.rs:86:9
|
LL | &**x
| ^^^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:88:11
--> $DIR/explicit_auto_deref.rs:90:11
|
LL | { &**x }
| ^^^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:92:9
--> $DIR/explicit_auto_deref.rs:94:9
|
LL | &**{ x }
| ^^^^^^^^ help: try this: `{ x }`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:96:9
--> $DIR/explicit_auto_deref.rs:98:9
|
LL | &***x
| ^^^^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:113:13
--> $DIR/explicit_auto_deref.rs:115:13
|
LL | f1(&*x);
| ^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:114:13
--> $DIR/explicit_auto_deref.rs:116:13
|
LL | f2(&*x);
| ^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:115:13
--> $DIR/explicit_auto_deref.rs:117:13
|
LL | f3(&*x);
| ^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:116:28
--> $DIR/explicit_auto_deref.rs:118:28
|
LL | f4.callable_str()(&*x);
| ^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:117:13
--> $DIR/explicit_auto_deref.rs:119:13
|
LL | f5(&*x);
| ^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:118:13
--> $DIR/explicit_auto_deref.rs:120:13
|
LL | f6(&*x);
| ^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:119:28
--> $DIR/explicit_auto_deref.rs:121:28
|
LL | f7.callable_str()(&*x);
| ^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:120:26
--> $DIR/explicit_auto_deref.rs:122:26
|
LL | f8.callable_t()(&*x);
| ^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:121:13
--> $DIR/explicit_auto_deref.rs:123:13
|
LL | f9(&*x);
| ^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:122:14
--> $DIR/explicit_auto_deref.rs:124:14
|
LL | f10(&*x);
| ^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:123:27
--> $DIR/explicit_auto_deref.rs:125:27
|
LL | f11.callable_t()(&*x);
| ^^ help: try this: `x`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:127:17
--> $DIR/explicit_auto_deref.rs:129:17
|
LL | let _ = S1(&*s);
| ^^ help: try this: `s`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:132:22
--> $DIR/explicit_auto_deref.rs:134:22
|
LL | let _ = S2 { s: &*s };
| ^^ help: try this: `s`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:148:30
--> $DIR/explicit_auto_deref.rs:150:30
|
LL | let _ = Self::S1(&**s);
| ^^^^ help: try this: `s`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:149:35
--> $DIR/explicit_auto_deref.rs:151:35
|
LL | let _ = Self::S2 { s: &**s };
| ^^^^ help: try this: `s`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:152:21
--> $DIR/explicit_auto_deref.rs:154:21
|
LL | let _ = E1::S1(&*s);
| ^^ help: try this: `s`
error: deref which would be done by auto-deref
--> $DIR/explicit_auto_deref.rs:153:26
--> $DIR/explicit_auto_deref.rs:155:26
|
LL | let _ = E1::S2 { s: &*s };
| ^^ help: try this: `s`