Skip to content

Commit 4cddd0e

Browse files
committed
Auto merge of #134258 - bjorn3:no_public_specialization, r=petrochenkov
Remove support for specializing ToString outside the standard library This is the only trait specializable outside of the standard library. Before stabilizing specialization we will probably want to remove support for this. It was originally made specializable to allow a more efficient ToString in libproc_macro back when this way the only way to get any data out of a TokenStream. We now support getting individual tokens, so proc macros no longer need to call it as often.
2 parents 25d004d + bc410fc commit 4cddd0e

File tree

3 files changed

+1
-59
lines changed

3 files changed

+1
-59
lines changed

clippy_lints/src/to_string_trait_impl.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::ty::implements_trait;
32
use rustc_hir::{Impl, Item, ItemKind};
43
use rustc_lint::{LateContext, LateLintPass};
54
use rustc_session::declare_lint_pass;
@@ -54,8 +53,6 @@ impl<'tcx> LateLintPass<'tcx> for ToStringTraitImpl {
5453
}) = it.kind
5554
&& let Some(trait_did) = trait_ref.trait_def_id()
5655
&& cx.tcx.is_diagnostic_item(sym::ToString, trait_did)
57-
&& let Some(display_did) = cx.tcx.get_diagnostic_item(sym::Display)
58-
&& !implements_trait(cx, cx.tcx.type_of(it.owner_id).instantiate_identity(), display_did, &[])
5956
{
6057
span_lint_and_help(
6158
cx,

tests/ui/to_string_trait_impl.rs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,46 +30,3 @@ impl Bar {
3030
String::from("Bar")
3131
}
3232
}
33-
34-
mod issue12263 {
35-
pub struct MyStringWrapper<'a>(&'a str);
36-
37-
impl std::fmt::Display for MyStringWrapper<'_> {
38-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39-
self.0.fmt(f)
40-
}
41-
}
42-
43-
impl ToString for MyStringWrapper<'_> {
44-
fn to_string(&self) -> String {
45-
self.0.to_string()
46-
}
47-
}
48-
49-
pub struct S<T>(T);
50-
impl std::fmt::Display for S<String> {
51-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52-
todo!()
53-
}
54-
}
55-
// no specialization if the generics differ, so lint
56-
impl ToString for S<i32> {
57-
fn to_string(&self) -> String {
58-
todo!()
59-
}
60-
}
61-
62-
pub struct S2<T>(T);
63-
impl std::fmt::Display for S2<String> {
64-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65-
todo!()
66-
}
67-
}
68-
69-
// also specialization if the generics don't differ
70-
impl ToString for S2<String> {
71-
fn to_string(&self) -> String {
72-
todo!()
73-
}
74-
}
75-
}

tests/ui/to_string_trait_impl.stderr

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,5 @@ LL | | }
1212
= note: `-D clippy::to-string-trait-impl` implied by `-D warnings`
1313
= help: to override `-D warnings` add `#[allow(clippy::to_string_trait_impl)]`
1414

15-
error: direct implementation of `ToString`
16-
--> tests/ui/to_string_trait_impl.rs:56:5
17-
|
18-
LL | / impl ToString for S<i32> {
19-
LL | | fn to_string(&self) -> String {
20-
LL | | todo!()
21-
LL | | }
22-
LL | | }
23-
| |_____^
24-
|
25-
= help: prefer implementing `Display` instead
26-
27-
error: aborting due to 2 previous errors
15+
error: aborting due to 1 previous error
2816

0 commit comments

Comments
 (0)