@@ -15,21 +15,40 @@ pub struct Rename {
1515}
1616
1717#[ derive( Debug , Serialize ) ]
18- pub struct DisallowedPath < const REPLACEMENT_ALLOWED : bool = true> {
18+ pub struct DisallowedPathWithoutReplacement {
19+ path : String ,
20+ reason : Option < String > ,
21+ }
22+
23+ #[ derive( Clone , Debug , Serialize ) ]
24+ pub struct DisallowedPath {
1925 path : String ,
2026 reason : Option < String > ,
2127 replacement : Option < String > ,
2228}
2329
24- impl < ' de , const REPLACEMENT_ALLOWED : bool > Deserialize < ' de > for DisallowedPath < REPLACEMENT_ALLOWED > {
30+ impl < ' de > Deserialize < ' de > for DisallowedPathWithoutReplacement {
2531 fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
2632 where
2733 D : Deserializer < ' de > ,
2834 {
2935 let enum_ = DisallowedPathEnum :: deserialize ( deserializer) ?;
30- if ! REPLACEMENT_ALLOWED && enum_. replacement ( ) . is_some ( ) {
36+ if enum_. replacement ( ) . is_some ( ) {
3137 return Err ( de:: Error :: custom ( "replacement not allowed for this configuration" ) ) ;
3238 }
39+ Ok ( Self {
40+ path : enum_. path ( ) . to_owned ( ) ,
41+ reason : enum_. reason ( ) . map ( ToOwned :: to_owned) ,
42+ } )
43+ }
44+ }
45+
46+ impl < ' de > Deserialize < ' de > for DisallowedPath {
47+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
48+ where
49+ D : Deserializer < ' de > ,
50+ {
51+ let enum_ = DisallowedPathEnum :: deserialize ( deserializer) ?;
3352 Ok ( Self {
3453 path : enum_. path ( ) . to_owned ( ) ,
3554 reason : enum_. reason ( ) . map ( ToOwned :: to_owned) ,
@@ -49,24 +68,45 @@ pub enum DisallowedPathEnum {
4968 } ,
5069}
5170
52- impl < const REPLACEMENT_ALLOWED : bool > DisallowedPath < REPLACEMENT_ALLOWED > {
53- pub fn path ( & self ) -> & str {
71+ pub trait AmendDiag {
72+ fn path ( & self ) -> & str ;
73+ fn reason ( & self ) -> Option < & str > ;
74+ fn replacement ( & self ) -> Option < & str > ;
75+ fn amend_diag ( & self , span : Span , diag : & mut Diag < ' _ , ( ) > ) {
76+ if let Some ( replacement) = & self . replacement ( ) {
77+ diag. span_suggestion (
78+ span,
79+ self . reason ( ) . map_or_else ( || String :: from ( "use" ) , ToOwned :: to_owned) ,
80+ replacement,
81+ Applicability :: MachineApplicable ,
82+ ) ;
83+ } else if let Some ( reason) = self . reason ( ) {
84+ diag. note ( reason. to_owned ( ) ) ;
85+ }
86+ }
87+ }
88+
89+ impl AmendDiag for DisallowedPathWithoutReplacement {
90+ fn path ( & self ) -> & str {
5491 & self . path
5592 }
93+ fn reason ( & self ) -> Option < & str > {
94+ self . reason . as_deref ( )
95+ }
96+ fn replacement ( & self ) -> Option < & str > {
97+ None
98+ }
99+ }
56100
57- pub fn diag_amendment ( & self , span : Span ) -> impl FnOnce ( & mut Diag < ' _ , ( ) > ) + use < ' _ , REPLACEMENT_ALLOWED > {
58- move |diag| {
59- if let Some ( replacement) = & self . replacement {
60- diag. span_suggestion (
61- span,
62- self . reason . as_ref ( ) . map_or_else ( || String :: from ( "use" ) , Clone :: clone) ,
63- replacement,
64- Applicability :: MachineApplicable ,
65- ) ;
66- } else if let Some ( reason) = & self . reason {
67- diag. note ( reason. clone ( ) ) ;
68- }
69- }
101+ impl AmendDiag for DisallowedPath {
102+ fn path ( & self ) -> & str {
103+ & self . path
104+ }
105+ fn reason ( & self ) -> Option < & str > {
106+ self . reason . as_deref ( )
107+ }
108+ fn replacement ( & self ) -> Option < & str > {
109+ self . replacement . as_deref ( )
70110 }
71111}
72112
@@ -93,10 +133,10 @@ impl DisallowedPathEnum {
93133}
94134
95135/// Creates a map of disallowed items to the reason they were disallowed.
96- pub fn create_disallowed_map < const REPLACEMENT_ALLOWED : bool > (
136+ pub fn create_disallowed_map < T : AmendDiag > (
97137 tcx : TyCtxt < ' _ > ,
98- disallowed : & ' static [ DisallowedPath < REPLACEMENT_ALLOWED > ] ,
99- ) -> DefIdMap < ( & ' static str , & ' static DisallowedPath < REPLACEMENT_ALLOWED > ) > {
138+ disallowed : & ' static [ T ] ,
139+ ) -> DefIdMap < ( & ' static str , & ' static T ) > {
100140 disallowed
101141 . iter ( )
102142 . map ( |x| ( x. path ( ) , x. path ( ) . split ( "::" ) . collect :: < Vec < _ > > ( ) , x) )
0 commit comments