Skip to content

Commit c195c79

Browse files
committed
Don't assume argument type is a type parameter
1 parent ae034e3 commit c195c79

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
@@ -982,6 +982,10 @@ fn needless_borrow_impl_arg_position<'tcx>(
982982
return None;
983983
}
984984

985+
if !matches!(child_arg_ty.kind(), ty::Param(_)) {
986+
return None;
987+
}
988+
985989
// If `child_arg_ty` is a type parameter that appears in more than one place, then substituting
986990
// it with `T` instead of `&T` could cause a type error.
987991
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)