Skip to content

Commit 2507d2a

Browse files
committed
Don't assume argument type is a type parameter
1 parent 37ee141 commit 2507d2a

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

clippy_lints/src/dereference.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,10 @@ fn needless_borrow_impl_arg_position<'tcx>(
980980
return None;
981981
}
982982

983+
if !matches!(child_arg_ty.kind(), ty::Param(_)) {
984+
return None;
985+
}
986+
983987
// If `child_arg_ty` is a type parameter that appears in more than one place, then substituting
984988
// it with `T` instead of `&T` could cause a type error.
985989
if cx

tests/ui/needless_borrow.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ fn main() {
132132
let _ = std::path::Path::new(".").join(".");
133133

134134
let _ = Box::new(&""); // Don't lint. Type parameter has no trait bounds
135+
ref_as_ref_path(&""); // Don't lint. Argument type is not a type parameter
135136
}
136137

137138
#[allow(clippy::needless_borrowed_reference)]
@@ -163,3 +164,9 @@ fn check_expect_suppression() {
163164
#[expect(clippy::needless_borrow)]
164165
let _ = x(&&a);
165166
}
167+
168+
fn ref_as_ref_path<T: 'static>(_: &'static T)
169+
where
170+
&'static T: AsRef<std::path::Path>,
171+
{
172+
}

tests/ui/needless_borrow.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ fn main() {
132132
let _ = std::path::Path::new(".").join(&&".");
133133

134134
let _ = Box::new(&""); // Don't lint. Type parameter has no trait bounds
135+
ref_as_ref_path(&""); // Don't lint. Argument type is not a type parameter
135136
}
136137

137138
#[allow(clippy::needless_borrowed_reference)]
@@ -163,3 +164,9 @@ fn check_expect_suppression() {
163164
#[expect(clippy::needless_borrow)]
164165
let _ = x(&&a);
165166
}
167+
168+
fn ref_as_ref_path<T: 'static>(_: &'static T)
169+
where
170+
&'static T: AsRef<std::path::Path>,
171+
{
172+
}

0 commit comments

Comments
 (0)