Skip to content

Commit

Permalink
Auto merge of rust-lang#9329 - xphoniex:fix-rust-lang#9317, r=flip1995
Browse files Browse the repository at this point in the history
Skip `unnecessary_to_owned` when `t != t.to_string()`

Fixes rust-lang#9317

changelog: [`unnecessary_to_owned`]: none
  • Loading branch information
bors committed Aug 15, 2022
2 parents 679fa9f + 1a2aaf6 commit 8c9040c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
17 changes: 15 additions & 2 deletions clippy_lints/src/methods/unnecessary_to_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use super::unnecessary_iter_cloned::{self, is_into_iter};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_opt;
use clippy_utils::ty::{
contains_ty, get_associated_type, get_iterator_item_ty, implements_trait, is_copy, peel_mid_ty_refs,
contains_ty, get_associated_type, get_iterator_item_ty, implements_trait, is_copy, is_type_diagnostic_item,
peel_mid_ty_refs,
};
use clippy_utils::{meets_msrv, msrvs};

Expand Down Expand Up @@ -279,7 +280,19 @@ fn check_other_call_arg<'tcx>(
&trait_predicate.trait_ref.substs.iter().skip(1).collect::<Vec<_>>()[..],
call_substs,
);
implements_trait(cx, receiver_ty, as_ref_trait_id, &composed_substs)
// if `expr` is a `String` and generic target is [u8], skip
// (https://github.com/rust-lang/rust-clippy/issues/9317).
if let [subst] = composed_substs[..]
&& let GenericArgKind::Type(arg_ty) = subst.unpack()
&& arg_ty.is_slice()
&& let inner_ty = arg_ty.builtin_index().unwrap()
&& let ty::Uint(ty::UintTy::U8) = inner_ty.kind()
&& let self_ty = cx.typeck_results().expr_ty(expr).peel_refs()
&& is_type_diagnostic_item(cx, self_ty, sym::String) {
false
} else {
implements_trait(cx, receiver_ty, as_ref_trait_id, &composed_substs)
}
} else {
false
};
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/unnecessary_to_owned.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,31 @@ mod issue_8759_variant {
rw.set_view(&rw.default_view().to_owned());
}
}

mod issue_9317 {
#![allow(dead_code)]

struct Bytes {}

impl ToString for Bytes {
fn to_string(&self) -> String {
"123".to_string()
}
}

impl AsRef<[u8]> for Bytes {
fn as_ref(&self) -> &[u8] {
&[1, 2, 3]
}
}

fn consume<C: AsRef<[u8]>>(c: C) {
let _ = c;
}

pub fn main() {
let b = Bytes {};
// Should not lint.
consume(b.to_string());
}
}
28 changes: 28 additions & 0 deletions tests/ui/unnecessary_to_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,31 @@ mod issue_8759_variant {
rw.set_view(&rw.default_view().to_owned());
}
}

mod issue_9317 {
#![allow(dead_code)]

struct Bytes {}

impl ToString for Bytes {
fn to_string(&self) -> String {
"123".to_string()
}
}

impl AsRef<[u8]> for Bytes {
fn as_ref(&self) -> &[u8] {
&[1, 2, 3]
}
}

fn consume<C: AsRef<[u8]>>(c: C) {
let _ = c;
}

pub fn main() {
let b = Bytes {};
// Should not lint.
consume(b.to_string());
}
}

0 comments on commit 8c9040c

Please sign in to comment.