Skip to content

Commit 8c9040c

Browse files
committed
Auto merge of rust-lang#9329 - xphoniex:fix-rust-lang#9317, r=flip1995
Skip `unnecessary_to_owned` when `t != t.to_string()` Fixes rust-lang#9317 changelog: [`unnecessary_to_owned`]: none
2 parents 679fa9f + 1a2aaf6 commit 8c9040c

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

clippy_lints/src/methods/unnecessary_to_owned.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use super::unnecessary_iter_cloned::{self, is_into_iter};
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::source::snippet_opt;
55
use clippy_utils::ty::{
6-
contains_ty, get_associated_type, get_iterator_item_ty, implements_trait, is_copy, peel_mid_ty_refs,
6+
contains_ty, get_associated_type, get_iterator_item_ty, implements_trait, is_copy, is_type_diagnostic_item,
7+
peel_mid_ty_refs,
78
};
89
use clippy_utils::{meets_msrv, msrvs};
910

@@ -279,7 +280,19 @@ fn check_other_call_arg<'tcx>(
279280
&trait_predicate.trait_ref.substs.iter().skip(1).collect::<Vec<_>>()[..],
280281
call_substs,
281282
);
282-
implements_trait(cx, receiver_ty, as_ref_trait_id, &composed_substs)
283+
// if `expr` is a `String` and generic target is [u8], skip
284+
// (https://github.com/rust-lang/rust-clippy/issues/9317).
285+
if let [subst] = composed_substs[..]
286+
&& let GenericArgKind::Type(arg_ty) = subst.unpack()
287+
&& arg_ty.is_slice()
288+
&& let inner_ty = arg_ty.builtin_index().unwrap()
289+
&& let ty::Uint(ty::UintTy::U8) = inner_ty.kind()
290+
&& let self_ty = cx.typeck_results().expr_ty(expr).peel_refs()
291+
&& is_type_diagnostic_item(cx, self_ty, sym::String) {
292+
false
293+
} else {
294+
implements_trait(cx, receiver_ty, as_ref_trait_id, &composed_substs)
295+
}
283296
} else {
284297
false
285298
};

tests/ui/unnecessary_to_owned.fixed

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,31 @@ mod issue_8759_variant {
329329
rw.set_view(&rw.default_view().to_owned());
330330
}
331331
}
332+
333+
mod issue_9317 {
334+
#![allow(dead_code)]
335+
336+
struct Bytes {}
337+
338+
impl ToString for Bytes {
339+
fn to_string(&self) -> String {
340+
"123".to_string()
341+
}
342+
}
343+
344+
impl AsRef<[u8]> for Bytes {
345+
fn as_ref(&self) -> &[u8] {
346+
&[1, 2, 3]
347+
}
348+
}
349+
350+
fn consume<C: AsRef<[u8]>>(c: C) {
351+
let _ = c;
352+
}
353+
354+
pub fn main() {
355+
let b = Bytes {};
356+
// Should not lint.
357+
consume(b.to_string());
358+
}
359+
}

tests/ui/unnecessary_to_owned.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,31 @@ mod issue_8759_variant {
329329
rw.set_view(&rw.default_view().to_owned());
330330
}
331331
}
332+
333+
mod issue_9317 {
334+
#![allow(dead_code)]
335+
336+
struct Bytes {}
337+
338+
impl ToString for Bytes {
339+
fn to_string(&self) -> String {
340+
"123".to_string()
341+
}
342+
}
343+
344+
impl AsRef<[u8]> for Bytes {
345+
fn as_ref(&self) -> &[u8] {
346+
&[1, 2, 3]
347+
}
348+
}
349+
350+
fn consume<C: AsRef<[u8]>>(c: C) {
351+
let _ = c;
352+
}
353+
354+
pub fn main() {
355+
let b = Bytes {};
356+
// Should not lint.
357+
consume(b.to_string());
358+
}
359+
}

0 commit comments

Comments
 (0)