@@ -14,41 +14,24 @@ pub struct Rename {
14
14
pub rename : String ,
15
15
}
16
16
17
- #[ derive( Debug , Serialize ) ]
18
- pub struct DisallowedPathWithoutReplacement {
19
- path : String ,
20
- reason : Option < String > ,
21
- }
17
+ pub type DisallowedPathWithoutReplacement = DisallowedPath < false > ;
22
18
23
- #[ derive( Clone , Debug , Serialize ) ]
24
- pub struct DisallowedPath {
19
+ #[ derive( Debug , Serialize ) ]
20
+ pub struct DisallowedPath < const REPLACEMENT_ALLOWED : bool = true > {
25
21
path : String ,
26
22
reason : Option < String > ,
27
23
replacement : Option < String > ,
28
24
}
29
25
30
- impl < ' de > Deserialize < ' de > for DisallowedPathWithoutReplacement {
26
+ impl < ' de , const REPLACEMENT_ALLOWED : bool > Deserialize < ' de > for DisallowedPath < REPLACEMENT_ALLOWED > {
31
27
fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
32
28
where
33
29
D : Deserializer < ' de > ,
34
30
{
35
31
let enum_ = DisallowedPathEnum :: deserialize ( deserializer) ?;
36
- if enum_. replacement ( ) . is_some ( ) {
32
+ if ! REPLACEMENT_ALLOWED && enum_. replacement ( ) . is_some ( ) {
37
33
return Err ( de:: Error :: custom ( "replacement not allowed for this configuration" ) ) ;
38
34
}
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) ?;
52
35
Ok ( Self {
53
36
path : enum_. path ( ) . to_owned ( ) ,
54
37
reason : enum_. reason ( ) . map ( ToOwned :: to_owned) ,
@@ -68,45 +51,24 @@ pub enum DisallowedPathEnum {
68
51
} ,
69
52
}
70
53
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 {
54
+ impl < const REPLACEMENT_ALLOWED : bool > DisallowedPath < REPLACEMENT_ALLOWED > {
55
+ pub fn path ( & self ) -> & str {
91
56
& self . path
92
57
}
93
- fn reason ( & self ) -> Option < & str > {
94
- self . reason . as_deref ( )
95
- }
96
- fn replacement ( & self ) -> Option < & str > {
97
- None
98
- }
99
- }
100
58
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 ( )
59
+ pub fn diag_amendment ( & self , span : Span ) -> impl FnOnce ( & mut Diag < ' _ , ( ) > ) + use < ' _ , REPLACEMENT_ALLOWED > {
60
+ move |diag| {
61
+ if let Some ( replacement) = & self . replacement {
62
+ diag. span_suggestion (
63
+ span,
64
+ self . reason . as_ref ( ) . map_or_else ( || String :: from ( "use" ) , Clone :: clone) ,
65
+ replacement,
66
+ Applicability :: MachineApplicable ,
67
+ ) ;
68
+ } else if let Some ( reason) = & self . reason {
69
+ diag. note ( reason. clone ( ) ) ;
70
+ }
71
+ }
110
72
}
111
73
}
112
74
@@ -133,10 +95,10 @@ impl DisallowedPathEnum {
133
95
}
134
96
135
97
/// Creates a map of disallowed items to the reason they were disallowed.
136
- pub fn create_disallowed_map < T : AmendDiag > (
98
+ pub fn create_disallowed_map < const REPLACEMENT_ALLOWED : bool > (
137
99
tcx : TyCtxt < ' _ > ,
138
- disallowed : & ' static [ T ] ,
139
- ) -> DefIdMap < ( & ' static str , & ' static T ) > {
100
+ disallowed : & ' static [ DisallowedPath < REPLACEMENT_ALLOWED > ] ,
101
+ ) -> DefIdMap < ( & ' static str , & ' static DisallowedPath < REPLACEMENT_ALLOWED > ) > {
140
102
disallowed
141
103
. iter ( )
142
104
. map ( |x| ( x. path ( ) , x. path ( ) . split ( "::" ) . collect :: < Vec < _ > > ( ) , x) )
0 commit comments