mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 15:14:29 +00:00
Skip unnecessary_to_owned
when t != t.to_string()
Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
parent
dfa780e0ef
commit
1a2aaf68de
3 changed files with 71 additions and 2 deletions
|
@ -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};
|
||||
|
||||
|
@ -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
|
||||
};
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue